Simple vehicle implementation

Discuss any questions about BEPUphysics or problems encountered.
mateod
Posts: 22
Joined: Thu May 03, 2012 1:42 pm

Re: Simple vehicle implementation

Post by mateod »

U're amazing, thanks! But to be honest, I had no idea I would have had so many questions about your vehicle physics implementation. But how can I change center of mass? It seems that it flips over every time I take a turn and I believe that the mass center might be the reason. Also, is there any way to change width of wheels and acceleration or power of the car?
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Simple vehicle implementation

Post by Norbo »

But how can I change center of mass?
The entity.CollisionInformation.LocalPosition offsets the shape from the entity.Position. This is effectively the same thing as changing the center of mass. So, if you want the center of mass to be lower relative to the shape, the LocalPosition should push the shape higher. The default demos vehicle configuration uses (0, .5f, 0).
Also, is there any way to change width of wheels
The wheels are actually ray casts, so no. You could increase the width of the graphical representation though. If greater traction is your goal, then increasing sliding friction would help (in the constructor of the WheelSlidingFriction given to the Wheel).
and acceleration or power of the car?
Acceleration is defined by the WheelDrivingMotor configuration. Each wheel's WheelDrivingMotor has a grip friction, maximum forward force, and a maximum backward force. Increasing the force and grip friction will make the car accelerate faster.
mateod
Posts: 22
Joined: Thu May 03, 2012 1:42 pm

Re: Simple vehicle implementation

Post by mateod »

And what if I would like to reset position on the car? I mean setting position of it is quite obvious, but i would like it to position it horizontally (for example put it on the wheels after it's been flipped). And I've noticed that when driving high speed, the vehicle tends to slide, wondering why?
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Simple vehicle implementation

Post by Norbo »

And what if I would like to reset position on the car? I mean setting position of it is quite obvious, but i would like it to position it horizontally (for example put it on the wheels after it's been flipped).
Set the orientation to some known good state. Quaternion.Identity might work. However, setting the Orientation property counts as a nonphysical teleportation. It can get your vehicle into invalid states. If this is a concern, you could pull it back to a good orientation by applying angular impulses or by verifying the state before applying it. Setting the Orientation is easy, though :)
And I've noticed that when driving high speed, the vehicle tends to slide, wondering why?
If you mean it slides when turning at high speeds, that's just insufficient sliding friction. However, sliding at high speeds is a common real-world phenomenon.
mateod
Posts: 22
Joined: Thu May 03, 2012 1:42 pm

Re: Simple vehicle implementation

Post by mateod »

Yepp, everything works fine now. Maybe you remember when I asked about models connected with vehicle? Right now I'm facing this problem, cause I put it off till today. Well, it's just now I have loaded model, working car, the last thing is to glue these two together. I have this code:

Code: Select all

arModel.Position = vehicle.Vehicle.Body.Position;
            carModel.Rotation = new Vector3 (vehicle.Vehicle.Body.Orientation.X, angle, vehicle.Vehicle.Body.Orientation.Z);
            vehicle.Update(space.TimeStepSettings.TimeStepDuration, KeyboardState);
}
But code above doesn't seem to change rotation in all directions (Y is the same you showed me with atan for x and z and works ok), but there is problem with X and Z - the model seems to be too horizontal, it doesn't fit the hill angles. I suppose it may have something to do with Body.Orientation.W, but no idea how to match all these values...

[EDIT] Oh, and I totally forgot - my custom model class has rotation field, which is Vector3 - it gets x,y and z parameters in radians.
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Simple vehicle implementation

Post by Norbo »

[EDIT] Oh, and I totally forgot - my custom model class has rotation field, which is Vector3 - it gets x,y and z parameters in radians.
That is likely what is known as Euler angles. They are indeed a very common representation (slipped my mind before :)), but I would strongly recommend moving away from them. They only appear intuitive. Once you get into any nontrivial transformations, like the ones you're working on now, they are a huge pain.
carModel.Rotation = new Vector3 (vehicle.Vehicle.Body.Orientation.X, angle, vehicle.Vehicle.Body.Orientation.Z);
The Orientation property is a quaternion; the individual values do not represent angles by themselves.

I would very strongly recommend running away from Euler angles as fast as you possibly can. The entity.WorldTransform tells you exactly where the entity is in a graphics-convenient format. Transforming the model by the WorldTransform will make the model follow the entity. The idea of matrices and transformations may appear daunting at first, but if treated like a set of tools in a tool box instead of a bunch of numbers, they are really quite simple- far, far simpler than Euler angles.

The GettingStartedDemo shows this in use. Also, for graphics stuff, check out the XNA website samples for ways of managing transformations.
mateod
Posts: 22
Joined: Thu May 03, 2012 1:42 pm

Re: Simple vehicle implementation

Post by mateod »

Yes, I've already learnt that using matrices is some (in my case not huge so far) simplification. But I'm afraid I am forced to use 'regular' (Euler) due to my class requirements (yeah...) and the fact, that I would be forced to completely change some code which I'd really love to avoid :(
That's why I was wondering if I can 'extract' the exact angles from a quaternion.
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Simple vehicle implementation

Post by Norbo »

But I'm afraid I am forced to use 'regular' (Euler) due to my class requirements (yeah...)
Oof; well, then I would recommend doing as much of the math as possible in non-Euler angles before doing a final conversion.
That's why I was wondering if I can 'extract' the exact angles from a quaternion.
It is indeed possible. The specifics vary on exactly what convention of Euler angles are being used (another one of those hidden unintuitive factors). Unfortunately, there's no helper methods in BEPUphysics for this, and I'm not aware of any Euler decomposition methods in XNA either (there are some Euler composition methods, like Quaternion.CreateFromYawPitchRoll, but those are conceptually simple).

I'd recommend googling it- http://www.euclideanspace.com has some pages about this too.
mateod
Posts: 22
Joined: Thu May 03, 2012 1:42 pm

Re: Simple vehicle implementation

Post by mateod »

Ok, I found a solution with very simple explanation. If someone had the same problem, here's a link: http://forums.create.msdn.com/forums/p/4574/23763.aspx
Thank you Norbo for the all effort you put into solving my problems, I'm very glad I came here for help! :)
mateod
Posts: 22
Joined: Thu May 03, 2012 1:42 pm

Re: Simple vehicle implementation

Post by mateod »

@Update: Do you think is it possible to leave just wheels visible in vehicle body? :)
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Simple vehicle implementation

Post by Norbo »

The answer is yes, though I don't exactly know what you mean :) The physics are totally separate from, and impose absolutely no requirements on, the graphics used to represent the physics. You are free to do whatever you want.
mateod
Posts: 22
Joined: Thu May 03, 2012 1:42 pm

Re: Simple vehicle implementation

Post by mateod »

Yeah, but I mean is hide these blocks and show only wheels for example in a demo program (cause I have quite similar, not to say almost exactly the same) :)
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Simple vehicle implementation

Post by Norbo »

If you just don't want the vehicle body entity to be visualized by the ModelDrawer, then do not add the body entity to the ModelDrawer. The wheels are handled separately and will still be visible.
Post Reply