🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

High speed partical collision detection

Started by
8 comments, last by Zenroth 24 years, 5 months ago
Ok ive been messing around with objects traviling at speeds of around 18 pixels a frame ,and at 60 or 75 frames a sec that gets pretty fast. So the problem ive been having is the object moving so fast it goes through the a object it should collide with. I tried some code like this but it didint seem to help. for(int x=Ball.oldx;x>Ball.x;x--) for(int y=Ball.oldy;y>Ball.y;y--){ check_collision(&object,x,y);} The above is a small 2 pixel ball that is updated by the standered x+=xv*time; I save the old x and y postion before moving however. As far as i can see the code above should work. I mean it should check pixel by pixel where the ball moved and test for a collision. However it seems 98% of the time the ball will just fly through the object. Any help would be great.
Advertisement
OK, first of all, your code won't work if the ball is moving right or down (in that case, old x/y is less than new x/y). To get it to work, you'd have to first go through and find the min/max, and loop through that:

int minx,maxx,miny,maxy;
if (Ball.oldx < Ball.x) { minx = Ball.oldx; maxx = Ball.x; }
else { minx = Ball.x; maxx = Ball.oldx; }
if (Ball.oldy < Ball.y) { miny = Ball.oldy; maxy = Ball.y; }
else { miny = Ball.y; maxy = Ball.oldy; }

for (int x=minx; x <= maxx; x++)
for (int y=miny; y <= maxy; y++) check_collision(&object,x,y);

Even if you get that code working, though, it'll be wicked slow, because you're checking a box whose corners are min x/y and max x/y.

If you need something faster, my suggestion is to get a geometry book and look up the equation that tells you if a line intersects a point (I don't remember it exactly). Make a line out of the point's last position and current position, then use the equation to see if that hits any other points.

Mason McCuskey
Spin Studios - home of Quaternion, 2000 GDC Indie Games Fest Finalist!
www.spin-studios.com

Edited by - mason on 1/12/00 11:44:18 AM

Edited by - mason on 1/12/00 11:45:38 AM

Edited by - mason on 1/12/00 11:50:02 AM
Founder, Cuttlefish Industries
The Cuttlefish Engine lets anyone develop great games for iPad, iPhone, Android, WP7, the web, and more!
Thanks alot manson the code works great. It even fixed a differnt problem I was having. The fps cost isn''t that bad about 4-8 fps. The only problem is i need it to break outa the loop when a collision happens.For some reason through i cant get that to work. Perhaps its becuase I havn''t really worked with for loops that much cept in tileing.
You should be able to use "break" to break out of the loop... Just make sure to break out of every loop if you are using nested loops, instead of just the first one...
Ya thats what is messing with me i tryed using break; i even tried returns,but i always get syntax errors. Hell i even tried to use a goto to break out.
what syntax errors are you getting?
Ok say i use something like this.

void check(){
for (int x=minx; x <= maxx; x++)
for (int y=miny; y <= maxy; y++)
if(collision_check(&paddleleft,x,y)==1)p1h=TRUE,break;}

Ill get a error C2059 syntax error : ''break''

Atleast thats what i get from vc++

In the previous code snippet break has to be in the same code block of the loop you''re trying to break out of. You can''t call it from a function and expect it to percolate out of the function into your loop.

If you try to use goto to move from one function to another I believe you have to use set_jmp and long_jmp functions. (Or something like that)
Hmm i just want to exit the function and return control to the function that called it. Of course i want to do that when a collision is detected.
To exit the function you should be able to call return. Even if the function is void, you can just call return without a value to exit the function. I think the problem with the last code you posted is that you have nested for loops, but only 1 call to break. It would break out of the inner loop, but stay in the outer one. It should be something like

for(...){  for(...)    if(...)      doBreak = true;      break;  if(doBreak)     break;  } 

Or something like that. I don''t know if that''s the best way to do it right now, because I''m sort of tired
But the point is you have to break out of both loops, or, if I''m not wrong, you could just return from the if statement, because I think that will leave the function, no matter how many layers inside the function with for loops and if statements it is.

------------------------------
Jonathan Little
invader@hushmail.com
http://www.crosswinds.net/~uselessknowledge

This topic is closed to new replies.

Advertisement