Can i dynamicly move a TriMesh?

From Newton Wiki
Jump to: navigation, search

Sometimes you may wish to move portions of static game world for creating elevators, trains and other special animations.

If you want to move an immobile object (mass = 0), then you need to:

  • set it's velocity using NewtonBodySetVelocity()
  • every frame, manually use NewtonBodySetMatrix() to move the body's position according to its velocity
  • every frame, wake up all objects within this body's AABB using NewtonWorldForEachBodyInAABBDo() or NewtonWorldGetFirst/NextBody() and NewtonBodySetSleepState()
game loop
    if body has 0 mass and NewtonBodyGetVelocity(body) != vec3(0, 0, 0)
        mat4 position = NewtonBodyGetMatrix(body)
        position.translate(NewtonBodyGetVelocity(body) * deltaTime)
        NewtonBodySetMatrix(body, position)

        vec3 min, max = NewtonCollisionCalculateAABB(body.collision, position)
        for each body in AABB(min, max) do // can be done via NewtonWorldForEachBodyInAABBDo(), for example
            NewtonBodySetSleepState(body, 0)

Now dynamic objects will react properly to any immobile mesh that is 'manually' moving (even if it's a tree collision). You can also use this technique to set the position of a body (not via velocity, forces or joints) and make other bodies react properly - simply move it as such, and set the velocity to the vector (newPosition - oldPosition). Then perform the rest of the operations above.

If it's not an immobile object, simply use NewtonBodySetVelocity() or NewtonBodyAddImpulse() to give the body a velocity.

This tip is for Newton 2.0

Back to the FAQ

Source tip by agi_shi on forums