# Slot Car- Follow line and Physics

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

## Recommended Posts

I am making a simple slot car game. My problem is that I am not sure how to start in having the car follow a track and also incorporate that with the physics involved. My main problem is to get the car to follow a curved piece of track while having the car skid+normal car physics (velocity,acceleration etc). Any help would be appreciated...

##### Share on other sites
Do you already know how to do the normal car physics? If so, then the you just need to add a constraint (following the track). As a broad overview, I'd recommend something like this:

Partitioning the space into cubes, each of which has a parametric equation for the track through that cube of space (or flagged if there is no track there). Each eqn could be a 3rd order polynomial or something (x(t) = at^3 + bt^2 + ct^ +d, y(t) = ..., z(t) = ..., where t always goes from 0 to 1)

Then at each timestep you see which cube contains your track-following-point on the car.

solve for the nearest point on the track, and move it to that point.

Calculate the rest of your physics as normal.

##### Share on other sites
Cool. I didnt think of that at all. But one question. How do I get the car to move the correct distance on the curve based on its previous velocity. I am sort of confused about that. Also, if the car is following the curve around, eventually
the velocity would change to a different direction or whatever (depending on the curve) how would that work its way in to the answer you gave?

##### Share on other sites

Say your car is moving up on a 2d track that starts to curve to the left. As the track starts to veer off, your pin-in-track constraint forces the pin back a bit and to the left a bit. So you lose some energy in the y-direction, but gain some in the x-direction. I would not presume to say that energy is conserved, but in real systems you will lose energy through friction, etc. So I wouldn't really worry about it unless this is more a simulation than a game. And as always, small time-steps are your friend.

##### Share on other sites
what you are asking is A.I. driving. It's kind of a complex subject.

As for an example of steering and obstacle avoidance, [google] for boids and autonomous steering bahaviour. It might not be your solution though, but should give you ideas.

##### Share on other sites
Quote:
 Original post by oliiiwhat you are asking is A.I. driving. It's kind of a complex subject.

errr - sure you got the right thread oliii?

Anyway, as for the original question - if you have your basic physics working "just" add a constraint at the pin - in particular, after accumulating your forces and applying collision impulses, then before you update velocity/position do the following:

1. Calculate the force that needs to be applied to the car at the position of the pin normal (i.e. sideways) to the slot direction so that the final sideways velocity of the pin is zero. In fact, if you know the curvature of the slot then you can calculate the sideways velocity that would be required to make the pin follow the slot (assuming its forwards speed doesn't change much) and this will give you an even more accurate force.

2. It's pretty much inevitable that constraining the sideways velocity as above will result in the pin drifting out of the slot. So calculate the positional error and apply an extra sideways force proportional to it that will eliminate the error over a couple of timesteps.

Since you'll only be applying "sideways" forces then constraining the pin to the slot this way won't result in a change in the forwards component of velocity of the CoM.

• ### 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
634091
• Total Posts
3015435
×