The ProgrammersTalk Community
Forum Register Search Today's Posts Mark Forums Read
Register

Go Back   The ProgrammersTalk Community > General Programming > C / C++


Welcome to the The ProgrammersTalk Community forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.
Reply
 
LinkBack Thread Tools    Display Modes   
  #1 (permalink)  
Old 05-13-2008, 11:48 AM
MrPickle's Avatar
MrPickle MrPickle is offline
Full Programmer
Join Date: Nov 2007
Location: England, Lincolnshire
Posts: 247
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
  #2 (permalink)  
Old 05-15-2008, 08:31 PM
ccoonen ccoonen is offline
PT Staff
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 4
Join Date: Jun 2007
Location: Wisconsin
Posts: 306
iTrader: (0)
ccoonen is on a distinguished roadccoonen is on a distinguished roadccoonen is on a distinguished roadccoonen is on a distinguished road
Don't you have to delete in reverse? like this block - maybe you need to decrement the i (i--) instead of i++ so it acts as a pop from the array? Just a thought (thats how it works with AS3 anyways)
for(int i = 0; i < Length-1; i++){
delete[] Heights[i];
delete[] Normals[i];
delete[] Connections[i];
}
Reply With Quote
  #3 (permalink)  
Old 05-20-2008, 01:30 PM
MrPickle's Avatar
MrPickle MrPickle is offline
Full Programmer
Join Date: Nov 2007
Location: England, Lincolnshire
Posts: 247
iTrader: (0)
MrPickle is on a distinguished roadMrPickle is on a distinguished roadMrPickle is on a distinguished road
I don't think you need to delete in reverse, I never have in the past.

I've changed the function completely now in an attempt to fix it but it's saying "Segmentation Fault" which means it's trying to access something out of range according to wiki. But I don't see how :3

Code:
void Terrain::setNormals(void){
    Vector Sum, Out, In, Left, Right;    
    for(int w = 0; w < Width-1; w++){
        for(int l = 0; l < Length-1; l++){
            Sum.setValue(0.0f, 0.0f, 0.0f);
            
            if(l > 0)
                Out.setValue(0.0f, Heights[w - 1][l] - Heights[w][l], -1.0f);
            
            if(l < Length - 1)
                In.setValue(0.0f, Heights[w + 1][l] - Heights[w][l], 1.0f);
            
            if(w > 0)
                Left.setValue(-1.0f, Heights[w][l - 1] - Heights[w][l], 0.0f);
            
            if(w < Width - 1)
                Right.setValue(1.0f, Heights[w][l + 1] - Heights[w][l], 0.0f);
            
            if(w > 0 && l > 0){
                Sum += Out.CrossProduct(Left);
                Sum.Normalize();
            }
            
            if(w > 0 && l < Length - 1){
                Sum += Left.CrossProduct(In);
                Sum.Normalize();
            }

            if(w < Width - 1 && l < Length - 1){
                Sum += In.CrossProduct(Right);
                Sum.Normalize();
            }
            
            if(w < Width - 1 && l > 0){
                Sum += Right.CrossProduct(Out);
                Sum.Normalize();
            }
            
            Normals[w][l] = Sum;
        }
    }
}

__________________

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
Reply


Thread Tools
Display Modes

   Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -7. The time now is 12:45 PM. Powered by vBulletin
Copyright © 2000 - 2007, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO © 2007 ProgrammersTalk Sedo - Buy and Sell Domain Names and Websites project info: programmerstalk.net Statistics for project programmerstalk.net etracker® web controlling instead of log file analysis


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50