3
\$\begingroup\$

I am working on a simple 3D space simulator that makes use of the Bullet physics engine. The spaceships are rigid bodies that have a mass and hitbox so that they can be considered for collisions. At the same time, I am trying to implement simple steering A.I. as detailed in numerous tutorials across the web. However, all of these tutorials assume zero-size points in 2D space with no orientation.

For the most part, I have succeeded in implementing the behaviors by applying a force to the rigid body in the direction of desired steering. The spaceship moves, turns, and accelerates smoothly and correctly navigates to the desired position in 3D space:

currentVelocity = body.getLinearVelocity();
tempVector = body.getWorldTransform().getTranslation();
desiredVelocity = targetPosition.sub(tempVector).normalize().scale(maxVelocity);
steeringForce = desiredVelocity.sub(currentVelocity);

body.setGravity(steeringForce); *[see footnote]

However, the spaceship does not change its orientation at all. The problem I am trying to solve is simple - to make the spaceship face in the direction that it is currently travelling.

From my research, it seems that simply setting the orientation of the object to face the direction of its velocity is really bad when using a physics engine because it may cause a rigid body to become stuck inside another one. On the other hand, using a more realistic navigation system based on thrusters is prohibitively (and needlessly) complex because the number of spaceships will be very high.

What is the best way of overcoming this problem?

Edit: Also of note is that the Bullet physics library does not allow setting the orientation of a body.

**Note: I used the function setGravity() instead of applyForce() as suggested by an admin at the Bullet physics forums because applying a force is dependent upon frame-rate. On the other hand, gravity is applied evenly according to Bullet's simulation steps, not to mention that gravity otherwise unused in a space simulator.*

\$\endgroup\$
7
  • 1
    \$\begingroup\$ Exactly how expensive would the solution involving thrusters be? I assume what you mean by thrusters is force applied relative to the object's orientation. \$\endgroup\$ Commented Mar 16, 2014 at 23:57
  • \$\begingroup\$ @awsumpwner27 To be honest, I'm not sure. The amount of literature available on the internet relating to implementing a en.wikipedia.org/wiki/Reaction_control_system A.I. for video games seems to be nearly non-existent and seems to be much too complex for a simple simulation. I just want the ships to be able to move like fighters in the game "Homeworld" yet still be susceptible to unpredictable forces from the physics engine that may throw off their navigation. \$\endgroup\$
    – Dashto
    Commented Mar 17, 2014 at 0:14
  • \$\begingroup\$ Duplicate of gamedev.stackexchange.com/questions/70128/… ? \$\endgroup\$
    – API-Beast
    Commented Mar 17, 2014 at 13:26
  • \$\begingroup\$ @Mr. Beast I read that question already and it is not very applicable to this one because a spaceship initiates its own motion and is not subject to drag forces nor friction. \$\endgroup\$
    – Dashto
    Commented Mar 17, 2014 at 17:58
  • \$\begingroup\$ Without drag forces it's absolutely impossible to do anything but large orbiting movment, that won't be very fun for the player. \$\endgroup\$
    – API-Beast
    Commented Mar 17, 2014 at 18:42

1 Answer 1

4
\$\begingroup\$

As a mechanical engineer whose also interested in game development I appreciate how difficult this problem can become; anything more than the naive solution I am about to describe requires a sound understanding of control systems.

Simple Solution

I am developing a 2d game where players have control over a spaceship (approach should work just as well for AI ships in 3d). I can get the ship to smoothly change directions if I apply the force (thrust vector) to a point slightly in front of the center of mass of the ship:

ship movement

The above gif is just me pressing combinations of ASDF to change the desired heading. When the game updates it applies a pre-defined thrust force to the ship's 'thrust point' in the direction of the heading vector. This is what the white line is showing.

If and when you implement this you can scale back the thrust force as the ship gets closer to the destination as you have already been doing.

Notice that, at the end of the gif, I do a ~180 degree turn to face downward. This shows (IMO) the only problem with this approach: the ship rotates slowly at first, then 'snaps' downward and overshoots the desired angle. The true behaviour of a ship with thrusters would be to circle around and gradually orient the ship towards the desired heading, which can't be achieved with this approach. For my game this departure from realism is acceptable, but for you it may not be.

This system can be tuned by changing:

  • The thrust magnitude
  • The moment of inertia of the ship (density and/or shape)
  • The angular damping coefficient of the ship
  • The linear damping (drag) coefficient of the ship
  • The distance between the 'thrust point' and the center of mass

Here ends the core of my answer. However, I feel the following discussion is necessary to shed light on the complexity of a more sophisticated control system.

Sophisticated Approach

When working with basic (single-input, single-output (SISO)) control systems, it is often sufficient to manually tune the critical parameters, such as the proportional, integral, and derivative gains manually by changing relevant properties (density, thrust power, angular damping, etc...) manually to subjectively determine what 'feels right'.

The current problem has multiple output variables: 2 angles describing the orientation, and 3 vector components describing position. To maneuver in 3d space you must have at least three independently functioning thrusters (multiple input variables). This is what is known as a multiple-input, multiple-output (MIMO) system. When working with MIMO systems, it is possible adjust input parameters 'by feel' as before without knowing mathematical relationships between the input and output variables, but there are two issues (that I can see):

  1. The number of control parameters can become very large. For each parameter you add, you can spend exponentially more time figuring out how changing each parameter affects the output variables given various combinations of the other parameters.
  2. Some (or all) of the control parameters will influence multiple output variables (e.g. distributed mass influences both the linear and angular acceleration of the ship), so those variables will be very difficult to tune.

The wikipedia article above should give you a good intro to MIMO systems. I have also found this presentation to be a good introduction.

My Advice would be to go with something simple (just try and control one variable). I haven't found any documentations online of MIMO systems for video game design to copy and learn from. If anyone has found some resource of this nature, please comment.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .