0
$\begingroup$

I am working on a skateboarding game and I need to detect when an object is moving backwards so the character can change stance and the controls will swap.
For example after doing a 180, or when coming down after going up a ramp (and not doing a 180). Ideally this would be a boolean, with an adjustable tolerance. I have been through every object I can think of and I can't figure out how to do this and it's had me banging my head against the wall for weeks. I'm guessing there is an old frame and new frame value to operate on to tell whether the player is moving in the negative y direction. Fairly new to blender and python, terrible at math. Any help is greatly appreciated.

$\endgroup$
  • $\begingroup$ UVed (up voted) so you don't get a headache. :) $\endgroup$ – David Jan 25 '16 at 4:27
  • 2
    $\begingroup$ Does getVelocity() provide the info you want? There are several other velocity properties - localLinearVelocity worldLinearVelocity.... $\endgroup$ – sambler Jan 25 '16 at 8:10
  • $\begingroup$ I'm kicking myself. I'm pretty sure localLinearVelocity is all I need, don't know how I missed it. $\endgroup$ – billyzill Jan 26 '16 at 3:08
1
$\begingroup$

getLinearVelocity() provides you the motion vector you object was travelling since the last frame to reach the position it is now.

The motion vector is the vector from the last position (L) to the current position (C).

With the scalar or dot product you can determine if the previous position was in-front or behind the current position. You can even determine if it was left/right or over/under the object.

Before you can calculate anything you need to define what is front and what is behind. You need an Front/Behind orientation vector. Lets assume the border is the local XZ-plane.

  • anything that is at the positive Y-axis is front
  • anything along the negative Y-axis is back.
  • anything on the XZ-plane is neither back nor front (it is left/right/over/under)

For correct calculation all vectors needs to be in the same space. I suggest to use the scene space (world) You convert the orientation vector to scene space (relative to the objects origin):

from mathutils import Vector

FORWARD_ORIENTATION = Vector([0, 1, 0])
forwardOrientationVector = object.worldOrientation * FORWARD_ORIENTATION 

The dot product projects one vector on the other and tells how much of the vector the projection uses. The calculation is pretty simple, but we still use the dot operation mathutils.Vector provides:

forwardRatio = forwardOrientationVector.dot(object.getLinearVelocity())

You check the range of the result, to get one of the three possible values:

if forwardRatio > 0.0:
    print("forward")
elif forwardRatio < 0.0:
    print("backward")
else:
    print("neither forward nor backward")

Similar calculations can be used to determine left/right (RIGHT_ORIENTATION = Vector([1,0,0])) and over/under (OVER_ORIENTATION = Vector([0,0,1])).

I hope it helps

$\endgroup$
-3
$\begingroup$

localLinearVelocity it is.
All I need to do is check whether y is positive or negative. So simple.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.