How do I make cars on a one-dimensional track avoid collisions?
Posted
by
user990827
on Game Development
See other posts from Game Development
or by user990827
Published on 2014-08-23T20:04:51Z
Indexed on
2014/08/23
22:36 UTC
Read the original article
Hit count: 190
three.js
Using three.js, I use a simple spline to represent a road. Cars can only move forward on the spline. A car should be able to slow-down behind a slow moving car. I know how to calculate the distance between 2 cars, but how to calculate the proper speed in each game update?
At the moment I simply do something like this:
this.speed += (this.maxSpeed - this.speed) * 0.02; // linear interpolation to maxSpeed
// the position on the spline (0.0 - 1.0)
this.position += this.speed / this.road.spline.getLength();
This works. But how to implement the slow-down part?
// transform from floats (0.0 - 1.0) into actual units
var carInFrontPosition = carInFront.position * this.road.spline.getLength();
var myPosition = this.position * this.road.spline.getLength();
var distance = carInFrontPosition - myPosition;
// WHAT TO DO HERE WITH THE DISTANCE?
// HOW TO CALCULATE MY NEW SPEED?
Obviously I have to somehow take current speed of the cars into account for calculation.
Besides different maxSpeeds, I want each car to also have a different mass (causing it to accelerate slower/faster). But this mass has to be then also taken into account for braking (slowing down) so they don't crash into each other.
© Game Development or respective owner