How can I modify the positions of the shapes in a compound ?

Discuss any questions about BEPUphysics or problems encountered.
Post Reply
csensivas
Posts: 5
Joined: Sun Sep 30, 2012 7:01 pm

How can I modify the positions of the shapes in a compound ?

Post by csensivas »

Hello guys, I am trying to modify the "boxes" of a compound shape entry on the run.. How could I do that ?

I am trying to modify it directly but fail.. Maybe I am messed up..

Code: Select all

                    
Quaternion or = vehicleInput.Vehicle.Body.Orientation;
                    Vector3 pos = vehicleInput.Vehicle.Body.Position;
                    var lowCse = (vehicleInput.Vehicle.Body as CompoundBody).Shapes[0];
                    var upCse = (vehicleInput.Vehicle.Body as CompoundBody).Shapes[1];
                    var lowBox = lowCse.Shape as BoxShape;
                    var upBox = upCse.Shape as BoxShape;

                    var list = new List<CompoundShapeEntry>()
                    {
                        new CompoundShapeEntry(new BoxShape(lowBox.Width, lowBox.halfHeight, lowBox.Length), lowCse.LocalTransform.Position + value, lowCse.Weight),
                        new CompoundShapeEntry(new BoxShape(upBox.Width, upBox.halfHeight, upBox.Length), lowCse.LocalTransform.Position, upCse.Weight)
                    };

                    vehicleInput.Vehicle.Body = new CompoundBody(list);

                    vehicleInput.Vehicle.Body.IgnoreShapeChanges = false;
                    vehicleInput.Vehicle.Body.Orientation = or;
                    vehicleInput.Vehicle.Body.Position = pos;
                    vehicleInput.Vehicle.Body.Tag = "The car chassis!";

                    bepuPM._debugDrawInvalidated = true;
                    bepuPM.RefreshDebugDrawer();
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: How can I modify the positions of the shapes in a compou

Post by Norbo »

In what way does it not work? In other words, what do you expect to see, and what do you get instead?

At a glance, there appear to be some syntax errors (lowBox.halfHeight and upBox.halfHeight are the names of private fields; HalfHeight with a capital first H is the name of the public property). The positions passed to the new compound shape entries are both based on lowCse's position; I'm not sure if that was intended.

IgnoreShapeChanges does not need to be set to false; it's a new entity and IgnoreShapeChanges defaults to false. In addition, unless you later adjust the shape, no shape change event will occur. Setting the position and orientation of the entity does not change the shape.

If you're seeing wheels suddenly become unsteerable or something along those lines when the vehicle is at rest, it probably just went to sleep. The Vehicle constructor sets flags on its body to prevent this from happening. You can do it too, externally:

Code: Select all

            Vehicle.Body.ActivityInformation.IsAlwaysActive = true;
            Vehicle.Body.ActivityInformation.AllowStabilization = false;
csensivas
Posts: 5
Joined: Sun Sep 30, 2012 7:01 pm

Re: How can I modify the positions of the shapes in a compou

Post by csensivas »

Dear Norbo!

Thanks for you quick answer. Lower cases due to copy-paste.

Actually I just want to be able to modify the compound shape - the body of the vehicle on the fly .. I am using Sunburn and wrote a component so that I can adjust the sizes of the body shape through the editor. After I modify the sizes of the each box in the vehicle body, i refresh the debug drawer and it appears fine.. But how can I modify the positions of the each box ? I mean how can I reach them ?

Code: Select all

(vehicleInput.Vehicle.Body as CompoundBody).Shapes[0].LocalTransform.Position += new Vector3(0, 1, 0);
For eaxample the above does not seem to work...

I guess I have to use, but that does not reflect neither in debug drawer nor in space:

Code: Select all

                    RigidTransform rT = (vehicleInput.Vehicle.Body as CompoundBody).Shapes[0].LocalTransform;
                    RigidTransform transform = new RigidTransform(new Vector3(0, 1, 0));
                    RigidTransform outTransform;
                    RigidTransform.Transform(ref rT, ref transform, out outTransform);
                    CompoundShapeEntry cse = (vehicleInput.Vehicle.Body as CompoundBody).Shapes[0];
                    cse.LocalTransform = outTransform;
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: How can I modify the positions of the shapes in a compou

Post by Norbo »

CompoundShapes are designed to be immutable (mostly; there does exist a CompoundHelper to split shapes). All the properties return copies of the internal data; attempting to change them will either cause a compiler error or just do nothing because it's not modifying the original data.

Creating a new compound shape is the currently recommend method for doing this sort of thing. You can avoid recreating the entire entity by using a MorphableEntity, which allows you to set the EntityCollidable on the fly. More information can be found in the EntityConstructionDemo.cs in the BEPUphysicsDemos.

Efficient mutable CompoundShapes (and mutable StaticGroupShapes) are something I'd like to add, but they've been on the backlog for more than a few months. Maybe once I finally get around to polishing up that destructibility system I made more than a year ago I'll have a strong reason to do it ;)
csensivas
Posts: 5
Joined: Sun Sep 30, 2012 7:01 pm

Re: How can I modify the positions of the shapes in a compou

Post by csensivas »

Thanks ! I will try that...

And how do you think that I can "totally" prevent the vehicle wheels to be airborne anytime.. I mean i want the vehicle all the time stay on the track (static mesh) that it is moving on.. First thoughts were applying a force to the vehicle in direction of its body's world's down vector, or raycasting and getting the distance etc. but i am sure there would be other ways :)

Any ideas ? I mean suggestions about vehicle settings or friction or grip settings ?

:roll: :roll: thanks in advance
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: How can I modify the positions of the shapes in a compou

Post by Norbo »

Shoving the car down with a force or finding the ground with a ray cast are both acceptable solutions. There's no perfect physical way to do it through mere tuning since cars going off bumps do tend to get some air time in real life :)
csensivas
Posts: 5
Joined: Sun Sep 30, 2012 7:01 pm

Re: How can I modify the positions of the shapes in a compou

Post by csensivas »

Norbo! great engine really.. i congratulate all your efforts. and appreciate.
Post Reply