About rotation, Entity class and Model holders

Discuss any questions about BEPUphysics or problems encountered.
Post Reply
allansuperze
Posts: 3
Joined: Mon May 30, 2011 2:04 pm

About rotation, Entity class and Model holders

Post by allansuperze »

Hello all,

I'm trying to port one of my projects to Bepu but I've stumbled on some walls.

The project had a Movable3DObject class that was very, VERY similar to the Entity class, with just two significant differences. How they handle AngularVelocity and having a Model property.

First hit on the wall: AngularVelocity (AV from now on). Bepu uses a Vector3 to represent AV, previously I was using a Quaternion to represent it. Here is what I was doing to change the AV.

Code: Select all

Quaternion angularVelocityChange = Quaternion.CreateFromAxisAngle(-Vector3.Up, yaw * time);
//do some other stuff with angularVelocityChange such as changing pitch an roll
AngularVelocity *= angularVelocityChange;
Orientation *= AngularVelocity * time;
The last line, when the AV is applied to the orientation, is done inside Bepu, so I only have to worry about the first two. But with a Vector3 representing AV I don't know how to change it's rotation relative to it's orientation. When I do:

Code: Select all

AngularVelocity += new Vector(0, 0, rotate * time);
It changes the rotation absolutely, not relative to the object's orientation.

Two questions about this. How to accomplish a relative rotation with what I have (yaw, pitch, roll changes)? and, why use a Vector3 instead of a Quaternion to represent AV?


Second wall (this is a little one): The Entity class. It doesn't have a reference to a Model. Is this intended? The class I am using is extending it so it doesn't make much difference to me but it seems like such a common thing to have that makes me wonder if I'm not missing something obvious.


Third wall: Extending the Entity class. My first attempt to integrate Bepu was by making my class extend Bepu's Entity class, this resulted in null pointer exception when adding it to the Space. I then simply changed my class to extend Box and changed it's constructor accordingly, worked. Puzzled by this I made sure to set all properties that were being set in the Box constructor to the Entity extended version of my class. Only Position and Mass were available at the Entity class and they were already being set. Still the null pointer exception. So, shouldn't I inherit directly from Entity? I have to use some of Prefabs classes? It makes sense if I imagine that Bepu can only handle things that have a shape, like the Prefabs classes, but well, having just started with Bepu this weekend I'm a bit insecure and in need of confirmation.

I appreciate any help.
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: About rotation, Entity class and Model holders

Post by Norbo »

why use a Vector3 instead of a Quaternion to represent AV?
A few things:
-A Vector3 is smaller.
-Transforming between AngularVelocity and AngularMomentum is a simple matrix transformation (the inertia tensor).
-It is conceptually simple. The direction of the angular velocity is the axis of rotation. The magnitude of the angular velocity is the speed of rotation around that axis.
-Operations involving a Vector3 are generally simpler than a Quaternion. You can simply add two angular velocities to get a physically intuitive combined angular velocity, amongst many other things.
How to accomplish a relative rotation with what I have (yaw, pitch, roll changes)?
Pick a direction and a rotation speed. Multiply them together. You can take multiple directions/speeds and add them up too.

The directions do not have to be around the world axes. You could use the local up vector, for example, to yaw an object.
The Entity class. It doesn't have a reference to a Model. Is this intended? The class I am using is extending it so it doesn't make much difference to me but it seems like such a common thing to have that makes me wonder if I'm not missing something obvious.
The physics engine manages physics and physics alone. However, the Tag property of the Entity can hold any object. If you need to figure out what gameplay object is associated with a physical entity, that would be the way to do it.
Extending the Entity class.
While this is possible, it's usually far easier to use a different game architecture. Instead of trying to bind everything into a physics entity type, you can create a separate object type outside of the physics engine which contains an Entity instance as its physics proxy. Within that external gameplay object you can hold everything else necessary for your game, like a Model.
allansuperze
Posts: 3
Joined: Mon May 30, 2011 2:04 pm

Re: About rotation, Entity class and Model holders

Post by allansuperze »

Thank you! That helped a lot.

About how to use the Entity class in conjunction with my classes, makes perfect sense what you said. That's the kind of mistake I make for trying to learn stuff at 2am.

About the rotation, this is what I came up with:

Code: Select all

            Matrix rotMat = Matrix.CreateFromQuaternion(Orientation);
            Vector3 angularVelocityChange = Vector3.Transform(Vector3.Down, rotMat) * yaw;
            angularVelocityChange += Vector3.Transform(Vector3.Right, rotMat) * pitch;
            angularVelocityChange += Vector3.Transform(Vector3.Forward, rotMat) * roll;
            AngularVelocity += angularVelocityChange * time;
I pick the absolute axis for each rotation, transform it to a relative vector considering my object's orientation, and then rotate.

First, it works as intended. But it seems awfully complex having to perform those transformations in order to rotate an object around it's own axis. Am I missing something? a simpler way to achieve this? This time I don't even have the excuse of working late.
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: About rotation, Entity class and Model holders

Post by Norbo »

But it seems awfully complex having to perform those transformations in order to rotate an object around it's own axis. Am I missing something? a simpler way to achieve this?
As it turns out, the Matrix has various properties such as Up/Down/Right, and these are equivalent to transforming world up/down/right by the rotation. These pull directly from the columns in the matrix without performing any math (except for perhaps negations, depending on the direction). This works because the rotation matrix basically represents another orthonormal basis with its own local axes.

So instead, you could do:

Code: Select all

Vector3 angularVelocityChange = (rotMat.Down * yaw + rotMat.Right * pitch + rotMat.Forward * roll) * time;
allansuperze
Posts: 3
Joined: Mon May 30, 2011 2:04 pm

Re: About rotation, Entity class and Model holders

Post by allansuperze »

Man, you are fast!

Thank you very much, this solves everything.
Post Reply