I am attempting to create a vehicle that automatically turns it’s wheels towards a target position. (using CylindricalConstraints and TargetAngle)
I managed to get it to work somewhat, but it appears that the following code only calculates the necessary angle relative to one axis.
local direction = CFrame.new(car.CarBody.Position, Position).lookVector
local angle = (math.deg(math.atan2(direction.x, direction.z) + math.pi)*-1)
How should I revise my code to make it calculate the necessary angle relative to the main body part in the car model? (car.CarBody)
I am having issues calculating the correct angle to rotate the wheels on a car in order to make them point towards a position. I am attempting to create a car that follows a set of points, and when it needs to turn I want it to simply rotate the wheels towards the next point (using CylindricalConstraints).
It seems like the following code perhaps only takes into account one axis (though I’m not 100% sure if that’s the issue)
local angle = math.atan2(car.CarBody.Position.X - Position.X, car.CarBody.Position.Z - Position.Z)
remote.Turn(math.deg(angle*-1))
remote.Turn() is a function that basically just sets the TargetAngle of the CylindricalConstraints to the number given.
Here is a video of the car not properly turning the wheels towards the next point (notice how it correctly turns towards the next point at first, but then it doesn’t correctly adjust the angle as the car itself turns):
You can apply kinematics in order to solve this problem.
Firstly, we will assume the car can be approximated with the kinematic bicycle model.
A car with steering angle 𝛿 will follow a circular path around some point. This point is called the instantaneous center of rotation, henceforth referred to as ‘ICR’. The ICR is the point where the perpendicular lines of the tires intersect. The distance of the ICR from the back tire, R, is dependent on 𝛿 (steering angle) and L (tire-tire distance).
R is related to 𝛿 and L via the equation R = L / sin(𝛿).
It is left as an exercise for the reader to calculate the ICR position given scalar distance R.
With this knowledge, you are able to formulate a relationship for the circle in which the vehicle travels given any steering angle 𝛿.
However, we want to calculate steering angle 𝛿 so our car passes through a desired point. Recall from lecture, a circle can be defined by 3 points. Given the position of the front wheel, back wheel, and desired point, we can calculate a desired circular path that we wish for the car to follow.
Given our previous relationship of R = L / sin(𝛿), we can solve for the steering angle 𝛿 which will cause our car to travel in a circular path which travels through a desired point.
Sorry to revive 4 years later, but I have this same exact problem and no other solution given to similar problems seem to fit my code, nor do I understand them. Can this be elaborated.