Game Development Reference
In-Depth Information
2. The constructor takes three of these (
size
,
speed
, and
orbit
) as the input, as
shown in the following code:
public StellarBody(float orbit, float speed, float
size)
3. We override the
setSpatial
method and add
RigidBodyControl
to the
supplied spatial with
SphereCollisionShape
, using
size
as the radius and
0
for mass:
RigidBodyControl rigidBody = new RigidBodyControl(new
SphereCollisionShape(size), 0f);
rigidBody.setGravity(Vector3f.ZERO);
spatial.addControl(rigidBody);
4. In the
controlUpdate
method, we make it move along its orbit by increasing
the speed of the cycle by multiplying it by
tpf
, as follows:
cycle += (speed * tpf) % FastMath.TWO_PI;
5. Then, we set the actual position of the planet along the orbit using the
sin
and
cos
methods of the
FastMath
class:
float x = FastMath.sin(cycle);
float z = FastMath.cos(cycle);
6. We multiply the result by the orbit and set
localTranslation
of the spatial
to the new location as follows:
spatial.setLocalTranslation(x * orbit, 0, z * orbit);
7. Then, we also need to set
physicsLocation
of
RigidBodyControl
to the
same location.
8. We need a new method,
getGravity
, that will take the position of the ship as
an input
Vector3f
.
9. The method begins by subtracting the input position by
worldTranslation
,
to get the position of the ship relative to the
StellarBody
class, as follows:
Vector3f relativePosition =
spatial.getWorldTranslation().subtract(position);