I have some issues in understanding the use of the bullet package. My problem is that I cannot understand how to apply a specific force to an object.
For clarity, we could refer to the following piece of code appearing in the Panda3d Manual pages.
import direct.directbase.DirectStart
from panda3d.core import Vec3
from panda3d.bullet import BulletWorld
from panda3d.bullet import BulletPlaneShape
from panda3d.bullet import BulletRigidBodyNode
from panda3d.bullet import BulletBoxShape
base.cam.setPos(0, -10, 0)
base.cam.lookAt(0, 0, 0)
# World
world = BulletWorld()
world.setGravity(Vec3(0, 0, -9.81))
# Plane
shape = BulletPlaneShape(Vec3(0, 0, 1), 1)
node = BulletRigidBodyNode('Ground')
node.addShape(shape)
np = render.attachNewNode(node)
np.setPos(0, 0, -2)
world.attachRigidBody(node)
# Box
shape = BulletBoxShape(Vec3(0.5, 0.5, 0.5))
node = BulletRigidBodyNode('Box')
node.setMass(1.0)
node.addShape(shape)
np = render.attachNewNode(node)
np.setPos(0, 0, 2)
world.attachRigidBody(node)
model = loader.loadModel('models/box.egg')
model.flattenLight()
model.reparentTo(np)
# Update
def update(task):
dt = globalClock.getDt()
world.doPhysics(dt)
return task.cont
taskMgr.add(update, 'update')
run()
Now the question is: suppose I want to apply a specific force to the box, maybe a force that depends on its position and time.
-
I should place the calculations of the force in the update() function, correct? Hence modify the function to be able to access the specific node I want the force applied to, since I will have to change the force magnitude during execution. Right? Or is there maybe a method that provides a callback function?
-
How do I apply the force? I saw that there is a “applyForce()” method for the BulletRigidBodyNode class. However, this function requires 2 arguments: a vector for the force components, and a position, and both parameter must be specified. I do not understand what does this position refer to, since what I want to do is to apply a force to the node, not in a specific position.
-
Suppose the above issue is solved, the “doPhysics()” call should update the position of the node, right?
That is, if I use getPos() on the node pointer to the node, I will get the new position, right?
Thanks in advance
Ode