# [fixed]this has me stumped

## Recommended Posts

I have a problem with one of the primitives in my collision detection system. I want to have three lines attached to each triangle, going around the vertices. one from vertex 0 to vertex 1 one from vertex 1 to vertex 2 one from vertex 2 to vertex 0 When I draw the lines, to check they are in the correct place, the first two work but the last one is drawn wrong. It starts at vertex 2 but doesn't end at vertex 0. Here is a screenie: The small line in the right bottom corner is supposed to be there. Its a line segment I'm testing for intersection with the triangle. That works just fine. Here is the source code to build the triangle:

void triangle::build(g_vector V1, g_vector V2, g_vector V3)
{
vertices = (g_vector *)malloc(sizeof(g_vector)*3);
lines = (line_segment *)malloc(sizeof(g_vector)*3);
num_vertices=3;
vertices[0]=V1;
vertices[1]=V2;
vertices[2]=V3;

plane.build(V1,V2,V3);

lines[0].build(vertices[0],vertices[1]-vertices[0]);
lines[1].build(vertices[1],vertices[2]-vertices[1]);
lines[2].build(vertices[2],vertices[0]-vertices[2]);
contacted = 0;
r=0; g=1; b=0;
};


and to draw a triangle:
void triangle::draw()
{
if(contacted)
glColor3f(1, 0, 0);
else
glColor3f(r, g, b);

glBegin(GL_POLYGON);
for(uint i = 0; i < num_vertices; i++){
float x,y,z;
p.normal.get_vector(x,y,z);
glNormal3f(x,y,z);
glVertex3f(vertices[i].x,vertices[i].y,vertices[i].z);
};
glEnd();

lines[0].draw();
lines[1].draw();
lines[2].draw();

};


anybody see what i did wrong? [Edited by - speciesUnknown on August 5, 2007 12:23:22 AM]

##### Share on other sites
I dump the lines to the console.
Format is:

start of line1
start of line plus direction of line1

start of line2
start of line plus direction of line2

start of line3
start of line plus direction of line3

-8.600000,-1.000000,-11.300000
3.600000,0.000000,3.000000

3.600000,0.000000,3.000000
10.000000,2.600000,-6.900000

10.000000,2.600000,-6.900000
-8.600000,-1.000000,-6.900000

They should meet at the end.

##### Share on other sites
sphen_lee    199
I assume this is C++ code and that g_vector is a class (since you have called member functions from it). In that case you should not be using malloc, since it won't call any constructors, you should use:

vertices = new g_vertex[3];

(which is easier to read and understand anyway :P )

