Asteroid

In the context of a 2D game world, an asteroid is represented as a polygon. This representation is crucial for determining interactions with other game elements, such as a laser beam fired by the player's spaceship. The primary task is to determine if the laser beam intersects with any of the line segments that define the asteroid's shape. This involves solving a linear algebra problem to check for intersections between the laser's path and the asteroid's edges.

Asteroid Motion and Velocity

Asteroids are given random velocity vectors to simulate motion within the game. Each asteroid's movement is defined by velocity components ( vx ) and ( vy ), which are added as properties to the PolygonModel object. These components represent the derivatives of position with respect to time, allowing the asteroids to move continuously across the game screen.

The game engine updates the position of each asteroid by using these velocity components. The position components ( x ) and ( y ) are updated based on the elapsed time, ensuring that the asteroids move smoothly. Figure 9.2 illustrates how each asteroid moves with a random, constant velocity.

Figure 9.2 With the preceding code included, each asteroid moves with a random, constant velocity. Figure 9.2 With the preceding code included, each asteroid moves with a random, constant velocity.

Keeping Asteroids on Screen

To maintain gameplay, logic is implemented to keep asteroids on the screen. The coordinates of the asteroids are constrained between -10 and 10. If an asteroid moves beyond these bounds, it is 'teleported' to the opposite side of the screen. This technique ensures that asteroids remain in play and do not disappear off-screen.

Asteroids and Gravitational Effects

Asteroids in the game are also affected by the gravitational pull of a black hole. Depending on their initial velocity and position, asteroids can either fall into the black hole or enter an elliptical orbit around it. Figure 11.15 shows an asteroid in an elliptical orbit around the black hole.

Figure 11.15 An asteroid in another elliptical orbit around our black hole Figure 11.15 An asteroid in another elliptical orbit around our black hole

Classic Asteroids Game Setup

The game is a simple remake of the classic Asteroids arcade game, where the player controls a triangle representing a spaceship. The player fires a laser at polygons, which represent asteroids, to destroy them before they can collide with the spaceship. Figure 7.1 depicts the setup of this classic game.

Figure 7.1 Setup of the classic Asteroids arcade game Figure 7.1 Setup of the classic Asteroids arcade game

Exercises

An example exercise (Exercise 9.1) asks which direction an asteroid with the velocity vector ( v = (vx, vy) = (-3, 1) ) is moving. The solution indicates that the asteroid is moving up and to the left, demonstrating the application of velocity vectors in determining movement direction.

FAQ (Frequently asked questions)

How is an asteroid represented in the game world?

How are asteroids given motion in the game?

What happens to asteroids when they move out of bounds?

How do asteroids move in the game?

In which direction does an asteroid with velocity vector v = (-3, 1) move?

What determines whether an asteroid falls into the black hole or orbits it?