Set Orientation from other point

Discuss any questions about BEPUphysics or problems encountered.
Post Reply
PhilipArthurMoore
Posts: 12
Joined: Sat Oct 13, 2018 1:40 am

Set Orientation from other point

Post by PhilipArthurMoore »

hello,
i have a box and i set orientation but it roate around center point
how to set orientation from other point(ex: first point )
var box = new Box(new Vector3(0, 2f, 3), 2f, 0.2f, 0.2f, 0);
box.Orientation = Quaternion.CreateFromAxisAngle(Vector3.Backward, (float)Math.PI / 6);
thanks
Nitec57
Posts: 6
Joined: Mon Oct 08, 2018 5:26 pm

Re: Set Orientation from other point

Post by Nitec57 »

Hi,

one possible solution is to translate your object first, rotate it and then translate it back.
If you do this, your object will be rotated around the "translated origin".

For example: You have a 2x2 sized cube and want it to rotate around a specific vertex. The center point would be (x: 0, y: 0, z: 0). One of the 8 vertices has the coordinates (x: 1, y: 1, z: 1).

Your rotation Matrix could be:

Code: Select all

Matrix rotationMatrix = Matrix.CreateTranslation(1, 1, 1) * Matrix.CreateFromAxisAngle(Vector3.Up, MathHelper.PiOver2) * Matrix.CreateTranslation(-1, -1, -1);
It is helpful to write a static method for this.

I hope I could help you.

EDIT: The "Box" class has a WorldTransform-property you can use.

Code: Select all

Box box = new Box(new Vector3(0, 0, 0), 2, 2, 2, 0);
box.WorldTransform = rotationMatrix;
PhilipArthurMoore
Posts: 12
Joined: Sat Oct 13, 2018 1:40 am

Re: Set Orientation from other point

Post by PhilipArthurMoore »

Hello,
thanks for help
this is my code:

Code: Select all

var box = new Box(new Vector3(1, 2f, 3), 2f, 0.2f, 0.2f, 0);
var position = new Vector( 0, 0, 3.1f);
var t = box.Position - position;
var t2 = -t;
Matrix.CreateTranslation(ref t, out var tran1);
var orientation = Matrix.CreateFromAxisAngle(Vector3.Backward, MathHelper.PiOver4);
Matrix.CreateTranslation(ref t2, out var tran2);
box.WorldTransform = tran1 * orientation * tran2;
but it is not success
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Set Orientation from other point

Post by Norbo »

If you want to rotate an object that is offset from the origin of rotation, you need to rotate the offset.

Code: Select all

offset = previousCenterOfBody - centerOfRotation
rotatedOffset = offset * rotation
newCenterOfBody = centerOfRotation + rotatedOffset
newOrientation = orientation * rotation
More robustly, if you have the offset stored in the local space of the body ahead of time, you don't need to recalculate the offset over and over:

Code: Select all

newOrientation = orientation * rotation
worldOffset = localOffset * newOrientation
newCenterOfBody = centerOfRotation + worldOffset
PhilipArthurMoore
Posts: 12
Joined: Sat Oct 13, 2018 1:40 am

Re: Set Orientation from other point

Post by PhilipArthurMoore »

hello,
i maybe see the problem.
Orientation is rotate entity around lines( ex: x axis, y axis, AB line, .. )
Rotate function by use AngularVelocity will rotate entity around center point. So to Rotate entity around wiff offset point, can i use entity.CollisionInformation.LocalPosition? if it is true, so i understood the problem
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Set Orientation from other point

Post by Norbo »

The LocalPosition serves as a local offset from the center of mass to the center of the collision shape. In other words,

Code: Select all

collisionShapeLocation = LocalPosition * entity.Orientation + entity.Position
So yes, that can be used to make the collision shape rotate around a point that is not the same as the center of the collision shape. Entities always rotate around their center of mass, which is what the entity.Position property refers to.
Post Reply