View Single Post
  #1 (permalink)  
Old 05-13-2008, 11:48 AM
MrPickle's Avatar
MrPickle MrPickle is online now
Sr. Programmer
Join Date: Nov 2007
Location: England, Lincolnshire
Posts: 279
iTrader: (0)
MrPickle is on a distinguished roadMrPickle is on a distinguished roadMrPickle is on a distinguished road
Code causing my computer to lock up

I really have no clue what's wrong with this :3 I suspect it's something to do with my class's operators or the way I've created the multidimensional arrays.

It only locks up when I close my program so that makes me think it's something to do with the multidimensional arrays not being deleted properly but then it works fine (and closes fine) if I comment out the setNormals function.

Not much more I can say, here's the code.

How I create the multidimensional arrays:
Code:
//declared in the class as:
        float **Heights;
        Vector **Normals;
        int **Connections;

                Heights = new float*[Length];
                Normals = new Vector*[Length];
                Connections = new int*[Length];
                for(int i = 0; i < Length-1; i++){
                    Heights[i] = new float[Width];
                    Normals[i] = new Vector[Width];
                    Connections[i] = new int[Width];
                }
This is how I delete them in the class's destructor:
Code:
    for(int i = 0; i < Length-1; i++){
        delete[] Heights[i];
        delete[] Normals[i];
        delete[] Connections[i];
    }
    delete[] Heights;
    delete[] Normals;
    delete[] Connections;
and here's the function that's making me lock up:
Code:
    int i, j;
    Vector Vertex1, Vertex2, Vertex3, Edge1, Edge2;
    for(i = 0; i < Width - 1; i++){
        for(j = 0; j < Length - 1; j++){
            Vertex1.setValue(j, Heights[i][j], i);
            Vertex2.setValue(j+1, Heights[i][j+1], i);
            Vertex3.setValue(j+1, Heights[i+1][j+1], i+1);
            Edge1 = Vertex2 - Vertex1;
            Edge2 = Vertex3 - Vertex1;
            Edge1.Normalize();
            Edge2.Normalize();
            
            Normals[i][j] = Edge1.DotProduct(Edge2);
            Normals[i][j].Normalize();
            Connections[i][j]++;
            Connections[j+1][i]++;
            Connections[j+1][i+1]++;
        }
    }
    
    for(i = 0; i < Width - 1; i++){
        for(j = 0; j < Length -1; j++){
            Normals[i][j].x /= Connections[i][j];
            Normals[i][j].y /= Connections[i][j];
            Normals[i][j].z /= Connections[i][j];
        }
    }
If it's something to do with my vector's operators then here's my vector functions & operators:
Code:
Vector::Vector(float vx, float vy, float vz){
    x = vx;
    y = vy;
    z = vz;
}

void Vector::setValue(float vx, float vy, float vz){
    x = vx;
    y = vy;
    z = vz;
}

Vector Vector::CrossProduct(Vector A){
    Vector Temp;
    Temp.x = x * A.x;
    Temp.y = y * A.y;
    Temp.z = z * A.z;
    return Temp;
}

Vector Vector::DotProduct(Vector A){
    Vector Temp;
    Temp.x = (y * A.z) - (z * A.y);
    Temp.y = (z * A.x) - (x * A.z);
    Temp.z = (x * A.y) - (y * A.x);
    return Temp;
}

void Vector::Normalize(){
    float Mag = Magnitude();
    x *= Mag;
    y *= Mag;
    z *= Mag;
}
    
float Vector::Magnitude(){
    float Magnitude = sqrt((x * x) + (y * y) + (z * z));
    return Magnitude;
}

Vector Vector::operator+ (Vector Add){
    Vector Temp;
    Temp.x = x + Add.x;
    Temp.y = y + Add.y;
    Temp.z = z + Add.z;
    return Temp;
}

Vector Vector::operator- (Vector Sub){
    Vector Temp;
    Temp.x = x - Sub.x;
    Temp.y = y - Sub.y;
    Temp.z = z - Sub.z;
    return Temp;
}

Vector Vector::operator* (Vector Mul){
    Vector Temp;
    Temp.x = x * Mul.x;
    Temp.y = y * Mul.y;
    Temp.z = z * Mul.z;
    return Temp;
}

Vector Vector::operator/ (Vector Div){
    Vector Temp;
    Temp.x = x / Div.x;
    Temp.y = y / Div.y;
    Temp.z = z / Div.z;
    return Temp;
}

void Vector::operator= (Vector Equ){
    x = Equ.x;
    y = Equ.y;
    z = Equ.z;
}

void Vector::operator++ (void){
    x++;
    y++;
    z++;
}

void Vector::operator-- (void){
    x--;
    y--;
    z--;
}

void Vector::operator+= (Vector AddEqu){
    x += AddEqu.x;
    y += AddEqu.y;
    z += AddEqu.z;
}

void Vector::operator-= (Vector SubEqu){
    x -= SubEqu.x;
    y -= SubEqu.y;
    z -= SubEqu.z;
}

void Vector::operator*= (Vector MulEqu){
    x *= MulEqu.x;
    y *= MulEqu.y;
    z *= MulEqu.z;
}

void Vector::operator/= (Vector DivEqu){
    x /= DivEqu.x;
    y /= DivEqu.y;
    z /= DivEqu.z;
}

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote