Jump to content

Getting correct rotation


Paumonsu
 Share

Recommended Posts

Hi guys.

I've recently started messing around with Babylon and my opinion sofar is that's fucking amazing. 

I came across a problem I can't figure out. I want to get the current rotation Vector3 after rotating the mesh with the rotate method.

//Initialization of cube rotation
cubeLogo.rotate( new BABYLON.Vector3(0,1,0), new BABYLON.Angle.FromDegrees(45).radians(), BABYLON.Space.LOCAL );
cubeLogo.rotate( new BABYLON.Vector3(0,0,1), new BABYLON.Angle.FromDegrees(-27).radians(), BABYLON.Space.WORLD );

babylon01.thumb.jpg.06f4a5c01a9773013074

I need the current rotation as Vector3 so i can assign the Vector to an animation key:

/* ANIMATIONS */
var logoRotateAnimation = new BABYLON.Animation("logo-rotate", "rotation", 10,
                              BABYLON.Animation.ANIMATIONTYPE_VECTOR3,
                              BABYLON.Animation.ANIMATIONLOOPMODE_CYCLE);

var logoRotateKeys = [];
var initRotation = cubeLogo.rotationQuaternion.toEurlerAngles();
var wRotation = new BABYLON.Vector3(0, -Math.PI / 2, 0);
var pRotation = new BABYLON.Vector3(0, 0, 0);
logoRotateKeys.push({
	frame: 0,
	value: initRotation 
});

logoRotateKeys.push({
    frame: 30,
	value: wRotation 
});

I can't get it working in any way. I'm sure I'm missing something. I've tried to get the current rotation as cubeLogo.rotationQuaternion.toEurlerAngles() but I get the following initial rotation result:

babylon02.thumb.jpg.0a3e4d32d7308ab04792

Also, why setting mesh.rotation after using mesh.rotate() has no effect on the mesh rotation?

Link to comment
Share on other sites

BABYLON.quaternion.RotationYawPitchRoll(yaw, pitch, roll)

instead of your rotation vectors.

also any time you do standard rotations you should do a Math.PI/x.  x being the amount you want to rotate, so 2 would be 45 degrees 4 I belive is 90 and 8 is 180?  not sure on that last part but its easy to figure out

Link to comment
Share on other sites

18 minutes ago, adam said:

mesh.rotate uses quaternions.

If you use quaterions, mesh.rotation is disabled.

Do you want the forward vector?

Yeah, i guess knowing how to calculate that should be enough.

I've tried to calculate the up vector but it's giving me a wrong direction.

 

cubeLogo.computeWorldMatrix();
var matrix = cubeLogo.getWorldMatrix();
var upLocal = new BABYLON.Vector3(0,1,0);
var upGlobal = BABYLON.Vector3.TransformCoordinates(upLocal, matrix);

 

Link to comment
Share on other sites

you do not need to know the up at all with BABYLON.quaternion.RotationYawPitchRoll(yaw, pitch, roll)

Now to establish its rotation value after the transform you need to decide what your reference point is... your camera or your object in global specs depending on what you want to do. 

once you decide that I can help you.


Also there is the possibility that there are younger children on this site so could you please edit your first post for its vulgarities

Link to comment
Share on other sites

3 minutes ago, adam said:

Please let the moderators handle this stuff.

I am a father, and I live in America, if I don't like something I'm gonna say something... and its on us as a community to keep the content appropriate for others in the community that may not be mature enough for certain things.  So Please Adam be supportive of the values of integrity.

Link to comment
Share on other sites

What offends people is subjective.

I've seen a post of yours that I found offensive, but I said nothing.

I've also seen some posts by the big guns (biggest) here on this forum that would offend you.  Do you want them to go back through their history and clean it up for you?

You are not a moderator of this forum and neither am I.

Peace.

 

Link to comment
Share on other sites

2 things:

1 - You need to ensure that the origin of the mesh is where you think it is.  This is the implicit 0,0,0 location that all your vertex positions are in relation to.  It does not have to be in the center.  If this not correct, nothing is ever going to work.  This is really better found / adjusted in the tool used to make the mesh.  Setting the pivot point once loaded could be an alternative to changing an incorrect origin.

