[SOLVED] Changing rotation from an entity doesn't happen until it's not moving

I would like to clamp the rotation to avoid the plane to go upside-down, to do so i just clamped the rotation from the entity like this.

Clampz.prototype.clampRotation = function(){
    var planeRotation = this.entity.getRotation().getEulerAngles();
    console.log(planeRotation.z);
    if (planeRotation.z !== lastRotValue) {
        var clampZRotation = pc.math.clamp(planeRotation.z, -50, 50);
        this.entity.setEulerAngles(0, 0, clampZRotation);
    }
};

After the plane goes static, the rotation happens. Feels weird, like the code is happening after the physics.

https://playcanvas.com/project/497554/overview/physicstest

I’ve implemented a console.log where you can see when the rotation happens.

Thanks!

EDIT
Extra “Thanks” to whoever knows why the image is blurry when the asset is clearly fine

As it’s a physics object, you need to do set rotations and positions via the rigidbody and not the entity as at the end of the physics simulation update, the entity is synced to the rigid body in the simulation.

If you look at the rigid body component source, you can work out how to get and set the rotation of the physics transform: https://github.com/playcanvas/engine/blob/master/src/framework/components/rigid-body/component.js#L545

The image is blurry because I’m guessing the original image wasn’t 128 x 32 pixels so when you imported it to the project, the editor resizes it to the nearest power of 2 and does a interpolation of pixel colour. Make sure your textures are powers of 2 to begin with and that should help.

Will’s Flappy Bird project is an example of using a sprite texture atlas and material UV settings to create sprites.

https://playcanvas.com/project/375389/overview/flappy-bird

2 Likes

Damn right :smiley:
thank you!

Pd: i will work onto that sprites then, thank you again :slight_smile: