Need help with coding wheel deceleration


I am stuck with some coding. I know the problem but I don’t know how to solve it.
My wheel is turning according to the velocity.
Now I need the wheel to slowly stop spinning when not in contact with the ground.
The Ray sensor checks if there is contact with the ground.
The rotation when in contact works fine but I don’t know how to save the last value when getting off the ground and loading it into the “else” loop.

Here is the code:

import GameLogic
import math
board = own.scene.objects["Board"]
boardVel = board["velocity"]
rayEmpty= own.scene.objects["WheelRay"]
ray = rayEmpty.sensors["Ray"]
vel = boardVel

if ray.positive:
    velWheel = (vel*0.1)*-1
    rotationVel = [velWheel, 0.0, 0.0]
    own.applyRotation(rotationVel, 1)
    own["prop"] = velWheel
    prop = own["prop"]
    velWheel = prop + 0.1
    rotationVel = [velWheel, 0.0, 0.0]
    own.applyRotation(rotationVel, 1)

I tried to solve it before with the game logic states with almost good results (the property was already decreasing while not in contact with the ground)
But I couldn’t load the property again into the code.
The code above is without game logic states, but I don’t know how to save the last value of the velWheel variable after exiting the positive if loop.

Any ideas?

Thanks for any help!

for starters:

import bge

is right.

import GameLogic

is wrong.

second, its better to do:

own.applyRotation((velWheel, 0, 0), True)

simply because its shorter. and using 1 for True isnt very readable.

and finally, to slow your wheel:

if ray.positive:
    velWheel = vel*-0.1
    own["prop"] = velWheel
    if own["prop"] < 0.001:  #assuming your property is positive
        own["prop"] -= own["prop"]*0.3  #how fast to slow the rotation (-30% per frame)
        velWheel = own["prop"]
        own["prop"] = 0
        velWheel = 0

own.applyRotation((velWheel, 0, 0), True)

Wow, thanks a lot… I’ve been fiddling all night to get it right and you just did it in 5 min.

Again, thank you!

GameLogic is not wrong. It is the original name of the game Logic API module.

Since Blender 2.50 the offical name is bge.logic. It should be used when possible. The original name was kept for compatibility reasons.

my apologies, i meant to use the word “proper”. :slight_smile:

a-k-m, im glad it worked! i cant seem to escape typos :smiley:

_Desculpem a intromissão, mas estou tentando aprender, programação python, gostaria de saber se as duas formas estão corretas ou devo usar apenas uma delas e qual ?