2 - This was already covered. You can use either Vector3 or Quaternion (internally always Quaternion).  Think once you start using Quaternion externally though, Vector3 is ignored.  Both Blender & 3DSmax can specify rotation either way.  Setting Mesh.rotationQuaternion = null, should allow you to use Vector3 again.

Link to comment
Share on other sites

Thank you all for the answers. You all gave me great ideas of how to get the correct transform.

The way I position the cube now is using quaternions, I also made sure the pivot was on the center of the cube. I tried placing the pivot on 3ds max but it doesn't seem to export to Babylon 

cubeLogo = scene.getMeshByName("webpixel3d-logo");
cubeLogo.position = new BABYLON.Vector3(0,0,0);
cubeLogo.setPivotMatrix( BABYLON.Matrix.Translation( 0, -cubeLogo.getBoundingInfo().boundingSphere.radius / 2, 0 ) );    
var yawQuaternion = BABYLON.Quaternion.RotationAxis( new BABYLON.Vector3(0,1,0), CUBELOGO_ANGLE_YAW );
var tiltQuaternion = BABYLON.Quaternion.RotationAxis( new BABYLON.Vector3(1,0,1), CUBELOGO_ANGLE_TILT );
var wp3dQuaternion = yawQuaternion.multiply( tiltQuaternion ).normalize();
cubeLogo.rotationQuaternion = wp3dQuaternion;

And I'm using Quaternions for animating it instead of Vector3.

 

Link to comment
Share on other sites

I ended up just using Vector3 rotations. I find them a lot easier to understand:

cubeLogo.defaultRotation = new BABYLON.Vector3( Math.PI / 8, Math.PI / 4, Math.PI / 8 );
cubeLogo.wRotation = new BABYLON.Vector3( 0, 0, 0);
cubeLogo.pRotation = new BABYLON.Vector3( 0, Math.PI / 2, 0);
cubeLogo.threeRotation = new BABYLON.Vector3( 0, Math.PI / 2, Math.PI / 2 );

 

Now that I have been experimenting with rotations, Isn't there any built in methods for getting Local Axis? Up, Front, Down? Yeah I know I can get them using getWorldMatrix(), as Adam explained to me before, but it makes the code a mess if I have to use it too much (which I do).

Link to comment
Share on other sites

Thanks adam. I was just wondering why aren't they built in since they are really useful.

I wonder if this could be included in a future release:

BABYLON.Quaternion.prototype.getForwardVector = function() {
	return new BABYLON.Vector3( 2 * (this.x * this.z + this.w * this.y), 
						2 * (this.y * this.z - this.w * this.x),
						1 - 2 * (this.x * this.x + this.y * this.y));
}

BABYLON.Quaternion.prototype.getUpVector = function() {
	return new BABYLON.Vector3( 2 * (this.x * this.y - this.w * this.z), 
	                    1 - 2 * (this.x * this.x + this.z * this.z),
	                    2 * (this.y * this.z + this.w * this.x));
}

BABYLON.Quaternion.prototype.getRightVector = function() {
	return new BABYLON.Vector3( 1 - 2 * (this.y * this.y + this.z * this.z),
	                    2 * (this.x * this.y + this.w * this.z),
	                    2 * (this.x * this.z - this.w * this.y));
}

 

Source was: http://nic-gamedev.blogspot.com.es/2011/11/quaternion-math-getting-local-axis.html

Link to comment
Share on other sites

I think I already added something to accomplish the purpose you have in mind to AbstractMesh: POV Rotation & POV Movement. There are also calc versions of each.  The calc versions are primarily for iteratively moving and rotating at the same time, as seen in this test scene here.  These cubes are moving forward, but also iteratively rotating.

It is assumed the the vast majority of meshes are defined facing forward, but facing backwards can also be done.

If you do add these, make sure to do it at AbstractMesh not Mesh, or InstancedMesh class will not be able to use them.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...