User controlled movement

Discuss any questions about BEPUphysics or problems encountered.
Post Reply
marpe
Posts: 2
Joined: Tue May 18, 2010 6:32 pm

User controlled movement

Post by marpe »

I'm trying to make a clone of SkyRoads (http://www.youtube.com/watch?v=F6Rovi9QSDk) and need some help imitating the behavior of the ship. The steering behaves as if there's infinite friction in right and left, x-axis (velocity changes are instant and movement stops when you let go of the right or left arrow) and zero in forward, z-axis (there's no deacceleration).

I've tried using this for the left/right steering:

Code: Select all

if(ship.TouchesGround)
    ship.Entity.linearVelocity *= new Vector(0, 1.0f, 1.0f); // Sets the x-axis velocity to 0
But for some reason this hinders the movement in the z-axis. And since I don't want the ship to deaccelerate, I've set the static and dynamicfriction to 0 but it still speeds down.

The gravity also seems to be veeeeery low... The ship is roughly 5x3x2 units and gravity is set to -10. When I press the jump key I use ship.Entity.linearVelocity += Vector3.UnitY * 15.0f; It's as if everything is in slow-motion.

I've also experimented with bounciness but I only want the ship to bounce off the ground, not walls/obstacles.

I don't know if there's actually a need for an advanced physics engine as bepu, if anyone has another idéa of how to do this, please say so :) I didn't want to waste 90% of the time on collision detection/response as this is a school assignment mainly focused on the rendering.

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

Re: User controlled movement

Post by Norbo »

for some reason this hinders the movement in the z-axis.
The linearVelocity field is a "buffered" field. This means that writes are applied at the beginning of the next physics update and the read value will only update at the end of a physics update. If the linearVelocity is set based on its own value, like that code does, it will use the last read value in the computation.

For example, if linearVelocity's value starts (0,0,2) and the following code is executed:

Code: Select all

ship.Entity.linearVelocity += new Vector3(0, 0, 1); //Write buffer gets a new entry, (0,0,2) from read buffer + (0,0,1) = (0,0,3).
ship.Entity.linearVelocity *= new Vector(0, 1.0f, 1.0f); //Write buffer gets a new entry, (0,0,2) from read buffer * (0,1,1) = (0,0,2)
... more code...
update begins, (0,0,2) was the last write so it is used as the value
The linearVelocity property will have the value of (0,0,2) in the read buffer after the update (assuming the engine didn't further change it due to velocity or a collision).

You can see more information about this in the asynchronous updating documentation: http://www.bepu-games.com/BEPUphysics/d ... tation.pdf

To avoid this issue entirely, you can use the 'internal' version of the properties (like internalLinearVelocity). These properties are not buffered and will immediately update their value.
And since I don't want the ship to deaccelerate, I've set the static and dynamicfriction to 0 but it still speeds down.
By default, the friction coefficient is averaged between the surface and the object. If your surface has a non-zero friction, the collision will have non-zero friction.

You can change this by setting the environment friction to zero, or changing the way the engine blends friction. The following forces the engine use the smaller value of friction instead of the average.

Code: Select all

space.simulationSettings.collisionDetection.frictionBlendMethod = PropertyBlendMethod.min;
The gravity also seems to be veeeeery low... The ship is roughly 5x3x2 units and gravity is set to -10. When I press the jump key I use ship.Entity.linearVelocity += Vector3.UnitY * 15.0f; It's as if everything is in slow-motion.
With a gravity of -10, I assume you are expecting earthlike gravity. This would mean your units are meters. A ship with dimensions of 5x3x2 is about 16x10x6 feet. Another way to look at it is in comparison to an SUV; 5x3x2 meters is about as long/wide as a very large SUV, but also substantially taller. Something that size doesn't rapidly tumble around like the SkyRoads vehicle in real life. Shrinking the vehicle or increasing the gravity will make it closer.


The behavior of that ship is very nonphysical, so I'd recommend you use a kinematic entity instead of a dynamic entity for its motion. They can be created by not passing a mass into the entity's constructor, or later by calling the entity's becomeKinematic method. Kinematic entities can be thought of as having infinite inertia. They don't slow down or change velocities at all unless you specifically change it. They'll also happily tunnel through any obstacle, but in SkyRoads, as soon as you hit something you can catch an event to blow the player up before it has a chance to ram through anything.

Using a kinematic entity means you'll have to manage the velocity yourself in every way, including simulated gravity and bounciness. Having such precise control is more likely to get you the skyroads-style movement.
I don't know if there's actually a need for an advanced physics engine as bepu, if anyone has another idéa of how to do this, please say so I didn't want to waste 90% of the time on collision detection/response as this is a school assignment mainly focused on the rendering.
A physics engine might be a little overkill for this (particularly because there really isn't any dynamic motion or complex collision detection). If you haven't already checked it out, the BoxCollider library that is built into the XNA Ship Game sample might be helpful:
http://creators.xna.com/en-us/article/s ... oxcollider

If you don't have a premium membership, there are some alternatives. All that is really needed for SkyRoads is AABB tests and maybe an acceleration structure across your level so that your ship only tests collisions with nearby AABB's. Here's another XNA thread/sample:
http://forums.xna.com/forums/p/10017/52350.aspx
http://fabio.policarpo.nom.br/Demos.html
marpe
Posts: 2
Joined: Tue May 18, 2010 6:32 pm

Re: User controlled movement

Post by marpe »

I think I'll give it a try using bepu, but I'll check out the links as well :) The FPS-example on Fabio Policarpo's website seems really interesting. I guess I'll finally get a premium account on creators.xna as well, hehe....

Thanks for the quick reply! Btw, I'm really impressed with your work! Keep it up!
Post Reply