Object rotation

Discuss any questions about BEPUphysics or problems encountered.
Post Reply
ggblake
Posts: 7
Joined: Thu Sep 17, 2009 4:19 pm

Object rotation

Post by ggblake »

What is the simplest way to rotate an entity a specigi number of degrees. Rotate sets it in motion but I need it to stop when fcing the camera
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Object rotation

Post by Norbo »

If you want it done in a single frame and don't care about collisions in the mean time, you can just set the orientation matrix (or orientation quaternion, or world transform).

The specific properties that you could change are:

orientationMatrix
orientationQuaternion
worldTransform
internalOrientationMatrix
internalOrientationQuaternion
internalWorldTransform


If you do care about the collisions or want it to rotate over multiple frames, I'd recommend checking its progress each frame and updating the angular velocity accordingly.

For example, if you had a (normalized) direction A attached to an entity that you wanted to align with some other (normalized) direction B, you can take the normalized cross product of the two directions to get the axis of rotation C (C = A x B / ||A x B||). The amount of rotation could be found by computing Acos(A dot B).

Then you figure out what angular velocity you want it to move at. First, just pick some scalar value that seems right. To prevent overshoot, use the minimum between the rotation remaining (computed earlier) divided by the time being simulated or the scalar you picked. In other words, angularSpeed = Min(rotationRemaining, angularSpeed).

Multiply that speed by axis C and set the entity's angular velocity to that value.

There's a lot of different ways to go about doing this sort of thing depending on the behavior you want and what other requirements you have. The above is just one somewhat simple option.
ggblake
Posts: 7
Joined: Thu Sep 17, 2009 4:19 pm

Re: Object rotation

Post by ggblake »

Thanks

I'll give it a whirl.

Not sure I get the ||A x B || notation

will Vector3.Crossproduct() work here ?
it will always be the Up axis
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Object rotation

Post by Norbo »

The A x B / ||A x B|| just meant "A cross B divided by the length of A cross B." So yes, getting the cross product and normalizing it would work if you needed to do that. If it's always going to be the up vector though, that makes things easier :)
ggblake
Posts: 7
Joined: Thu Sep 17, 2009 4:19 pm

Re: Object rotation

Post by ggblake »

one way I am trying is:

entity.orientationMatrix.Y = (float)rotation;
or
entity.orientationMatrix = Matrix.CreateRotationY((float)rotation);

This throws a compilation error, yet orientationMatrix has get/set properties.

What am I missing ?
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Object rotation

Post by Norbo »

There's a few things:
1: The orientationMatrix property returns a copy of the orientation matrix. If you take a field or property from the returned copy and try to set it, you're setting a 'worthless' value since it's not the original. The compiler checks this. For example:

Code: Select all

toAddBox.orientationMatrix.M11 = 1
generates an error:
Error 2 Cannot modify the return value of 'BEPUphysics.Entities.Entity.orientationMatrix' because it is not a variable....

2: The XNA Matrix class has no 'Y' field or property. If you meant 'Up,' then you need to give it a Vector3, not a float. I don't think this is what you are aiming for though since it doesn't produce a rotation.

3: Your second option:

Code: Select all

entity.orientationMatrix = Matrix.CreateRotationY((float)rotation);
works. This assumes, however, that rotation is a scalar. If rotation is a Vector3 or some other type that cannot be cast to a float, it will fail.
Post Reply