Speedometer Problem

Here is the script I have written for my speedometer for my car. It works fine, but I have one problem. I am pretty sure getVelocity returns the global velocity and not the local velocity. At least thats what I think because the script works fine unless I turn the car around. Then the speed is in the negatives.

car = GameLogic.getCurrentController().getSensor("body").getOwner()
velocity = car.getVelocity()[1]
setspeedprop = GameLogic.getCurrentController().getActuator("setspeedprop")
speed = round(velocity*(-2.24), 0)

Is that what is happening? If it is, then how do you set it to get the local velocity? Sorry, I’ve never really worked with velocity in Python before.

And if you are wondering what the -2.24 is for…
I have the car scaled so that 1 Blender Unit = 1 meter. Since it returns the velocity in blender units per second, you multiply it by 2.24 (meters per second times 2.24 = MPH)

I though for velocity it was setup like this: getVelocity([X, Y, Z, Local])

Try that.

EDIT:: put a 1 where it says local to set it to true at 0 for false.

if you are using 2.46 or above you can:

speed = car.getLinearVelocity(1)[1]

that will get the local y speed

thanks cyborg_ar! Now it works!

Except for this problem…
The speedometer needle goes retarded after you hit 80 MPH. I have no Idea why. Here’s the blend if anyone can figure it out.
Controls - arrow keys

PS - the steering is kinda bad cuz its not done right yet


Ferrari_F40.blend (139 KB)

What I did to create a speedometer is this:

create a speedometer needle animation that starts at 1 and ends at 100 (or whatever your max mph is) now add a prop that’s called speed. Now create an actuator that’s IPO > Property: speed

Now add your python script, and add own.speed = car.getVelocity(1)[ 1 ]

Now it should work. Also make sure to set your motion so it has to be under your max to get faster like

(this is a concept that can either be python or logic bricks)

for car to move forward - speed < maxspeed - move forward.

I hope this helped!

if you can go backwards i’d suggest to use

car.speed = abs(car.getVelocity(1)[ 1 ])

so it will be always positive

Killer, that is exactly what I did. The max MPH is 220. I gave the needle an Ipo starting at 1 ending at 221. I used a property called speed to control the Ipo. And for some strange reason, the needle flickers around once you get to 85 MPH. Check the blend. I seriously have no idea what’s wrong.

EDIT: Oh and thanks cyborg_ar. That’s an easy way to get the MPH for reverse. Thanks for telling me that, otherwise I would have done it a different way

It worked fine for me, just checked the blend.

thats wierd. whenever the car goes over 80MPH for me, the needle jumps back and forth however much ahead of 85 it is. So if ur going 90, It will jump between 80 and 90. If ur going 100, it will jump between 70 and 100. Its really pissing me off. I really don’t know why its happening

EDIT: nvm. I fixed it. I just had to redo the Ipo. All that was wrong was that it didn’t have X or Z rotations (idk why that would screw it up) but whatever