objects doing funny things with gravity

I’m trying to get my movement script working, and stumbled on something interesting: With the script as I have it now, linear velocity is set explicitly to move my character in whichever direction the corresponding key tells it to… Which has the interesting effect of cancelling gravity and inertia! If you move in a circle you can pretty much float. It’s neat, but how do I fix it?

Here’s the code:

import bge

ctrlr = bge.logic.getCurrentController()
obj = ctrlr.owner
snsr = ctrlr.sensors["Keyboard"]
move = ctrlr.actuators["move"]
speed = 0.2

def movement (ctrlr):

    if snsr.positive:
    #   If keys are pressed, figure which and move character accordingly.
        for key,status in snsr.inputs:
            if status is bge.logic.KX_INPUT_JUST_ACTIVATED or bge.logic.KX_INPUT_ACTIVE:
                # ESDF FTW
                if key is bge.events.EKEY:
                    move.linV = tuple (sum (x) for x in zip (move.linV, (0, 1*speed, 0)))
                if key is bge.events.SKEY:
                    move.linV = tuple (sum (x) for x in zip (move.linV, (-0.6*speed, 0, 0)))
                if key is bge.events.DKEY:
                    move.linV = tuple (sum (x) for x in zip (move.linV, (0, -0.8*speed, 0)))
                if key is bge.events.FKEY:
                    move.linV = tuple (sum (x) for x in zip (move.linV, (0.6*speed, 0, 0)))
    else:
        move.linV = (0, 0, 0)

#   Thought that if I added the linear velocity to the movement velocity I'd get the thing to respect physics.
#   Instead I got a positive-feedback loop. What do?    
#    vel = obj.getLinearVelocity (0)
#    move.linV = tuple (sum (x) for x in zip (move.linV, vel))
    ctrlr.activate(move)

And here is the blend file if you wanna take a look.
resources.blend (6.58 MB)

If you’re in python you can just alter the velocity directly:


import bge

cont = bge.logic.getCurrentController()
own = cont.owner

if bge.logic.keyboard.events[bge.events.WKEY]:
    own.localLinearVelocity.x = speed
if bge.logic.keyboard.events[bge.events.SKEY]:
    own.localLinearVelocity.x = -0.8*speed
....

Otherwise, the servo motion actuator probably does what you want without changing your code above. Just set the force limits on the z axis to zero.

Thanks! Such a simple solution! I knew I was close…

How do I mark this thread as solved?

Go edit mode, advanced edit, and up by the thread title.