Thread Rating:
  • 5 Vote(s) - 4.2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Ways of determining vehicle velocity: an exploration
Ways of determining vehicle velocity

For those interested in programming vehicles on roblox, the topic of knowing how fast a vehicle is traveling at has probably come up at some point or another. The knowledge of a vehicles speed can be put to use in telling the user how fast the vehicle is going, but it can also do many other things, including determining fuel usage, whether or not a user is cheating, or if a vehicle needs to be destroyed because it is bouncing around the map. There are a number of ways to determine a vehicle's velocity in roblox, and the best method to determine velocity depends on what system needs the information.

This thread covers four ways of determining vehicle velocity, with the goal that vehicle programmers can use the information to make vehicles on roblox better than ever.

The classic way to determine speed is a simple magnitude check. It's easy, simple, and can be applied in many, many situations.

speed = part.Velocity.magnitude

For a general speed gauge, it certainly works well. It doesn't matter what the propulsion method is, whether it be wheels or propellers or thrusters, and it doesn't matter what the orientation of the person controlling the vehicle, it tells the user how fast a vehicle is traveling.

Computers aren't quite as clever as humans, and there are some situations that may have unusual outcomes using this method. Motor sounds, for example, will act like the car is speeding up when the car is falling off the world, or will rev up if someone hits the car from behind or the side. The check is also relative to the world, which means that if the car is on top of something that is moving, the car will be treated as if it is moving even though it may not be, or be treated as staying still even though it isn't.

The value that the magnitude check gives is always positive, which means you can use it in sound pitch calculations and speedometer calculations with no alterations.

Now, what can we do with magnitude to get different effects? Why not multiply the velocity vector to only select a few axis?

Overland Magnitude
speed = (part.Velocity*,0,1)).magnitude

The overland magnitude tells you only your speed over land. It does this by totally removing the part of your velocity that is vertical. With vertical speed, your speed will be reported as higher (but only if you're traveling up or down quickly) than your overland speed, and sometimes it's handy to know only how fast you are going over land. Regardless, the way to do that is to multiply component-wise so that the vertical component is zero.

If your speed is being reported by a GPS speedometer, this is the kind of information it would get. Because they are magnitude checks, the values are always positive, and relative to the world, so moving surfaces are not correctly handled.

A more advanced way to determine a vehicle's velocity is a dot product. It gives the speed in the direction the car is facing, including if it is going backwards.

Dot product
velocity = part.CFrame.lookVector:Dot(part.Velocity)

The dot product is a way of determining how close two vectors are, and if they are facing away from each other. In the case of a vehicle, it will tell you how fast the vehicle is going relative to some direction on the car, like the way the driver seat faces. Because dot products tell you if two vectors are facing away from each other, you can tell if a car is moving backwards when you use them. If two vectors are facing away, the value is negative instead of positive. For the code snippet, the vehicle going in the Front direction will yield a positive velocity.

Like with the magnitude check, there are some strange effects that can happen if the car gets into an unusual situation: if the car gets pushed from the front or behind, the motor sound will rev up even if the car is idling. Being pushed from the side on the other hand behaves how it should: the motor sound will not rev.

Because the value from the dot product is signed (which means it can be negative), you will need to use an absolute value before using it in sound or speedometer displays. Jalopy is a game that doesn't do this, and you can hear the motor sound playing in reverse when the car backs up. Quite unusual.

What can we do to extend the dot product, though? Determining how fast a wheel is rotating is one way.

Wheel rotation
WheelRadius = part.Size.X/2
velocity = part.CFrame.lookVector:Dot(part.RotVelocity)*WheelRadius

A unit vector dotted with a rotation velocity will tell you the rotation velocity along that axis, which can be used for determining the velocity of car wheels by choosing the wheel's rotation axis as the unit vector. Knowing how fast the wheels are turning is helpful, because, among other things, you can tell if the car's wheels are slipping. If the wheel's velocity isn't the same as the velocity in the dot product, you can easily tell the car is doing a burnout or the car is skidding to a stop. Like the simple dot product before, it returns a signed value, so you can tell if the wheel is rotating forwards and backwards.

The wheel rotation code has a few more steps to set up than the dot velocity code: you need to know more information to get the right information out of the equation. First, you need to know how large the wheel is so that you can get a velocity out of it. Second, you need to know which axis the wheel rotates on. The code snippet above is for wheels that rotate on the Front or Back sides. If your wheel rotates on the top or bottom, or left or right sides, use CFrame.upVector or CFrame.rightVector instead of CFrame.lookVector. Finally, once you know both of those, you'll need to determine if the wheel rotates in the direction you expect. If it doesn't, you'll need to add a negative sign to the equation, but if you don't care you can just use an absolute value.

Using the wheel rotation to determine car velocity has a number of advantages in certain situations. As mentioned before, you can tell if the car is skidding to a halt or doing a burnout, but also the motor sound will react properly in situations where the car is sailing through the air or drifting as well as stopping and starting quickly, and the velocity will not be skewed if the vehicle is on a moving surface


I wrote this thread with the hopes that your cars can be improved to their fullest potential. Not all methods of finding velocity are for every application, so use them wisely! Happy coding!
[Image: acl6rng.png]
Did you code Portago's chassis?

Southern Ontarian roadgeek.
Je parle le français.
"Smart people know whether he/she is smart or not"-Me

Signature edited in accordance to forum rules. -M
(09-21-2016, 06:14 PM)andrepoiy Wrote: Did you code Portago's chassis?

No, I made the TRC chassis which Portago then took as his own. A lot of the vehicles he makes are barely modified from the originals.
[Image: acl6rng.png]

Users browsing this thread: 1 Guest(s)