Archived

This topic is now archived and is closed to further replies.

Sloth

Car racing collision detection 2d

Recommended Posts

Over the past week or so I''ve been researching and trying to implement Collision Detection to my game. I''ve scowered through this forum, looked at the articles and resources section, but haven''t been able to overcome my problem. The game works by a map array MAP[100][100]. It scrolls around this map drawing from these tiles as an example. The problem: ------------ Implementing a collision detection system which checks for collision with different objects(walls, pit lane, other cars) whilst rotating. The collision detection has to be accurate enough to collide around corners. The last game I worked on I used dx7 and ddraw. For the collision detection in that I locked the backbuffer and check certain points depending on where my character was for certain pixel colors. The approach ive tried sofar to solving the problem was creating a "bitmask" for each tile to check against the background. I tried this by having 2 arrays eg: BYTE SCREEN[640][480]; BYTE tile1[64][64]; memcpy(&SCREEN[tilex][tiley],&tile1,sizeof(tile1)); What I was trying to do here was copy the bitmasks to a screen array at a certain point say SCREEN[64][64], for each of my tiles. Filling the screen array. I would then have check points in SCREEN[][] to see if there was a collision. However this didn''t work. Other info is im using DX8.1 and the ID3DXSPRITE interface. Could someone plz give me some alternative approach I could take to solving this problem.

Share this post


Link to post
Share on other sites
Hey,

I''ve been thinking about a car racing game myself, I haven''t found a solution to how I would like to design my tracks (which would lead to various collision detection methods).

I''ve thought about tile based ideas like yours but I want something more versatile, though I reacon you have the right idea using masks for this type of method. You could have something like a 4 bit mask or something if you wanted to define things like road/grass/impassable/etc.

The only other option I think would be vector type tracks, which I could do in some 3D package, you could place positions of tree, etc as points - this could be easier to convert to 3D....I dunno I''m toying around with the idea still.

The type of game I''m looking to have it like would be circuit breakers from playstation but start with a 3D version.

And your problem....I dont think the memcpy function works in coping arrays chunks like that. memcpy would copy the array into the other large array as a continous chunk of data, but you want each line offset of the small array in the big one. I''m not sure I can explain that much better atm.

Anyway hope you get past that bottleneck, I love them too >:-)

Share this post


Link to post
Share on other sites
Thanks for your input Smorg. It got me to thinking how about having a bitmask for each tile as I said before.




Then use this structure:

struct tiles{

int x, int y; // position of tile
mask[64][64]; // bitmask

}

then create an array:

tiles tile[number_of_tiles_that_fill_screen]; // 75 fill screen



Every time I draw the tiles onto the screen I could update tile:

tile[number].x = position it plots at;
tile[number].y = position it plots at;
memcpy(&tile[number].mask,&tilebitmask[numberoftile],sizeof(tilebitmask[numberoftile]));

Then when I want to test if there is a collision I use the points from the car.



These points can be rotated to find theyre new x and y value by radius from centre and angle of car.

Then to check if there is a collision:

for (x = 0; x < 75; x ++) // loop thru all the tiles on the screen
{

if (tile[x].x > carposition - 64 && tile[x].x < carposition + 64) // if a tile is near car
{

test = tile[x].mask[car.point.x][car.point.y]; // check for all 4 points on the car

if (test == 1)
{
collision = TRUE;
}

}

}

I haven't fully thought this through and can see a problem with getting the right position in the mask array. Oh and another thing I wont have to update all 75 tiles only 9 as the car will only need to check collision in 9 different angles. But for the time being im doing it as the whole screen.

More thoughts would help

[edited by - Sloth on August 30, 2002 9:13:26 AM]

Share this post


Link to post
Share on other sites
hello,

i''m developping a super cars clone, and i use a second bitmap (8 bit) to represent the physical property of my track.
the big disavantage is that is memory and time processing consuming.
the advantage is my AI can use it to find it way.

about detection collision, i use simple box.
i use point on the tires to detect physical property of the floor.

hop i can give you some ideas.

Steph.

sorry for my bad english, i like to eat frogg''s legs

Share this post


Link to post
Share on other sites
I think sagaEterna is right.
Using a second bitmap just for collison info it a waste of time and space unless you have some very intricate, strangely shaped items.

If the area from 0,3 to 5,8 is clear space, then why store :

00000
00000
00000

BYTE tile1[64][64]; = 4096 Bytes or 4 Ints

This is just a small example, size can easily get out of hand.

Also you are wasting Video memory, because if this is a surface it is being stored in video memory, but video memory doesn''t neccesarily provide accelerating for you to access a pixel in memory and check it''s color. This will cause a serious bottleneck between system memory and video memory.

I too am working on a topdown racer. I am using track tiles that are initially read from a track.dat file. There are 12 different tiles that can be used to make up a track of any size.

I am working on a linked list of rects, ie (top,left,bottom,middle) for each source tile.

So in code I see that the player is at worldx,worldy which means he is over :
whichTile = Tile[worldx/tilewidth][worldy/tileheight];
Then I access the Collision Linked list once I know what type of tile he is over.

I''ll give you some more once I finish coding that part...

J.

return(0);

Share this post


Link to post
Share on other sites
One more thing. If you wanted to use a height map, then the other idea might be a good idea. Or a traction map, so different parts of the track have different friction amounts, Like the grass is 255 and the dry track is 0 and you have a shoulder thats somewheres in between.

Also, let''s talk about physics. Car dynamics are very complicated. How are you moving your car? any tricks you wanna share? and good websites ...

J.

return(0);

Share this post


Link to post
Share on other sites
Hey cult,

I found this webpage that went right into the car physics stuff, but right into means just that....overly complex for a game that I would want to make.

Anyhow, I want to have a car that slides around corners, but it doesn''t work too well with the method I''m trying. Basically I turn the image of the car to a different direction I''m facing. I got this idea from the game trophy at www.clanlib.org.

Let me know if you have a good idea yourself.

Share this post


Link to post
Share on other sites
>>smorg123
cars can appear to slide if you turn the car as soon as the user cranks the wheel, but lazily update the Vx,Vy ...

1. calculate newVx,newVy based on present car direction
2. average with old values
ie Vx = (Vx + newVx) / 2; // same for y
3. to make it slide more at higher speeds...
try :
Vx = (Speed * Vx + newVx ) / (Speed + 1); // and y

Depending on you range of values for speed, this might be too strong ... experiment by replacing Speed with (Speed / ?) on both sides of the equation. Also if speed can be -1 watch out for the divide by zero!

I would have to call this an "Asteroid" car physics model...

J.

return(0);

[edited by - cultofpurplecabbage on September 4, 2002 1:52:08 AM]

Share this post


Link to post
Share on other sites