How to calculate the speed of cars and apply it into GUI?

I was wondering how do people calculate speed of cars and show it on the GUI. I have basic understanding about GUI but how do they make the speedometer that actually rotate on the display?. In addtion, i know something about the speed bar of roblox that is default of roblox vehicle seat( the horizontal bar that shows speed everytime we drive on a vehicle seat) . According to my understanding, the bar shows the speed of the car in studs/s but how do we access this data type? Therefore, calculating speed of an object or people use a different method to achieve this?

2 Likes

It depends on how the power is decided.

If power of the car is based on studs per seconds, there you go.

You could also just make your own values with a bit of math it doesnt have to be accurate, aslong as it feels about right

If you get the PrimaryParts position then wait about 0.01 seconds then get the distance between the two that will give you studs per 0.01 seconds to which you can multiply that by 100 to get the studs per seconds

what do you mean power of the car? Is it related to the default speed bar of vehicle seat?

Measurement perhaps?
character limit

If speed of the car is say, 50 and the script of the vehichle uses the power to determine SPS ( Studs per seconds ) then you have your speed.

As above said you could also check the distance last moved but it would be delayed in changing

Yes but the less you wait and the more you multiply then the smoother it is

ok, that is an interesting method to calculate speed

Yeah but its not based off how much power is coming out the car its more based on the distance you moved if it is that way.

the thing you mentioned aka power of the car, could you describe it more specifically?

Based on the car script, you could make the script move the car at a certain studs per seconds, this could be the Power value

mine used cylindrical constraint to move so how do i achieve this"make the script move the car at a certain studs per seconds"?

The default vehicle GUI uses the magnitude of the velocity to get the speed.

local speed = vehicleSeat.Velocity.Magnitude

The only flaw to this is when the car falls off the map the speed will increase crazily (you can try it yourself)

To get the same result but without this flaw you can calculate the average of the cars linear rotational velocity instead.

Here is the function which I used for my chassis.

local function getAngularVelocity(wheel)
	return wheel.RotVelocity:Dot(wheel.CFrame.rightVector)
end

local function measureWheelVelocity()
	local wheelRadius = wheelParts[1].Size.Y / 2
	local averageAngularVelocity = 0
	for i, v : BasePart in pairs(wheelParts) do--wheel parts is an array of wheel parts
		averageAngularVelocity += getAngularVelocity(v)
	end
	averageAngularVelocity = averageAngularVelocity / #wheelParts
	local wheelVelocity = averageAngularVelocity * wheelRadius
	return wheelVelocity
end

8 Likes

can you explain to me what this do: Dot(wheel.CFrame.rightVector). You know, the dot function because i’ve been searching for the use of this function for 2 hours and it got no result

The purpose of the dot product in this scenario is to do a scalar projection.

Why are we trying to do a scalar projection?

Ok do you know that we can break up a vector into components like so?

image

We can apply the same concept with angular velocity instead to find the wheels rotational velocity in a certain axis. (Using scalar projection because it’s 3d with vectors)

This axis is assumed to be the wheels right vector in which the wheel will rotate from there.

You can have a value that’s called speed and when you move it moves at the value called speed. From there you make a gui and tween is to the value / max speed.

1 Like

oh , so it is not a function of roblox