[Solved] Locking rotation of the X and Y

How to prevent the RigidBody to rotate along the axes X and Y, allow only on axis Z?

My code (In it, the body sometimes turns over, and this must be prevented):

var dir = object.transform.look().normalize();
var vel = new Vec4(
    dir.x * forward_speed,
    dir.y * forward_speed,
    dir.z * forward_speed);
body.setLinearVelocity(vel.x, vel.y, vel.z);		
body.setAngularVelocity(0,0,rotation_speed);

How do your body turn over? by hitting object? or it turn over out of now here?

From hitting or stumbling over the floor, because of which it can fly up (this should be prevented).

I need the body to move only on a floor and not flying off during collisions. That is, I need to somehow freeze the rotation along the X and Y axes.

You can’t prevent rigidbody from turing over on hitting object.
You can lock it like how you did it. but it cannot be fully prevented.

It look like you are just setting velocity but aren’t using physic in game, kinda like you are just translating.

You can use physics in script like this:

#if (!arm_physics)

public function new() { super(); }

#else

<Your Code Here>

#end

Check this to understand how to use above code -> https://github.com/armory3d/armory_templates/blob/master/first_person/Sources/arm/FirstPersonController.hx

1 Like

You can keep it from rotating on X and Y by setting the Angular factor to zero for those axes in the physics properties panel:

image

3 Likes

Thanks all for help!
I did this with setAngularFactor.

Full code (and video: https://streamable.com/yjccn ):

package arm;

import iron.math.Vec4;
import armory.trait.physics.RigidBody;
import iron.Trait;

// AUTONOMOUS AGENT
class TestAI extends Trait {

#if (!arm_physics)
	public function new() { 
		
		super(); 
		
		}
#else

	var body:RigidBody;
	var move_speed = 30;
	var turn_speed = 3;

	public function new() {

		super();
		iron.Scene.active.notifyOnInit(init);
	
	}

	function init() {

		// get all components
		body = object.getTrait(RigidBody);
		notifyOnUpdate(update);

		}

	function update() {

		// move (in future change to apply force)
		var dir = object.transform.look().normalize();
		var vel = new Vec4(dir.x * move_speed, dir.y * move_speed, dir.z * move_speed);
		body.setLinearVelocity(vel.x, vel.y, 0);

		// turn	(in future change to random turn)
		body.setAngularVelocity(0, 0, turn_speed);
		body.setAngularFactor(0, 0, 0);

		}
#end
}
3 Likes