Trying to rotate a custom compund body + More

Discuss any questions about BEPUphysics or problems encountered.
Post Reply
Danthekilla
Posts: 136
Joined: Sun Jan 17, 2010 11:35 am

Trying to rotate a custom compund body + More

Post by Danthekilla »

Hey Norbo,

I have been writing a level editor for our games. It now supports adding objects with multiple components (i.e. 2+ physics bodys) so I can for instance add a fan that has 2 dynamic bodys, one for the fan blade and another for the bases body, Now in the editor I can rotate the object and any other components will rotate around correctly so even thou it can be made of 10 physics bodys it can be rotated as one piece... now that has all worked fine so far but now I am having a problem with getting it to work with rotating the revolute joint that spins the fan around...

How would you go about this as I am not sure this is the best way.

This is the code I currently use to constuct the fan.
in_ is the prefix I use for variable that have been passed into the build function.

Code: Select all

            Bodys.Add(new Box(new Vector3(0, 2, 0), 2f, 4f, 2f, 50));
            Bodys[0].mass = 0;//Settings.Mass;
            Bodys[0].dynamicFriction = 1.0f;
            Bodys[0].staticFriction = 1.0f;
            InitialPartStats.Add(new EntityPartStat(new Vector3(0, 0, 0), Quaternion.Identity));

            Bodys.Add(new Box(new Vector3(0, 2, 1.2f), 0.5f, 4f, 0.1f, 5));
            //Bodys[1].mass = 0;
            Bodys[1].dynamicFriction = 1.0f;
            Bodys[1].staticFriction = 1.0f;
            InitialPartStats.Add(new EntityPartStat(new Vector3(0, 0, 1.2f), Quaternion.Identity));

            RevoluteJoint axisJoint = new RevoluteJoint(Bodys[0], Bodys[1], (Bodys[0].centerPosition + Bodys[1].centerPosition) / 2, Vector3.Forward);
            axisJoint.motor.isActive = true;
            axisJoint.motor.settings.velocityMotor.goalVelocity = 30;
            axisJoint.motor.settings.maximumForce = 200;
            Constrants.Add(axisJoint);

            Position = in_Position; //NOTE THIS IS THE PROPERTY FROM BELOW

            for (int i = 0; i < Bodys.Count; i++)
            {
                if (!(g_LevelEditor.GET.GetEditorState != EditorStateType.EDITING && Settings.RenderOnlyInDebug))
                    g_EntityManager.GET.space.add(Bodys[i]);

                Bodys[i].tag = this;
                Bodys[i].internalCenterPosition = in_Position + InitialPartStats[i].StartingPosition;
                Bodys[i].internalOrientationQuaternion = in_Rotation + InitialPartStats[i].StartingRotation;

                Bodys[i].isActive = !Settings.StartAsleep;
            }

            for (int i = 0; i < Constrants.Count; i++)
                g_EntityManager.GET.space.add(Constrants[i]);
This is the code I currently use to rotate and move the fan.

Code: Select all

 
       public Vector3 Position
        {
            get { return Bodys[0].internalCenterPosition; }
            set
            {
                Vector3 OldPos = Bodys[0].internalCenterPosition;
                Bodys[0].internalCenterPosition = value;
                for (int i = 1; i < Bodys.Count; i++)
                {
                    Vector3 Diff = Bodys[i].internalCenterPosition - OldPos;
                    Bodys[i].internalCenterPosition = value + Diff;
                }
            }
        }

        public Quaternion Rotation
        {
            get { return Bodys[0].internalOrientationQuaternion; }
            set
            {
                Bodys[0].internalOrientationQuaternion = value;
                
                for (int i = 1; i < Bodys.Count; i++)
                {
                    Bodys[i].internalCenterPosition = Bodys[0].internalCenterPosition + Vector3.Transform(InitialPartStats[i].StartingPosition, Matrix.CreateFromQuaternion(Bodys[0].internalOrientationQuaternion));
                    Bodys[i].internalOrientationQuaternion = Bodys[0].internalOrientationQuaternion * InitialPartStats[i].StartingRotation;
                }
            }
        }

Anyway I hope you can help me it you have the time :)
I have been trying to get multi-object rotation to work for a few days now...

Also please take a look at a screenshot of a few very early development images.
(Note: Its all programmer art at the moment, but that will change soon as we have just hired 2 artists and a sound guy)

Screenshot at this link...
http://www.2point0studios.com/images/GamePreview.jpg

Thanks again for your help!
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Trying to rotate a custom compund body + More

Post by Norbo »

Progress picture is looking good!

At a glance I notice a couple of issues:
First, I'm not sure I follow the flow of setting position/rotation. First, the Position property is set, which moves every entity. But then in the body-adding for loop, the bodies' center positions are overwritten completely, apparently nullifying any effect the previous Position set call had.

Second:
Bodys.internalOrientationQuaternion = in_Rotation + InitialPartStats.StartingRotation;

This should be a quaternion multiplication, since addition is rarely a meaningful operation for quaternions (outside of special relativity). Adding quaternions could cause the quaternion to have a non-unit length so that it no longer represents an orientation. The internalOrientationQuaternion property normalizes the input value so it represents some rotation, but probably not the one you're looking for. Also, beware the order of multiplication as it is not commutative.

In addition, the position setting code in the for loop doesn't take into account rotation like your Rotation property does.

What kind of behavior is the joint exhibiting?
Danthekilla
Posts: 136
Joined: Sun Jan 17, 2010 11:35 am

Re: Trying to rotate a custom compund body + More

Post by Danthekilla »

Well I managed to get everything figured out and it all working :)

I managed to fix all the roatation and movement bugs today as well as most of the scale and static bugs too :)

Bepu sets the mass of an object very high when its static?

Any idea when you will be adding the pathing system to move objects along a path? In about 3 weeks or so i will be writeing my own but i figured that yours will be more stable and probably easier to use too... Anyway if it could be bumped up the list a notch or two i would be very happy :) (assuming that it doesnt require many other things to be added for it)

Glad you liked the picture :) Its about a week old and the graphics are a bit better now (have added ambient occlusion) and will be adding depth of field soon too 8)

Anyway thanks again for all your help.
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Trying to rotate a custom compund body + More

Post by Norbo »

Bepu sets the mass of an object very high when its static?
Yes; it chooses float.MaxValue arbitrarily. This is generally not used directly in any calculations; the kinematic actually has infinite inertia and cannot be affected by even gravity. Actual infinite float values were avoided for minor performance reasons.
Any idea when you will be adding the pathing system to move objects along a path?
While it is planned as one of the earlier features, 3 weeks might be a little aggressive. I definitely don't want each piece of v0.13.0's features taking months to finish, but little interruptions and issues can wreak havoc on short timescales. In other words, I'll try but no guarantees :)
Danthekilla
Posts: 136
Joined: Sun Jan 17, 2010 11:35 am

Re: Trying to rotate a custom compund body + More

Post by Danthekilla »

In other words, I'll try but no guarantees :)
Cool thanks :)
Thats great to hear.
Post Reply