This is probably not what is causing the error, but it may well be (without more details of what g_vector does in its constructor, if anything, it's hard to say).

##### Share on other sites
Frounk    144
lines = (line_segment *)malloc(sizeof(g_vector)*3);

I think your error is trying to allocate enought space for three g_vector instead of three line_segment making you play in memory where you shouldn't :P

And sphen_lee is also right about the way you create your objects. When using a class, you should use the "new" operator to make sure the constructor is called.

##### Share on other sites
g_vector doesn't have a constructor. Neither do my other primitives. They get defined and destroyed within function scope, particularly the vector class which is used within statement scope, for example:

distance_to_target=(this.position-target.position).get_length();

I've overloaded the appropriate operators for vectors and matrices.

Is that a problem? I often use classes in this way. As a result, I prefer to use malloc when defining an array of such data types. I use new when I am dealing with pointers, because as you say, its easier to read.

It contains junk data until its initialized, but that only causes a problem if I don't initialize it, in the same way as an uninitialized int.

Here is the build method for my line_segment class. It doesn't have a constructor either.

void line_segment::build(g_vector s, g_vector e){	start = s;	direction = e;};

I'm initializing them in the triangle build function, as pasted in my original post.

g_vectors seem to work perfectly. I've been using the same class since I wrote it 18 months ago. But, my system of not using constructors might be the problem. I've never really thought about it before.

##### Share on other sites
Quote:
 Original post by Frounklines = (line_segment *)malloc(sizeof(g_vector)*3);I think your error is trying to allocate enough space for three g_vector instead of three line_segment making you play in memory where you shouldn't :P

:-/

Never again will I use copy and paste.

If you were not so far away I'd buy you a pint. Thanks.

Frounk    144

##### Share on other sites
iMalc    2466
Quote:
 Original post by speciesUnknownHere is the build method for my line_segment class. It doesn't have a constructor either.*** Source Snippet Removed ***
It is probably better to use a constructor for that. You can still have a parameterless constructor that does nothing if you're worried about data being initialised needlessly and then changed.

void line_segment::line_segment(g_vector s, g_vector e) : start(s), direction(e){}void line_segment::line_segment(){}

##### Share on other sites
Thanks,
I started another thread to discuss my policy of not always using constructors,

http://www.gamedev.net/community/forums/topic.asp?topic_id=458567

and I've come to the same conclusion.

##### Share on other sites
Zahlman    1682
A triangle always has three vertices (by definition); why would you be doing any dynamic allocation at all (or for that matter, storing a vertex count)?

In fact, what your "build" functions are doing is exactly what the constructors should be doing. And since it has to happen sometime, you're not going to sell me any performance arguments :P (But you *do* know about initialization lists, yes? Oh, and "contacted" sure sounds like it should be a boolean value...)

And what's a g_vector, anyway?

triangle::triangle(const vector& V1, const vector& V2, const vector& V3) :                   r(0), g(1), b(0), contacted(false), plane(V1, V2, V3) {	vertices[0] = V1;	vertices[1] = V2;	vertices[2] = V3;		lines[0] = line(V1, V2 - V1);	lines[1] = line(V2, V3 - V2);	lines[2] = line(V3, V1 - V3);}

(And that's ignoring potential deeper design problems, such as redundancy in the stored data...)

##### Share on other sites
I think you just flamed me, even if you didn't mean to.
Is it a crime to be new to this stuff? Its complicated, I'm not a mathemetician, and I generally get good advice from those who post here.

The rest of you have been very understanding. Thanks for your comments.
Quote:
 Original post by ZahlmanA triangle always has three vertices (by definition); why would you be doing any dynamic allocation at all (or for that matter, storing a vertex count)?

Not fair. Of course I knew that a triangle has three angles. Are you being sarcastic? Thats a little offensive.

Quote:
 Original post by ZahlmanIn fact, what your "build" functions are doing is exactly what the constructors should be doing. And since it has to happen sometime, you're not going to sell me any performance arguments :P

Unless I want to rebuild them without redefining them. I'm experimenting with different stuff and I like learning by experimentation.

Quote:
 Original post by ZahlmanOh, and "contacted" sure sounds like it should be a boolean value...)

Not fair. c++ doesn't have a bool, remember?
Just for you, from this day forth ill define my own bool using an enum.
int collided is used so i can render a collision mesh and see which triangles collided with something. I draw them red if they collided and their native color if they didn't.

Quote:
 Original post by Zahlman(And that's ignoring potential deeper design problems, such as redundancy in the stored data...)

When I understand what I'm doing ill use vertex references to avoid redundant data, like I do in the meshes that get passed to the renderer. But I'm learning right now and I don't want the added complexity.

Quote:
 Original post by ZahlmanBut you *do* know about initialization lists, yes?

And I'm *obliged* to use them, yes?

Your not being fair. The design issues are there as a result of me piecing together stuff I found and stuff I already had. I'm aware of them and when i write my collision detection system for real I'll design it first.

Quote:
 Original post by ZahlmanAnd what's a g_vector, anyway?

ill give you a couple of clues: my first name is Gavin and its a vector class.
I wrote it to teach myself vector maths, matrix maths, operator overloading, and a few other things. It was a big achievement, and now your putting me down because of the name?

Quote:
 Original post by Zahlman[various sniping comments]

You were new to this once.

[Edited by - speciesUnknown on August 4, 2007 11:51:22 PM]

##### Share on other sites
Endar    668
Quote:
 I think you just flamed me, even if you didn't mean to. Is it a crime to be new to this stuff? Its complicated, I'm not a mathemetician, and I generally get good advice from those who post here. I know your trying to help but your not being very helpful.
If he didn't mean to, then it's not really a flame. Don't worry about people flaming you because it's sometimes hard to tell if they are. Just ignore all the words except the technical advice.

Quote:
 Not fair. Of course I knew that a triangle has three angles. Are you being sarcastic? Thats a little offensive.
No, he is simply saying that because a triangle has 3 and only three points, you can simplify your code, which will make it easier to understand and to debug.

Quote:
 Unless I want to rebuild them without redefining them. I'm experimenting with different stuff and I like learning by experimentation.
No problems with that for some objects, but just keep in mind that other objects are so small and take up so little memory and processing power to create, that you might as well remove the 'build' functions and use a constructor. The less functions you have, the less functions you have to worry about working/debugging or refactoring.

So, keep the 'build' functions, but don't keep them there because you thought it was a good idea 5 minutes ago, make sure it is still a good idea now ('now' defined as any time you look at your code).

Quote:
 Not fair. c++ doesn't have a bool, remember? Just for you, from this day forth ill define my own bool using an enum.
Now you're just being silly. For starters, the 'bool' type is built-in for C++.

Quote:
 When I understand what I'm doing ill use vertex references to avoid redundant data, like I do in the meshes that get passed to the renderer. But I'm learning right now and I don't want the added complexity.
Excellent point. Just make sure that you attempt to reduce the complexity in every part of your code as much as possible. It makes every soooo much easier later on down the track.

Quote:
 And I'm *obliged* to use them, yes?
Of course not. But, consider the fact that they exist for a reason.

Quote:
 Your not being fair. The design issues are there as a result of me piecing together stuff I found and stuff I already had. I'm aware of them and when i write my collision detection system for real I'll design it first.
As long as you understand that design issues exist and are willing to learn from the responses to your questions.

Quote:
 ill give you a couple of clues: my first name is Gavin and its a vector class.I wrote it to teach myself vector maths, matrix maths, operator overloading, and a few other things. It was a big achievement, and now your putting me down because of the name?
Okay, now you're being a jerk. Remember that everyone started at that stage where we wrote our own math classes. Don't automatically assume that someone is flaming you because they have a question about code that you have written.

It was a fair question and you will face many, many more questions like that. If you don't like what someone is saying, just ignore them. You will either realise that they weren't trying to put you down, or everyone will see that they were being a jerk and you were right. People remember that.

It's a win-win situation for you.

Quote:
 I apologize for being so obviously inferior to you and will strive to improve myself in future.
Sarcasm is irrelevant. All that will happen now is that a bunch of people saw you getting annoyed at someone who was trying to help. It is now slightly less likely that people will try to help you.

##### Share on other sites
Thanks,
I must admit I was annoyed by the tone of that post, an needed to somehow demonstrate that everything i do is for a reason.

c++ has a bool type built in? I don't know how that managed to get past me. How foolish.

I'm new to this, I'm learning, and I get upset when people write in a chiding tone. It may not be intentional but I smelt something burning.

##### Share on other sites
Endar    668
Quote:
 Original post by speciesUnknownI'm new to this, I'm learning, and I get upset when people write in a chiding tone. It may not be intentional but I smelt something burning.

I was the same way when I started, but I soon found that if people are actually answering posts it means that they want to help, and the people that don't make it really, really obvious. [grin]

##### Share on other sites
I need to get a book on how to understand people, they are far more complicated than 3d geometry. And you cant reprogram them when you get undesirable data.