move an entity

Discuss any questions about BEPUphysics or problems encountered.
Post Reply
grobi
Posts: 34
Joined: Wed Sep 23, 2009 5:47 pm

move an entity

Post by grobi »

hi,
how do i make an entity like an airplane move / fly towards its "direction"?

thx
Grobi
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: move an entity

Post by Norbo »

If you have a specific speed/direction in mind, the easiest way to do it is by changing the velocity of the entity. Entities have both linear and angular velocity properties available.

You can also apply impulses and forces if you're looking for a more physical simulation. Going this route usually requires more tuning to get to the desired result.
grobi
Posts: 34
Joined: Wed Sep 23, 2009 5:47 pm

Re: move an entity

Post by grobi »

i wanna go the more physical simulation way, but first, how do i use a Force right? If i just want to move the plane "forward" with a constant "force" how do i have to setup the Force? As position shall I use center of mass of the body or just its center position? But way harder seems the dir vector to me, how do I calculate that? I got the xna worldmatrix of the entity and I can get a normalized "direction" vector of that, may those things help me?

Grobi
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: move an entity

Post by Norbo »

You don't need to really calculate anything for the direction. If you want a 'forward' direction, try a few axes (maybe the Forward Left and Up vectors of the world matrix for starters) to see which one coincides with what you want.

The center of mass and center position are the same location unless you have changed the center of mass manually. Applying at the center of mass will exert no torque even if you later offset the center of mass from the center position, so you may want to go that way.

Since the 'forward' direction and the center of mass change over time, you should ensure that your force has its isTrackingTarget and isChangingDirectionWhileTracking fields set to true. This will make the force push in the 'local' forward direction always.

However, I would strongly recommend looking into applying impulses or changing velocities to control precise motion. Applying forces are good for playing around or for very specific effects, but if your plane is supposed to be controllable by a player, it's very likely that you'll end up using either fine-tuned impulses or velocities. You can think of impulses as instantaneous forces applied each frame. Over time, these impulses can act like normal forces if you so choose and provide a lot of control in other respects.
grobi
Posts: 34
Joined: Wed Sep 23, 2009 5:47 pm

Re: move an entity

Post by grobi »

thank you very much for the explanations, i think i will go on reading about impulses cos
the whole thing i am trying to do goes into direction of multiplayer aircombat style game.

Grobi
grobi
Posts: 34
Joined: Wed Sep 23, 2009 5:47 pm

Re: move an entity

Post by grobi »

ok impulse seem a bit tricky, i tried like this
Vector3 direction = Vector3.Normalize(new Vector3(worldMatrix.M31, worldMatrix.M32, worldMatrix.M33));
Vector3 trust = (direction * -Throttle);
Vector3 yaw = worldMatrix.Left * (Yaw * duration);
Vector3 roll = worldMatrix.Up * (Roll * duration);
Vector3 pitch = worldMatrix.Up * (Pitch * duration);
Vector3 p = body.centerOfMass + (direction * 10f);
body.applyImpulse(p, yaw);
p = body.centerOfMass + (worldMatrix.Left * 5f);
body.applyImpulse(p, roll);
p = body.centerOfMass + (direction * 10f);
body.applyImpulse(p, pitch);
body.applyImpulse(body.centerOfMass, trust);
the plane flies in its direction, turns and pitches and rolls, but it doesn't look right, i can't fly curves its just slides a bit etc.
Is there any paper or reference on how an airplane could be simulated with bepu physics or with information which can be adopted to use with bepu physics?
Grobi
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: move an entity

Post by Norbo »

What kind of game are you planning on making? Is this a realistic aircraft simulation (or a close approximation), or is it supposed to feel arcadey? The approaches can be significantly different.

Here's some base information:

Applying an impulse to an entity at a given location P with a given strength D will always change the entity's linear momentum by D regardless of P. The change in angular momentum is based on the 'lever arm' from the center of mass to P.

So, by applying the impulses in the manner posted, you will likely experience a significant amount of sliding in inappropriate directions. There is nothing stopping the plane from sliding either; if the plane tried to bank around a turn using that system, it would accelerate in the new direction but all the old momentum would continue shoving it along its old path, producing a highly slidey effect.

To get what you want, you'll need to think about what exact behaviors you want. For example, let's say that you wanted a fairly arcadey feel that prevented your plane from 'sliding' in any direction except forward/backward. How would you go about doing this? One option would be to compute the sliding velocity and get rid of it.

What's the sliding velocity? By our definition above, it's any velocity that isn't forward/backward. With some vector "planeForward" representing the forward direction of the plane:

Code: Select all

Vector3 slidingVelocity = plane.internalLinearVelocity - planeForward * Vector3.Dot(planeForward, plane.internalLinearVelocity);
To remove this unwanted velocity, we need to change the plane's velocity. You can do this using impulses or whatever else you want, but it's very simple to just do this:

Code: Select all

plane.internalLinearVelocity -= slidingVelocity;
Any plane velocity existing before this computation that would have caused the plane to slide has been removed. There's many ways of going about doing this sort of thing, so just try to come up with a set of very specific rules that will govern how your plane will act. These rules can be used to manage the physics.

To understand and closely approximate 'real' plane behavior, you should take a look around online. General resources covering lift, drag, thrust and those sort of topics can be translated into rules and physics code. The level of detail you want to portray in your simulation will determine how many and how real these rules are.
grobi
Posts: 34
Joined: Wed Sep 23, 2009 5:47 pm

Re: move an entity

Post by grobi »

thanks a lot, with removing the "slide" velocity turning, rolling etc. looks better now.
But thats all pretty far away from fancy flying. Fine tuning the impulses will take several test-hours i think and for making it realistic i think i will have to read more about the real flying physics like drag, drift u mentioned.
Btw. is there a simple way of using airdrag or makin the plane behave more like it is put in a liquid? (Thats the way I used to handle aircrafts with other physicsengines like NewtonPhysicsDynamics (boyancy forces))
And a question about terrain, i got it all working but is there a way on decreasing memory usage? I noticed a terrain from heightmap 256x256 consumes something about 150MB of memory and using a 1024x1024 heightmap which i am planning to use in the game takes nearly 1GB of memory. Ok most computer systems today have 2GB or more of ram but the game shall run fine on "older" machines, too.

Grobi
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: move an entity

Post by Norbo »

You can increase the damping of the plane entity to simulate flying through syrupy air. Entities have linearDamping and angularDamping fields that take values from 0 to 1, 0 being undamped and 1 being totally stuck.

A buoyancy force can be simulated with impulses or a force that just pushes upward. A realistic "lift" impulse would work here too. There technically exists a FluidVolume object (which has a buoyancy force and damping built in), but it has some overhead for this sort of simulation since it has to determine if an object is in the fluid to begin with.

The memory usage associated with the terrain you're seeing is probably the graphics. Try turning off the generation of graphics, your total memory usage should drop from around a gigabyte to 30-60 megabytes. The Demos display system isn't designed for extreme speed or memory efficiency, so if you want to render a large terrain, you'll need to set up your own renderer.
grobi
Posts: 34
Joined: Wed Sep 23, 2009 5:47 pm

Re: move an entity

Post by grobi »

ok i noticed the graphics was in the memory calculation, my fault.
I build a quadtree to display the graphics which speeds up the drawing enormous.
So i can easily display a 1024x1024 map or bigger, without the quadtree i wasn't able to display a map bigger than 256x256 because of the onboard graphics device i got.

Grobi
Post Reply