Page 2 of 3

Re: Simple vehicle implementation

Posted: Thu May 03, 2012 8:13 pm
by Norbo
The object that needs to be added to the ModelDrawer is the vehicle.Body entity. The ModelDrawer will not recognize the Vehicle or VehicleInput directly.

If you can't see anything at all, checking the camera would be a good idea. Also note the existence of the BEPUphysicsDrawer's BoundingBoxDrawer which might be handy for verification purposes.

Re: Simple vehicle implementation

Posted: Thu May 03, 2012 8:27 pm
by mateod
And about wheels - everything works now (thank you very much! :)), but there is a small matter of wheels visibility, or invisibility. I loaded models provided by demo, but now i can't see them. And, can I load some custom .fbx model instead of drawing 2 boxes which imitate a car?

Re: Simple vehicle implementation

Posted: Thu May 03, 2012 8:34 pm
by Norbo
I loaded models provided by demo, but now i can't see them.
Make sure the effects are set up correctly and the appropriate transforms are applied.
can I load some custom .fbx model instead of drawing 2 boxes which imitate a car?
Yup. The physics engine has no concept of graphics. It doesn't stop you from doing anything. (The ModelDrawer is more restricted, but it's just a debug visualization tool, not a graphics engine.)

The physics engine just provides the current state of the objects in the form of positions and orientations or whatever else you need to move your graphics around.

For this sort of graphics specific stuff, I'd recommend heading over to the XNA website and checking out the samples. They have a bunch of great educational content for this sort of thing.

Re: Simple vehicle implementation

Posted: Thu May 03, 2012 8:50 pm
by mateod
Ok, I check their stuff out. I must admit, that the car is totally working (I solved problem with wheels) except for steering. I threw out the part of vehicleinput.update() which was responsible for checking wether it's active or not as it's supposed to be always active. So there are two arguments int Update() method. The second one is obvious, but I'm a little confused about the first one. Should I pass some space property to 'dt' argument? And also vehicle.update() should be just in my Update() method among space.Update() and ModelDrawer.Update()? Does the order matter?

Re: Simple vehicle implementation

Posted: Thu May 03, 2012 8:56 pm
by Norbo
"dt" is the simulation time elapsed since the previous update. The Space.TimeStepSettings.TimeStepDuration works for the demos because the demos do not use internal time stepping, so every frame one time step is simulated. You can do this too.

If you use internal time stepping or perform multiple time steps otherwise, passing in the elapsed gametime or the different amount of simulated time would be needed.

The order does not really matter. I put the vehicle update after the space update in the demos.

Re: Simple vehicle implementation

Posted: Thu May 03, 2012 10:59 pm
by mateod
That's a lot of useful information, great! I managed to work everything out with your help, even messed with it for a while tweaking some parameters. But still I'm not entirely sure about loading any fbx as a vehicle model. I figured that I could use TriangleMesh to get vertices and indices model information, but then I'm not sure how to wrap it up wit the CompoundBody so it would be suitable for the constructor of Vehicle(). Could you give me a hint? :)

Re: Simple vehicle implementation

Posted: Thu May 03, 2012 11:02 pm
by Norbo
The MobileMesh would be a more suitable entity type than the CompoundBody if you actually want to use a mesh for the physical body. The MobileMeshDemo shows how to configure them (it's pretty similar to setting up a StaticMesh).

However, that's a lot more detail than the physics needs for a good simulation. Generally, it's good to use a simplistic approximation for the physics while the complicated graphical mesh is drawn as the visual representation. Using a couple of BoxShapes in a CompoundShape or some ConvexHullShapes in a CompoundShape would get you better performance and possibly better behavior.

Re: Simple vehicle implementation

Posted: Thu May 03, 2012 11:08 pm
by mateod
So you mean that I should stay with the idea of

Code: Select all

var bodies = new List<CompoundShapeEntry>()
            {
                new CompoundShapeEntry(new BoxShape(2.5f, .75f, 4.5f), new Vector3(0, 0, 0), 60),
                new CompoundShapeEntry(new BoxShape(2.5f, .3f, 2f), new Vector3(0, .75f / 2 + .3f / 2, .5f), 10)
            };
to make approximate shape of the car and then cover it with an actual one? I kinda don't understand how to achieve that...

Re: Simple vehicle implementation

Posted: Thu May 03, 2012 11:34 pm
by Norbo
The complicated visual mesh can be transformed to match the position and orientation of the physics body. That way you can have a simple and fast physics object while keeping fancy looking graphics.

The Entity exposes a WorldTransform (built directly from its Position and Orientation) which you can use to transform the graphic.

For actually setting up the graphics systems needed to handle that sort of thing, the XNA samples are a good start.

Re: Simple vehicle implementation

Posted: Mon May 07, 2012 2:29 pm
by mateod
Hi again!

I must admit that your idea with regular model as a graphics and boxes as a physical model turned out to be gr8 one. But right now I'm facing another problem connected with rotation. I use custom model class, and It requires Vector3 position and Vector3 rotation parameter, which turns the model around its center with specified vector value. And now, there is no problem with specifying the position basing on vehicle.position, but how can I access the rotation and get it in a vector form?

Re: Simple vehicle implementation

Posted: Mon May 07, 2012 2:35 pm
by Norbo
That depends on how the rotation is stored. Three dimensional vectors are not as frequently used to express orientation as quaternions or matrices, but it might be an axis * angle representation. In that case, convert the entity's Orientation quaternion to an axis and angle (Toolbox.GetAxisAngleFromQuaternion can do this) and multiply them together.

Re: Simple vehicle implementation

Posted: Mon May 07, 2012 3:34 pm
by mateod
Thanks for the clue! It works, but not totally - I want a camera to follow the car. And to figure out what is wrong, I print the angle from Toolbox.GetAxisAngleFromQuaternion(ref q, out axis, out angle); and it seems to be always a number between 0 and 3.14. But when it passes these values, it also varies between PI values but this time, directions of rotation are changed. Then, the angle is still positive (cause if it was negative, then abs would solve the problem...). Do you have any clue how to make it work?

Re: Simple vehicle implementation

Posted: Mon May 07, 2012 3:54 pm
by Norbo
It sounds like the axis and angle method is producing expected results.

I'm not clear on what your goal is, though. Making a camera follow a car is best accomplished by just applying some transformations; jumping in and out of axis angle representation shouldn't be needed. For example, to put a camera behind and above a car, the camera's world transform (not to be confused with the 'view matrix,' which typically refers to the inverse of the camera's world transform) would be Matrix.CreateTranslation(0, 5, 5) * carEntity.WorldTransform.

Re: Simple vehicle implementation

Posted: Mon May 07, 2012 4:35 pm
by mateod
Ok, so I'll try to put it in a different light. I need the exact rotation angle of the car, because I need to use some class that rotates camera using a vector3. But now it seems that it doesn't differ the direction of rotation. And this what I get now is fine, but I have to make it rotate depending on direction.

Re: Simple vehicle implementation

Posted: Mon May 07, 2012 5:06 pm
by Norbo
It sounds like you want angle around a particular axis, which is distinct from the axis/angle provided by the actual orientation. For that, one of the easier ways would be to measure some axis following the orientation of the vehicle body against a test basis. If you want the facing angle around the Y axis, you could use Math.Atan2(entity.OrientationMatrix.Forward.Z, entity.OrientationMatrix.Forward.X).