Jump away from surface (python)

Hi there blenderers!

I’m making a sonic game, and I want him to jump away from the surface he is standing on. The problem is I’m taking reference of The Sonic Physics Guide, from Sonic Retro, but this only applies to 2d games.

In 2d games, the formula to do it is something like this:

xspeed = jumpspeedsin(ground_angle)
yspeed = jumpspeed
cos(ground_angle)

But in 3d… well I’m kinda stuck, I guess I could get the angle of the ground with hitNormal somehow, but have no idea on how the formula could be, because now i have two angles and three axis.

Please help me.

Ray, player -Z local,

get sens.hitNormal

apply force -sens.hitNormal*Jump force

however, your sonic will need to align to the normal anyway, making his own +Z Face away from the normal anyway

Thank you, i’m testing it out right now.

I tried this, and i got some weird error in the console

own = cont.owner
if space.positive:
own.applyForce(ray.hitNormal*jump, False)

AttributeError: error setting vector, 39 args , should be 3

import bge
from mathutils import Vector


def main():


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


    St = Vector ([0,0,-1])
    Start = own.worldPosition+(own.worldOrientation * St)
    St = Vector ([0,0,-2])
    End= own.worldPosition+(own.worldOrientation * St)


    hitObject, hitPoint, hitNormal = own.rayCast(End,Start, .0, "ground")
    # check the orientation of the wall the player is hanging from and then use it for our own orientation
    if hitNormal != None:
        own.alignAxisToVect(hitNormal, 2, .8)


main()

Attachments

Sonic.blend (444 KB)

It works, but it isn’t smooth, at least in the way i set it up. Is there a way not to use force, but instead set the linear Velocity along the vector for a split second, and then let the gravity do his job? :smiley:

btw i’m using a motion actuator and artificial gravity.

change the .8 to .25 or so in

own.alignAxisToVect(hitNormal, 2, .8)

the .8

is 0 not rotate at all to 1 rotate instantly.

check this out

space is jump

I use a property as a timer, in logic

Attachments

SonicJump.blend (442 KB)

But i don’t want to affect his rotation, otherwise if he jumped from a ceiling, he would draw an “U” in the air because while he was rotating back to the ground, the direction of the force was rotating as well. I mean, I just want to move sonic along the hitNormal by using world coordinates.

The jump force is only applied for a instant,
So when jumping while upsidedown, you just shoot down, and the flip over,

I should add a else and timer to flip back to facing world up if off the surface for two long,

you can apply a force forward, during the jump local to sonic, if moving over a certain

own.localLinearVelocity.y