What's Wrong with my Code? character Movement Acceleration over time.

Hello, im trying to get some very subtle speed acceleration to a character walk speed to help add weight to the game feel.
from what i can tell this is due to “cont.activate(motion)” not updating as velocity rises.
but for the life of me, i can’t figure out why… any ideas?

Code in question and Blend below.

POC 2.blend (608 KB)

import bge
from bge import logic


###-Define Stats-########################################


walk_acl = 0.01
walk_max = 0.08
run_spd = 0.125
jump_speed = 2
stamina = 25
sanity = 100


###-Define some varriables-################################


cont = logic.getCurrentController()
own = cont.owner
grounded = own["IsGrounded"]
keyboard = bge.logic.keyboard
activated = bge.logic.KX_INPUT_JUST_ACTIVATED
deactivated = bge.logic.KX_INPUT_JUST_RELEASED
hold = bge.logic.KX_INPUT_ACTIVE
noInput = bge.logic.KX_INPUT_NONE


###-Define Sensors-##########################################


forward = cont.sensors["Forward"]
back = cont.sensors["Back"]
left = cont.sensors["Left"]
right = cont.sensors["Right"]
jump = cont.sensors["Jump"]
ground = cont.sensors["Ground"]
#delay = cont.sensors["Delay"]
always = cont.sensors["Always"]


###-Define Actuators-#########################################


motion = cont.actuators["motion"]
idelF = cont.actuators["idelfalse"]
idelT = cont.actuators["ideltrue"]
trackto = cont.actuators["TrackTo"]


###-Define Game Properties-####################################


idel = own["IsIdel"]
velocity = own["velocity"]


#checks if player is moveing and assigns it as a boolean
    
if right.positive or left.positive or back.positive or forward.positive:
    #Sets idel to False
    cont.activate(idelF)
    #Reorients Charecters movement releative to camera
    cont.activate(trackto)
    #increaces Velocity until max is reached
    if velocity < walk_max:
        own["velocity"] += walk_acl
else:
    #Sets Idel to True
    cont.activate(idelT)
    cont.deactivate(trackto)
    #Lowers Velocity when not moveing
    if velocity > 0.01:
        own["velocity"] -= walk_acl
        cont.activate(motion)
        #sets Velocity to 0 if its lower then 0
    if velocity < 0.0:
        own["velocity"] = 0.01


###-Execute Controls-############################################


###-Movement-### 
    
if forward.positive and velocity < walk_max:
    print(velocity)
    motion.dLoc = ([0,(own["velocity"]),0])
    cont.activate(motion)


if back.positive and velocity < walk_max and grounded == True:
    motion.dLoc = ([0,-(own["velocity"]),0])
    cont.activate(motion)
    
if left.positive and velocity < walk_max and grounded == True:
    motion.dLoc=([-(own["velocity"]),0,0])
    cont.activate(motion)
    
if right.positive and velocity < walk_max and grounded == True:
    motion.dLoc=([(own["velocity"]),0,0])
    cont.activate(motion)
    
else:
    #Deactivates motion when Velocity is under 0.01
    if velocity < 0.011:
        cont.deactivate(motion)

I’m not sure if your approach is what you are looking for.

Basically you are calculating a velocity strength. That is fine
Then you calculate the step to perform. That can be one of four directions. But you do not consider that the velocity has a direction too. That means when the character steps forward and then backward it makes not much sense to do the same or even a wider step than before (as this was a different direction).

Wouldn’t it make more sense to decelerate first or at least start the new direction with a small step?

This is what happens with physics. You accelerate with a force resulting in a velocity (direction + speed). When you change direction you need a counter force (direction + speed again) to cancel out the current motion and get the desired motion. This can happen once or other time.

To clarify this. What are you aiming for?
A) Do you want to let the user request a direction and the character will smoothly change the current motion to the desired direction over time? This means after a given time the character moves into the requested direction only. (When it travels forward and the user requests left it travels left after x frames) = character behavior

or
B)do you want to “add” the requested direction to the current motion? This means after a certain time the character moves into the direction of combined directions. (When it travels forward and the user requests left it travels forward and left after x frames) = space ship behavior

yes, im wanting to work towards something like your A example. but while figuring things out I hit a snag here with this acceleration. if you think I’m going in the wrong direction I’m more than open to suggestions.

though I don’t want the character to move rigidly in 4 directions like it does now. I was going to add 4 more arguments for an up+right and right+back after I figured this acceleration out. to hopefully help give it more of a full range of motion… do you have a better suggestion by any chance?

Thanks for the help btw.

Hello, I’ve been messing around a bit with the character control.
i Got it pretty close to how i want it to behave. however, the character falls extremely slow… which from what I’ve read is due to the linear Velocity.
any ideas?
or any suggestions to make character movement better.

btw thanks for all the help you do here monster.
I see you a lot on here and I admire your dedication and passion for helping others.

testGrounds.blend (551 KB)

maybe change the mass of the character, this is my guess, but I just picked up BGE yesterday :slight_smile:

EDIT: Nope, was not the mass…maybe someone useful will come along. Sorry.

yeah, it wouldn’t be mass… mass only affects forces… in this case, it would just make the character move slower.

i see where you’re going but objects of different mass fall at the exact same rate. any discrepantly such as a bowling ball vs a sheet of paper is due to drag. in a vacuum all object fall at the same rate.

most likely i will have to apply a constant force along the z axis to make it fall faster.

thanks anyway XD

well I was assuming over a short distance, it WOULD effect the rate at which it hit terminal velocity…so a higher mass would make it react more slowly to the force…assuming the engine is not taking drag etc into account…but still…sorry it did not help.

np XD adding a velocity along its z axis when not grounded worked like a charm.