Hey @Johannes_Wellerdiek, welcome to the forum!
So, first of all, when you enable the “animated” checkbox in the physics panel, it will disable all collision response or any kind of physics behavior entirely. I’m pretty sure this is by design, so that you can programmatically enable and disable the physics response. I’m not sure if there is any other purpose for it, but the gist is, if you want to have it act like physics object at all, leave it unchecked.
Second, there are two different ways you could go about moving your object: you can set the objects’s velocity, or you can set the object’s location.
Set Velocity
Setting the velocity of the object, is the more physics integrated solution, meaning that the physics simulation will behave more realistically when setting the velocity of the object than it will when setting the location.
When you set the velocity of the object, it sets the speed that the object is moving in a direction in the physics simulation. If the object runs into something during its movement, the object will collide ( relatively ) realistically with the object it runs into and transfer momentum to the object it collided with as you would probably expect, but you have to take into account that the velocity that you are setting will override any velocity that may be set by the physics engine for that object. If you set the velocity of the object to Vec4(0, 1, 0)
on update, then the object’s will move at a speed of 1 along the Y axis with zero velocity along the Z and X axes. Even if gravity is enabled, because you set the Z velocity to zero the object will not fall.
For objects like bullets/cannons/etc., it makes enough sense to set the velocity once, when the bullet is fired and let the physics engine take care of the velocity from then on so that the movement is realistic and you don’t have to calculate it every frame.
Here is how you can set velocity in Haxe:
var body:RigidBody = object.getTrait(RigidBody);
body.setLinearVelocity(0.0, 1.0, 0.0);
Set Location
You can also choose to move the object by setting its location like @Simonrazer mentioned. When setting the location of the object, though, the physics engine cannot tell certain things like how fast the object is moving because you are actually “teleporting” the object to the exact point of space that you want it in an instant. This can cause strange reactions when the object collides with other objects that are not passive. When setting the location in small increments, the physics engine should still be able to keep your object from going through stuff, though.
When setting the location, rotation, or scale of an object directly, though, you always have to remember to call object.transform.buildMatrix()
afterwards to update the objects transform:
object.transform.loc.add(new Vec4(0, 1, 0));
object.transform.buildMatrix();
If you have multiple changes to make to loc
, rot
, or scale
only call buildMatrix()
after you have made all of them ( just for efficiency ).
Full Example
I’ve got a full example of a relatively effective character controller that might be useful for some reference. There are a lot of problems that you can run into when making a decent character controller and it to hours upon hours of trial and error for me to get it right.