1
$\begingroup$

Is there a way in BGE to get the force vector applied to the object? I need something like worldLinearVelocity, but it should show the force vector instead.

$\endgroup$
1
$\begingroup$

Although it appears blender has not implemented access to an object's force, we can calculate the force using object's the mass and its change in velocity.

This equation for force relates force, time, and change in momentum. Because momentum is just mass times velocity, we can calculate the force. We can compute the change in time and the change in velocity between frames by recording initial time and velocity from the previous frame to calculate the difference with the current frame time and velocity.

Add the following script as a Module on a logic brick. It needs to be a module to save the previous time and velocity variables.

import bge, time
from mathutils import Vector

controller = bge.logic.getCurrentController()
obj = controller.owner

t = time.time()
v = Vector(obj.worldLinearVelocity)
m = obj.mass

def main():
    global t, v, m

    #compute change in time
    dt = time.time() - t
    t += dt

    #calculate change in velocity
    dv = Vector(obj.worldLinearVelocity) - v
    v += dv

    #force is change in momentum, which is velocity times mass, divided by time
    f = m * dv / dt
    print(f)

Before the monkey hits the ground, we can see the force on the z axis is around -9.2 which is the gravity of the scene and the force acting upon it.

Animated gif

$\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.