Collision disabled if object is animated

#1

Hi there,
I’m pretty new in armory and I have some struggles with collision detection:
Shortly: When I set an object to animated (because I wrote a script for movement) in the physics tab, then all collisions seem to be disabled (I can walk through walls ect.). When I don’t the collisions are enabled, but I can’t move. :frowning:
I think that this problem is easy to fix, but i am too stupid, to find out how XD
I hope that you can help me to solve that problem
Thanks

0 Likes

#2

What are you using in your script to move the object? Usually object.transform.loc.add(vec) should work with active rigidbodies just fine.

0 Likes

#3

Mhhh, okay, you can compare my code with the bundle-script SimpleMoveObject.hx, which is pretty similar to mine(my inspiration). As a result I used the same tools :smiley: But I will try your possible solution. First of all thanks for your reply :slight_smile:

0 Likes

#4

by the way, I want to use scipts more than logic nodes, because I want to improve my haxe skills :slight_smile:

0 Likes

#5

Can you maybe give me an example?

0 Likes

#6

…because i am still running through walls

0 Likes

#7

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.

6 Likes

#8

As they said – “either you are setting the position of the object, or physics is. Pick one or the other.” If you set the position, physics won’t look for collisions or anything else. So, set the location and movement when you place the object on the field, and then (only) anytime thereafter that you want to “play God.” Let the physics engine drive the object at all other times, and you should then get collision events as expected.

0 Likes