# Couple problems with collision

This topic is 3567 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I currently have a working collision, but I have a few problems with it that I can either not figure out or don't know how to go about it. My character walks around and the way his height moves is by finding what triangles he is standing over, get the highest triangle, and translate the height to that triangle. Then when he moves his height is updated by getting the next list of triangles he is standing over, and moves to the triangle that is highest. Here is where the problem comes in. I have a bridge that is sort of old looking, and have gaps here and there so there is empty space underneath him. When he walks over the bridge and steps on a missing part he instantly falls to the ground below him since that is the only triangle he is standing over. Then there's other times when I try to make him walk up a tree and it translates the right height, but in the wrong direction, then the next time he moves he's at the right height in the right direction. So.. 1) what way can I make my collision know when to translate the world up or down when my character steps over the bridge, and needs to stay on the bridge. 2) what could be causing the weird glitching up and down in wrong directions 3) when my character is standing over nothing, just blackness, it reads that he is standing over a triangle no matter where he is. I tried a couple different shapes including just planes, what could cause this? 4)Should I update my characters height by setting the height, or by increasing the height by the difference? Here is some code, this is literally all I have for collision detection.
///info: get's the heightest triangle height
///param: vector of points from the list of triangles, one number represents the highest point in one triangle
second is the current y position
float GetNearestY(vector<float> input, float cur)
{
if(input.empty()) return cur;
float y = 0;
bool flag = false;

for(unsigned int i = 0; i < input.size(); i++){

if(input > y){
y = input;
flag = true;
}
}

if(flag) return y;
else return cur;
}

///info: WithinPolygon works correctly (checks which triangle he is standing over)
///and ID is for determining what is the character and what is not
if(WithinPolygon(v3_origin, v3one, v3two, v3three) && ID != SHOW_ALWAYS){

//set up the list of highest points from each triangle he is standing over
y_list.push_back(max(v3one[1],v3two[1],v3three[1]));

SetWindowText(Hwnd,"Within Triangle");    //set window title
}

ypos = -GetNearestY(y_list, ypos); //set y position



##### Share on other sites
for 1) as for your bridge, you could make invisible triangles on which he could stand.

2) no idea. gaps between triangles?

for 3) you do clear the y_list every frame?

4) should not matter.

##### Share on other sites
1) I'm not sure how I would make invisible triangles unless I stored the triangle data in an array or vector and did not render it, but told the collision detection that it was rendered. That could possibly work, but that would mean I need to make a whole other layout that would be the walking collision mesh that basically laid over the visual meshes, and that's more to load, keep track of, so on. I like this idea though, cause then I wouldn't have to worry about the character standing on top of more than one triangle at a time, and it would speed up what it has to calculate.

2) I should have clarified this. He moves up and down at insane translations, sometimes so much the scene is no longer visible. Although it does have something to do with the gaps of triangles and how I am figuring out the height.

3) Yes, y_list.clear() at the end of each frame rendering.

##### Share on other sites
- If the nearest Y is a long way (i.e. further than he will fall in one logic frame) below the player, don't clip to it; instead, let gravity do its job.
- If the nearest Y is more than a specified value above the player's current location (the value needs tuning by experiment), reject the move, i.e. put the player back to their previous location, play a 'no move' sound or animation, take 'run into wall' damage etc as appropriate.

##### Share on other sites
Thanks Bob, and oliii that helped me figure some things out. I originally made a parameter in the render function to tell the collision what was and was not the character, and I used the same parameter to tell it if I want ground collision, tree/wall collision, or no collision at all. So far I only have the ground and no collision working, but I think this will work out for me. Thanks again.

##### Share on other sites
while it is nice to make your algorithm work as it is general solution, you should consider having separate mesh for collision detection.

triangles in this mesh can always be spaced appropriately to avoid most, if not all, of your problems.

by the way you describe your geometry, your scene looks very, very... ultra detailed, with many little polygons which makes collision algorithm much more heavy than necessary with all that grouping and choosing... collision mesh, it seems, could be as much as thousands of times more simple than your scene geometry. the increase in speed could be nice and your whole bridge could be as simple as three polygons in "invisible collision mesh", so no jumps or holes there, unless you want them.

##### Share on other sites
Actually the bridge is not that detailed. Way less than what you'd find in WoW or the like. It has planks that have 2 faces it needs to figure out, being the top surface and bottom surface. I turned collision detection off and the FPS was the same as collision on, so it really is not wasting a lot of speed. It tests if the player is standing over a triangle and if not, don't do anything. It works very well and smooth now, but my only problem now is figuring out the correct terrain height so my character doesn't jump up and down in large spurts. I think it's called interpolation, but the way I've been trying to figure it out is with barycentric coordinates since that is how I test if the player is inside a certain triangle or not. This doesn't seem to work that well since my player gets shot off in to infinity. If anyone has ideas on what they did or what I can do I'd love to hear them. I've tried making numerous triangles to figure out the height, used barycentric coords, and used sines and cosines but I noticed using trig functions drastically slows things down when used a lot.

##### Share on other sites
in any case, i do not remember that i've seen any 3D games lately that do not use collision mesh. everyone is using it and part of the reason are problems like yours. it usually does not slow down but speeds up.

- does your character goes smoothly from single flat polygon to inclined single polygon?

also try not setting "float y = 0", but try to remember the last one and adjust the new height from there. you can also limit this "adjustment amount" per unit time - that will smooth it, but that will not look good with steps and other large/sharp bumps.

when you have collision mesh, then instead of stairs you can have one flat polygon and character will look as if it is climbing stairs once you actually see stairs and not collision mesh.

##### Share on other sites
It would be smooth if my interpolation worked. Right now it jumps since it only sees the highest point of the 3 vertices of a triangle, and chooses which one to adjust the height to according to that. Once I get some form of interpolation working it will be smooth. If it's not called interpolation then someone correct me.

Also, in my function it does remember the last y position from the float cur parameter. The comment seems to be cut off in my window saying that. I've changed it around a little so it does not look exactly like that, but still similar.

##### Share on other sites
Quote:
 It would be smooth if my interpolation worked. Right now it jumps since it only sees the highest point of the 3 vertices of a triangle, and chooses which one to adjust the height to according to that.

i suppose if you trow some debug output and check what kind of numbers you have there. some printf() to actually print out all the vertices heights and see if algorithm have the right numbers to choose from.

that is why would i test first with two polygons, then three, four...

actually, this might solve it...

instead to deal with 3 vertices per each triangle, try to take an AVERAGE, so if triangle has v1(x,y,10), v2(x,y,15), v3(x,y,12)

you should do (10+15+12)/3 and get its average height. if you do that for all of the triangles you will practically do some kind of interpolation within the geometry.

also,
when character moves to new position and you have to pick the next triangle. first calculate averages of all of them, then pick the one that has the closest average height to your previous one.

with all that said, i still think you should do some debugging 1st and see if the function actually works with correct values.

1. 1
Rutin
32
2. 2
3. 3
4. 4
5. 5

• 13
• 58
• 11
• 10
• 14
• ### Forum Statistics

• Total Topics
632967
• Total Posts
3009558
• ### Who's Online (See full list)

There are no registered users currently online

×