Projectile Design - UQdeco2800/2022-studio-2 GitHub Wiki

Back to Skills

Projectile Code

Projectile Direction and Rotation

The projectiles were designed to be able to be spawned at an angle rotated from the player's movement direction. This allows for the projectiles to be aimed by the player (if fired with no angle), or to spray projectiles in any direction relative to the direction the player is facing. This was implemented below within the playerfactory class.

/**
* Creates a non-colliding projectile shooting in the walk direction of the player
* which damages enemies.
* @param player the player entity
* @param angle the angle in multiples of pi radians, angle = 2 = 360deg (2pi radians)
*              from the walk direction of the player
* @return the projectile entity
*/
public static Entity createBasePlayerProjectile(Entity player, double angle)

Dynamic Assignment of Projectile Animation Direction

This was implemented through checking the angle of the direction vector of the projectile, and assigning the animation for that angle to the projectile. This was done through a range of angles (at 45deg incremenets from 0 to 360deg), and so if a projectile is spawned at any angle a sprite that visually represents the direction of the projectile movement will be assigned. If a this is used for a projectile sprite sheet, the animations should include: "right", "upright", "up", "upleft", "left", "downleft", "down", "downright", directions for the corresponding directions of the projectile movement. These animations are assigned through the function below in projectile factory:

private static void setAnimationDirection(double angle, AnimationRenderComponent animator)

The vector angle in this calculation was also measured as a rotation from the positive x axis in the counterclockwise direction, calculated from the x and y directions of the vector. This was done for consistency, as using the arctan of the two x and y components gives results from pi radians to -pi radians, which is difficult to interpret and confusing for users. This angle is calculated in the below method:

private static double getVectorAngle(Vector2 vector)