# Collision detection for circular racing tracks

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

## Recommended Posts

Hello All! hope everyone had a good new year... I am working on a car sim and i'm having difficulties implementing collision detection on the circular track. Not quite sure of the algorithm to use... 1. intersection with a plane? (in my case the plane or boundary of the track is oval/circular) can anyone offer me some suggestions please.. many thanks J

##### Share on other sites
What you want to do depends on the what your track is like. Firstly is it 3D or 2D? And collision to the track or the stuff around the track. Give a little more information about your game.

##### Share on other sites
If your track is circular, then you are basically working with a sphere. Or in 2D a circle.

You can simply use sphere versus sphere collision detection (seach google) but I will explain one way you could do it. The collision may be a little inacurate (rectangular shaped car versus circular collision radius), but the simplicity of this method makes up for it.

The basic rule is that if the distance of your vehicle from the tracks center point plus the radius of the cars collision radius is greater than the radius of the tracks circle then you most definately have a collision.

So want some code? Lets both assume that we are working in C++ and 2D here.

  // this vector is from the center of the circular track to the vehicle  Vec2D vecCollide = car.GetPosition() - track.GetPosition();  // get the length of this vector use pythagorus  float dist = vecCollide.Length();  // we add the car radius to the dist so that the car radius is accounted for  // if we do not do this the car will penetrate to its center  dist += car.GetRadius();  // we know that if the distance of the car from track center is  // greater than the tracks radius we have collided  if (dist > track.GetRadius())  {    // this car has collided so do some funky collision handling here    // you can use the normalised version of vecCollide to help with your    // collision reaction, do not forget to inverse the normal  }

The immediate issue with this method is it will only act once a collision has occured, you get no information of where the collision occured. The lower your frame rate the more obvious this issue will be.

A simple way to solve this is to use the negative velocity of your vehicle and trace back until no collision with the vehicles collision radius is detected.

This approach is very simple, its probably not a good method to use for anything but messing about, why did I show you this then?I figure this will help you nail down some basics in your own mind. From this knowledge you can take bigger, more complicated and more effective steps on your own.

Feel free to message me with any specifics if you need to.

##### Share on other sites

SuperNerd The game is 3D and I am trying to test for collision with the boundry of the track.
The car sits between two boudaries and i am trying to prevent the car from going beyond them.
Please download the demo from the following URL to better understand what i am trying to achieve.

ps lubby will try ur idea and get back to u

http://www.geocities.com/chukaegbunike/Chuka_games.html

##### Share on other sites
I'd do a 'portal-like' system
Split the track up into segments (lots of them to make it look curved), each one made up of 4 boundry planes
two of them on opposite sides will be the walls of the track, the plane in front and back join up to the next and previous track segment.
depending on what segment the car is currently inside of, you can test the two wall planes for that segment to check if it is inside the track or not.
You also know when the car moves to the next segment if it crosses the front or back planes of the current segment.
all just simple plane intersection test

##### Share on other sites
You can best model this collision detection in 3D then when you want bridges or similar your collision will still work.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 10
• 11
• 13
• 9
• 11
• ### Forum Statistics

• Total Topics
634089
• Total Posts
3015460
×