[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);		


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(); }


<Your Code Here>


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


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:



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;

class TestAI extends Trait {

#if (!arm_physics)
	public function new() { 

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

	public function new() {


	function init() {

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


	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);