Vehicle sample using a Model of a vehicle
-
- Posts: 11
- Joined: Sat Mar 10, 2012 5:36 pm
Vehicle sample using a Model of a vehicle
I am trying to get my vehicle moving using BEPU, but am having trouble. I have created a new class called TankModelInput which is based on the VehicleInput class (I am using the Tank from the AppHub samples which is more like a car than a normal tank), but, I am not sure how I am to deal with the wheels since those are part of my model and I don't have a separate model for them to use. Can you tell me what all I would need to change to make the VehicleInput class work with a model or point me to an example of where someone has already done that?
Thanks.
Thanks.
Re: Vehicle sample using a Model of a vehicle
The vehicle and physics engine itself is blind to graphics in general, so it is a matter of isolating the individual pieces in a model and transforming them to match the motion defined by their physics representation. The VehicleInput shows how to transform graphics to match a wheel; the same logic can be applied to a mesh within a model. If I remember correctly, the Tank model does have separately transformable meshes in it. If the model is just one big monolithic mesh, then you'll need to separate it to control the wheels individually.
-
- Posts: 11
- Joined: Sat Mar 10, 2012 5:36 pm
Re: Vehicle sample using a Model of a vehicle
Yes you are correct about the Tank model having the wheels (as well as the turret and the hatch) all are transformable parts. So in my custom TankModelInput class, would I still need to use the Vehicle class and the Wheel class or are those unneeded since I have a model?
Re: Vehicle sample using a Model of a vehicle
The Vehicle class is a piece of the physics simulation. It and the rest of the physics engine have no concept of graphics; they are the simulation components. The graphics should represent the physics.
If you want to have vehicle physics, then you need the Vehicle and Wheels. If you don't need vehicle physics (because you made your own separate implementation, for example), then you don't need to use the Vehicle and Wheels.
If you want to have vehicle physics, then you need the Vehicle and Wheels. If you don't need vehicle physics (because you made your own separate implementation, for example), then you don't need to use the Vehicle and Wheels.
-
- Posts: 11
- Joined: Sat Mar 10, 2012 5:36 pm
Re: Vehicle sample using a Model of a vehicle
Thanks, I definitely want Vehicle Physics. I am still working on my TankModelInput class, if I do get it working I will send you a video of it. Thanks for you help so far.
-
- Posts: 11
- Joined: Sat Mar 10, 2012 5:36 pm
Re: Vehicle sample using a Model of a vehicle
Well I have my TankModelInput class close to working, the wheels now steer and rotate, the vehicle just doesn't move. Not sure what exactly I have missed though. The vehicle does have mass, so it shouldn't be thinking its an non-movable object. Any suggestions?
Re: Vehicle sample using a Model of a vehicle
I would recommend grabbing the BEPUphysicsDrawer from within the BEPUphysicsDemos source and adding the vehicle's body to a model drawer. That way, you can visualize what the physics body looks like and where it is. If it's truly not moving at all, make sure there's gravity to pull it to the ground and that the space is being updated.
-
- Posts: 11
- Joined: Sat Mar 10, 2012 5:36 pm
Re: Vehicle sample using a Model of a vehicle
I have tried as you had suggest to draw the tankmodel using the ModelDrawer, and nothing draws when I try it that way, so I am still having no luck. I did try a very simple test of just replacing my tank with just a simple Box (entity) and that does draw and does move, so I think that my problem is that I am not applying my tank model to the correct entity, that is, I have been creating a ConvexHull for my entity and then using that ConvexHull for creating my TankEntity, but I just can't seem get it to work, so I have come to the conclusion that I must be using the wrong entity to create my TankEntity, so what Entity should I be using?
Re: Vehicle sample using a Model of a vehicle
Was the model drawer's update method being called? Calling its draw without a prior update will leave the transforms unupdated/zeroed out.I have tried as you had suggest to draw the tankmodel using the ModelDrawer, and nothing draws when I try it that way, so I am still having no luck.
If the question is regarding which entity instance to represent with a graphic, then the answer is the Vehicle.Body.so I think that my problem is that I am not applying my tank model to the correct entity, that is, I have been creating a ConvexHull for my entity and then using that ConvexHull for creating my TankEntity, but I just can't seem get it to work, so I have come to the conclusion that I must be using the wrong entity to create my TankEntity, so what Entity should I be using?
If the question is regarding which entity types can be used, the answer is 'all of them.' All entities have the functionality of Entity. A ConvexHull is just a preconfigured entity type that uses a convex hull shape (check out the EntityConstructionDemo in the BEPUphysicsDemos for more information on how things are set up). There's no requirement that the vehicle body needs to be a box or anything like that.
-
- Posts: 11
- Joined: Sat Mar 10, 2012 5:36 pm
Re: Vehicle sample using a Model of a vehicle
Hi Norbo, Yes I am calling ModelDrawer.Update() at the bottom of my games Update loop (just before calling base.Update(gameTime)). If I am understanding you correctly, then using the ConvexHull as the Entity for my TankEntity should work, so i must be doing something wrong. Can you look at the following code and see if I am doing this correctly?
Thanks.
public TankModelInput(TerrainDemo game, Vector3 position, Space owningSpace, Camera cameraToUse, ModelDrawer drawer, Model tank)
{
// This is not all of the code but the main part that creates the Vehicle
Vector3[] vertices;
int[] indices;
Vector3 scale = new Vector3(1, 1, 1);
float mass = 100f;
//This load method wraps the TriangleMesh.GetVerticesAndIndicesFromModel method
//to output vertices of type TriangleGroupVertex instead of TriangleMeshVertex or simply Vector3.
TriangleMesh.GetVerticesAndIndicesFromModel(tank, out vertices, out indices);
ConvexHull hull = new ConvexHull(vertices, 10);
hull.Tag = "noDisplayObject";
hull.WorldTransform = Matrix.CreateScale(scale) * Matrix.CreateTranslation(position + hull.Position);
hull.Mass = mass;
Matrix world = Matrix.Identity * Matrix.CreateScale(scale) * Matrix.CreateTranslation(position - hull.Position);
tankEntity = new TankEntity(hull, tank, world, game);
Vehicle = new Vehicle(hull);
Space.Add(Vehicle);
ModelDrawer.Add(Vehicle);
Thanks.
public TankModelInput(TerrainDemo game, Vector3 position, Space owningSpace, Camera cameraToUse, ModelDrawer drawer, Model tank)
{
// This is not all of the code but the main part that creates the Vehicle
Vector3[] vertices;
int[] indices;
Vector3 scale = new Vector3(1, 1, 1);
float mass = 100f;
//This load method wraps the TriangleMesh.GetVerticesAndIndicesFromModel method
//to output vertices of type TriangleGroupVertex instead of TriangleMeshVertex or simply Vector3.
TriangleMesh.GetVerticesAndIndicesFromModel(tank, out vertices, out indices);
ConvexHull hull = new ConvexHull(vertices, 10);
hull.Tag = "noDisplayObject";
hull.WorldTransform = Matrix.CreateScale(scale) * Matrix.CreateTranslation(position + hull.Position);
hull.Mass = mass;
Matrix world = Matrix.Identity * Matrix.CreateScale(scale) * Matrix.CreateTranslation(position - hull.Position);
tankEntity = new TankEntity(hull, tank, world, game);
Vehicle = new Vehicle(hull);
Space.Add(Vehicle);
ModelDrawer.Add(Vehicle);
Re: Vehicle sample using a Model of a vehicle
Here's a few things (not all necessary/related to the problem at hand):
Instead of setting the mass of the hull afterwards, just pass the mass directly into the constructor for some extra speed and less code.
An Entity's world transform can contain only a rigid transform. If the transform set to the property includes a scale, the resulting orientation and position of the entity will be wonky due to the decomposition. It's mainly a convenience property for rendering; it's built directly from the Position and Orientation properties of the entity.
After removing the scale factor from the entity, it should be removed from here as well. The Matrix.Identity is also unnecessary. You may just want to use the entity.WorldTransform here to make it easier, unless there's some particular need for the different translation.
Instead of adding the Vehicle itself to the model drawer, add the vehicle's body. If the rest is set up correctly, then it should be visible.
Code: Select all
ConvexHull hull = new ConvexHull(vertices, 10);
...
hull.Mass = mass;
Setting the hull.Tag to "noDisplayObject" is also unnecessary unless you're using a demo-style display object add loop (where it explicitly checks for the tag before adding it). If you do have such a loop, then this will make it invisible (unless you add it directly).hull.Tag = "noDisplayObject";
Code: Select all
hull.WorldTransform = Matrix.CreateScale(scale) * Matrix.CreateTranslation(position + hull.Position);
Code: Select all
Matrix world = Matrix.Identity * Matrix.CreateScale(scale) * Matrix.CreateTranslation(position - hull.Position);
Code: Select all
ModelDrawer.Add(Vehicle);
-
- Posts: 11
- Joined: Sat Mar 10, 2012 5:36 pm
Re: Vehicle sample using a Model of a vehicle
I have tried everything that I can think of (and everything that Norbo suggested) and just cannot get it to work with the ConvexHull, I have it working using a very very short box with wheels (which are invisible) and it seems to function fine except (since there is no ConvexHull) I can throw items at my tank and they go right through it (which is to be expected) so I really do need to be able to use the convex hull, but it seems to me that the convex hull is a non-movable object because when I use it the vehicle will not move. Below is a link to a video of it moving using the very very short box with wheels. You may need to wait a few minutes before the video is ready (its still uploading).
http://youtu.be/eJZkQDo8wvM
http://youtu.be/eJZkQDo8wvM
Re: Vehicle sample using a Model of a vehicle
Is it just a matter of the wheels not protruding from the convex hull, so the vehicle never has traction?
Convex hulls truly are just as mobile as boxes and any other entity, though. It may help to fiddle with the demos VehicleInput a bit. For example, if this portion of the constructor:
is replaced by this portion:
The vehicle still works (and looks a little more futuristic).
(The vehicle can be 'broken' by modifying the above to increase the vertical thickness (use 1 instead of 0.4f, for example), making it so the wheels don't touch the ground.)
Convex hulls truly are just as mobile as boxes and any other entity, though. It may help to fiddle with the demos VehicleInput a bit. For example, if this portion of the constructor:
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), 1)
};
var body = new CompoundBody(bodies, 61);
Code: Select all
var body = new ConvexHull(new Vector3[]
{
new Vector3(-1.25f, -.4f, -2.25f),
new Vector3(1.25f, -.4f, -2.25f),
new Vector3(-1.25f, .4f, -2.25f),
new Vector3(-1.25f, -.4f, 2.25f),
new Vector3(1.25f, .4f, -2.25f),
new Vector3(-1.25f, .4f, 2.25f),
new Vector3(1.25f, -.4f, 2.25f),
new Vector3(1.25f, .4f, 2.25f),
new Vector3(0, 1, 1)
}, 61);
(The vehicle can be 'broken' by modifying the above to increase the vertical thickness (use 1 instead of 0.4f, for example), making it so the wheels don't touch the ground.)
-
- Posts: 11
- Joined: Sat Mar 10, 2012 5:36 pm
Re: Vehicle sample using a Model of a vehicle
Thanks Norbo, I am amazed at just how responsive you are to each and every question.
I believe that you are exactly right about the wheels not getting any traction, I was thinking that the convexhull had basically squashed the wheels so that they couldn't roll. I did notice the same problem when using the box if I made the box's height to tall (such as larger than 1.0f). I will play with the example you provided and see if I can use it to figure out what I need to change.
Thanks.
I believe that you are exactly right about the wheels not getting any traction, I was thinking that the convexhull had basically squashed the wheels so that they couldn't roll. I did notice the same problem when using the box if I made the box's height to tall (such as larger than 1.0f). I will play with the example you provided and see if I can use it to figure out what I need to change.
Thanks.
-
- Posts: 11
- Joined: Sat Mar 10, 2012 5:36 pm
Re: Vehicle sample using a Model of a vehicle
Thank You Norbo, I adjust the WheelSuspension restLength and now the wheels are below the ConvexHull and the vehicle does move. Thank you so much for you help.