Collision detection
I''m making a 2d top-down race-game, and I am looking for a way to detect a collision between the race-car and the wall of the track, so that the car keeps on the track. I think some sort of color scan is a solution, so that the road has another color than the walls next to the road. So when the car collides with the wall, the car crashes. Does anyone know how to make such a colorscan in VC++ 6.0 with Direct Draw 6.0? Or can anyone tell me where I can find information that could help me?
thanks
You're too vague.
Is the track curved? Does the car have a circle or rectangle bounding box? Do you want perfect collision detection, but willing to sacrafice speed?
Source code examples of the problem really help
Edited by - gi-centerprintf on October 10, 2000 4:32:40 PM
Is the track curved? Does the car have a circle or rectangle bounding box? Do you want perfect collision detection, but willing to sacrafice speed?
Source code examples of the problem really help
Edited by - gi-centerprintf on October 10, 2000 4:32:40 PM
October 10, 2000 03:45 PM
gi-centerprintf, he did describe his problem well enough for me at least.. Maybe you just didn''t understand the question about colorscan.
Just_me, if you''re going to do a scrolling map, use an array. Lets say array[1024][1024] should be big enough and it only takes a megabyte of memory. You could allocate the memory for it with new or malloc, but it''s not necessary.
Then draw your map in your array[y][x], and when you need to draw it to screen, just draw a part of the whole map to screen (obviously). You could use DirectDraw surfaces to get speed.
If you use a DD surface, you''ll have to lock it to gain access to it''s memory.
So to check the collision detection:
Lets say wall is color 0-127 and road is colors 128-255..
Your car is at location carx,cary and it''s sizes are xs and ys (pixels).
Then just check:
I hope that this helped!
Just_me, if you''re going to do a scrolling map, use an array. Lets say array[1024][1024] should be big enough and it only takes a megabyte of memory. You could allocate the memory for it with new or malloc, but it''s not necessary.
Then draw your map in your array[y][x], and when you need to draw it to screen, just draw a part of the whole map to screen (obviously). You could use DirectDraw surfaces to get speed.
If you use a DD surface, you''ll have to lock it to gain access to it''s memory.
So to check the collision detection:
Lets say wall is color 0-127 and road is colors 128-255..
Your car is at location carx,cary and it''s sizes are xs and ys (pixels).
Then just check:
bool collision(){for (y=0;y<ys;y++)for (x=0;x<xs;x++){ if (maparray[cary+y-ys/2][carx+x-xs/2]<128 && cargfx[y][x]>0) return 1; //collision occured}return 0; //no collision}
I hope that this helped!
Look into some methods for point-in-polygon tests, and map out the roads with 4-sided polygons. The biggest reason for this is that it will give you the ability to ''deflect'' the car off the walls, by comparing the angle of impact to the angle of the wall - I find it hard to visualise how you''d be able to do that with a colour-scan alone (unless you made all your walls run horizontal or vertical, and never at any other angle...)
It may seem like over-kill now, but it should be as easy to implement as the colour scan stuff, and the results would be much better.
Bye
Matt
Check out my project at:www.btinternet.com/~Matthew.Bennett
It may seem like over-kill now, but it should be as easy to implement as the colour scan stuff, and the results would be much better.
Bye
Matt
Check out my project at:www.btinternet.com/~Matthew.Bennett
October 11, 2000 10:22 AM
4-sided polygons when the game is 2d? Even 2d-vectors would be hard to do: intersection math and putting them in every single level . You''d need an editor for that and you''d be in trouble again..
You can very easily calculate an accurate wall angle at the point you hit ground, even with color-scan systems. And then you''ll get as good collision results as with polygon/vector systems, or even better.
That''s it..I''ve implemented it already in my 2d game and it works really great.
You can very easily calculate an accurate wall angle at the point you hit ground, even with color-scan systems. And then you''ll get as good collision results as with polygon/vector systems, or even better.
bool collision(){int w_x=0; //vector from car to wall. set it to zero at startint w_y=0;for (y=0;y<ys;y++)for (x=0;x<xs;x++){if (maparray[cary+y-ys/2][carx+x-xs/2]<128 && cargfx[y][x]>0){w_x+=x-xs/2;w_y+=y-ys/2;}}if (w_x || w_y){//wall is at direction w_x, w_y. return 1;}return 0; //no collision}
That''s it..I''ve implemented it already in my 2d game and it works really great.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement