Game Development Reference
In-Depth Information
velocity.y = MathUtils.clamp(velocity.y, -
terminalVelocity.y, terminalVelocity.y);
}
3.
Finally, make the following change to the already existing update() method:
public void update (float deltaTime) {
updateMotionX(deltaTime);
updateMotionY(deltaTime);
// Move to new position
position.x += velocity.x * deltaTime;
position.y += velocity.y * deltaTime;
}
The two new methods updateMotionX() and updateMotionY() are called on every
update cycle to calculate the next x and y components of the object's velocity in terms
of the given delta time. The calculation is done in the following three steps:
1.
If the object's velocity is not equal to zero, the object must be in motion.
Therefore, friction needs to be applied on the velocity to slow it down.
As the property of friction is meant to decrease velocity, the friction
coefficient needs to be either subtracted from positive or only added to
negative velocity values. The velocity is directly set to zero as soon as
the algebraic sign changes to fully stop the ongoing motion using the
Math.max and Math.min functions.
2.
Next, acceleration is applied to the current velocity.
3.
Finally, it is made sure that the new velocity value will always be inside the
range of the positive and negative terminal velocity.
After both the velocity components have been updated, the displacement that
simulates the actual motion of an object is done by simply adding the new velocity
vector to the position vector that holds the last position.
Creating the gold coin object
The gold coin game object consists of only one image. It is a collectible item, which
means that it can be collected by the player's character by simply walking over it. As
a result of the gold coin being collected, the object will turn invisible for the rest of
the game, as shown here:
 
Search WWH ::




Custom Search