Hello, i am trying to learn CFrames and one thing is bugging me alot.
So i have been doing raycast suspension, and the guide that i am following uses this to prevent right vector movements.
local rotOnlyDirFrame = CFrame.lookAt(Vector3.zero, spring.CFrame.LookVector, spring.CFrame.UpVector)
local locvel = rotOnlyDirFrame:ToObjectSpace(CFrame.new(crawler.PrimaryPart:GetVelocityAtPosition(raycastResult.Position)))
Now, the person is creating a cframe at origin and replicating the rotation of the local CFrame of the chassis. And then he is getting the velocity of the spring in world position, and then transforming it relative to the local CFrame he created.
What i don’t understand is why did he just not use the chassis’ cframe for converting the velocity to local space.
basically, why didnt he do this
local locvel = chassis.CFrame:ToObjectSpace(CFrame.new(crawler.PrimaryPart:GetVelocityAtPosition(raycastResult.Position)))
One thing i managed to get is that, it is because by creating an axis at origin, he is preventing translation when going to local space. But why is preventing translation important? What happens if we don’t account for this?
It won’t be easy to answer your question because I don’t know what is going on in this code:
local rotOnlyDirFrame = CFrame.lookAt(Vector3.zero, spring.CFrame.LookVector, spring.CFrame.UpVector)
local locvel = rotOnlyDirFrame:ToObjectSpace(CFrame.new(crawler.PrimaryPart:GetVelocityAtPosition(raycastResult.Position)))
I understand what CFrame.lookAt() does, and what :ToObjectSpace does, but what exactly is spring.CFrame.LookVector, and what is the primary part of the crawler (where is it located relative to the crawler), and what is rayCastPosition.Position? Is it the position of where the thruster raycast hit the ground? Or is it something else?
You also said:
Could you provide us with the guide that you are using, so that way, some of us with more experience in raycast suspensions could make a guess as to why the guide put those two lines of code there?
You could experiment, by replacing those two lines of code with the code you provided below, and see what happens:
Hello,
sorry for not providing context
the video link:
he uses it at 2:50
primary part is a simple rectangular part, and there are parts called springs at each corner of primary part, and and they act as the origin of the ray.
Okay. So, here is what I think is going on. The first line of code is just creating a CFrame at the world origin (0, 0, 0), but setting the rotation of that cframe to the rotation of the car. This is because, CFrame.lookAt will create a cframe that will “look” at another vector, but since the first argument (the location of that CFrame, but not the rotation) is equal to Vector3.zero, that means that the CFrame will be located at the center of the world (the world origin, 0, 0, 0). But since there is carCFrame.LookVector in the second argument, that means the cframe will be pointing in the same direction as the look vector, which means that it will have the same rotation as the car. The third argument makes sure that the up vector of the CFrame that the lookAt function produces is the same as the up vector of the car. But, we can replace this line of code with local rotationsOnlyWheelDirCFrame = workspace.Car.CFrame - workspace.Car.Position. This will take the CFrame of the car (which contains position, and rotation), and subtract the position of the car from the CFrame, which will move the CFrame to the world origin (0, 0, 0), but since we are only subtracting the position of the car from the CFrame of the car, it will only change the position, and not the rotation, which means that the CFrame will still have the same rotation as the car. Next, we have the local LocalVelocity line. What this line is doing is getting the velocity of the car at the raycast’s position in world space. What this now means is that we have the velocity of the car, but relative to the world, and not the car. This means that if the car is sliding to the right, that doesn’t mean that it’s velocity will be equal to something along the lines of Vector3.new(rightVelocity, 0, 0), or if the car is sliding forward, or backward, it doesn’t mean that the velocity will be equal to something similar to Vector3.new(0, 0, forwardVelocity), because it’s relative to the world (I’m assuming that you understand that the X axis is right and left, while the Z axis is forward and backward). So what this is doing is it’s getting the velocity of the car in world space at the position of where the ray cast hit, relative to the RotationsOnlyWheelDirCFrame, so that way the X axis will always represent how much the car is moving along the X axis, and the Z axis will always represent how much the car is moving forward and backward, so that way it will become easier to perform necessary mathematical calculations on this data. Now, I don’t really understand why he is using CFrame.new(velocity) in the second line, instead of just velocity, but I haven’t watched the full video.
Edit:
The reason why, is because if the velocity is calculated like this:
local locvel = chassis.CFrame:ToObjectSpace(CFrame.new(crawler.PrimaryPart:GetVelocityAtPosition(raycastResult.Position)))
It won’t accurately calculate the velocity of the chassis, because the chassis is not at the world origin. The position of the chassis will affect how the velocity is calculated, but since it is using a cframe that is in the center of the world, it won’t affect the calculation.
that was a great explanation, and really helped me understand it well. However i wanted to know the reason, that if we dont consider position to be zero, then why does it affect the velocity that is calculated? is it because of the way the CFrame matrix is made? or is there some other reason?
If your velocity is equal to Vector3.new(10, 0, 0), and your CFrame is equal to CFrame.new(10, 0, 0), then the velocity relative to this CFrame is zero, but if the velocity is equal to Vector3.new(0, 0, 0), and your CFrame is equal to CFrame.new(0, 0, 0), then the velocity will be equal to 10, because the velocity is being measured from the center of the world. Here is a simple way to think about it. If you are facing south, and someone tells you to turn 90 degrees to the right, you will be facing west, but if I am facing north, and I turn 90 degrees to the right, I will be facing east. We both turned 90 degrees, but we started at different rotations. Or, here is another analogy. If you need to measure the distance between two different points, then you take a measuring tape, and put one end of the measuring tape on one point, and pull it until it goes to the second point, and read the measurement displayed by the measuring tape. But if you start measuring it one inch away from the first point, and you read the measuring tape, it won’t be accurate. The measurement could be up to one inch too long, or too short. Now, imagine if one of these points is the center of the world, another point is the velocity of the car, and another point is the position of the car. If you start measuring how far away the velocity point is away from the center of the world point, then you will get the real velocity of the car, and the distance will represent the velocity, and the direction will represent the direction of the velocity (is the car moving to the right of the world origin, or the left, or up, or down, or forward, or backward etc…), but if you measure the velocity by measuring the distance between the car point, and the velocity point, because the position of the car point is not the same as the position of the center of the world point, the distance won’t be the same. Another way to think about it is the distance between New York City, and Philadelphia is roughly 80.12 miles (not the actual measurement), but the distance between New York City and Newark New Jersey is roughly 9.10 miles (not the actual measurement). These are two different points, in multiple different positions. We are measuring from the same point (New York City), but we are measuring from that point to a different point, so the distance will be different. In that example, you can think that New York City is the velocity, Philadelphia is the world origin, and Newark New Jersey is the position of the vehicle.