# P2 Physics - Advanced accelerate to object

#1

Hey guys! I am working on a game where player’s car is chased by AI cars and for these I am using code from this example:

The problem is that I want to make advanced tweaking to the AI cars and I would appreciate to set the amount of turn force towards the player - the solution from the example makes car turn so fast and I want them to make their turn curves bigger.

I know I have to basically constrain the angle change between frames and I did this so far:

``````var angle = Math.atan2(obj2.y - obj1.y, obj2.x - obj1.x);
var diff = angle - this._lastAngle;
angle = this._lastAngle + (diff * 0.1); //reduce the amount of the angle 10 times
this._lastAngle = angle;//save current angle as an object property for next update frame

obj1.body.force.x = Math.cos(angle) * speed;    // accelerateToObject
obj1.body.force.y = Math.sin(angle) * speed;
``````

The problem is the above doesnt works the best - when the change between the angles passes the point where the sign is changed (Math.atan2 returns the values from 0 to PI and then from 0 to -PI), the car gets crazy, because the diff is to big (for example -6) and it doesnt make sense to add it to the angle anymore. For example, the situation is like this:
FRAME 1 angle: -3.111
FRAME 2 angle: 3.13
diff: -6.25…//should be like 0.03 in reality

Please, do someone know how to deal with these sign changes in order to make it work properly? Thanks in advance!

#2

If i was working on such problem i should draw a circle on a piece of paper around the object working with degrees unit and have a calculator open to convert angles from deg to rad