Skip to content

Chapter 3

Sandra Moen edited this page May 13, 2019 · 5 revisions

Extended Starfish Collector

This chapter extends the basic model from chapter 2 with more complex mechanics seen in many games.

Chapter 3 gameplay

Animation

Value-Based Animations uses a single image and continuously changes associated values (such as position or rotation). These effects can easily be added to the game by using LibGDX's Action class.

animation of square rotating and changing color animation of circle fading animation of square growing and shrinking

Image-Based Animations is created from images that are rapidly displayed in sequence to create the illusion of movement. In LibGDX this is accomplished by using the Animation class.

spritesheet of mario walking

Texture Filtering

In computer graphics, texture filtering or texture smoothing is the method used to determine the texture color for a texture mapped pixel, using the colors of nearby texels (pixels of the texture). There are two main categories of texture filtering, magnification filtering, and minification filtering.

Texture filtering: nearest vs Linear Texture filtering: nearest vs Linear

Physics and Movement

Velocity

How an object's position (speed and direction) changes over time.

  • Position (x, y) [m]
  • velocity <x, y> [m/s]

figure of a vector

The image above is a visual representation of the vector <Vx, Va> with length V and direction angle ø. LibGDX uses its own Vector2 and Mathutils libraries to handle vector calculations.

Acceleration

Acceleration represents how fast velocity is changing

  • Position (x, y) [m]
  • Velocity <x, y> [m/s]
  • Acceleration <x, y> [m/(s^2)]

If an object's initial velocity is <a, b> and there is a constant acceleration of <c, d>, then after t seconds, the formula at that time is <a + tc, b + td>

Movement

Movement is implemented by the BaseActor.applyPhysics and keys are set in Turtle.kt

Collision detection

All BaseActors have their default collision box set to a rectangle.

default_collision_box-png

A custom collision box is supported, based on the BaseActor's width and height and dependent on the number of points chosen will approximate roundness. E.g. an actor with a width that equals its height will be an octagon if the number of sides chosen is eight.

octagon

Note that LibGDX's Polygon collision algorithm only supports convex polygons.

convex vs. concave polygon

Relationships between the classes in the final version of the Starfish Collector game. There are four main groups of classes involved in creating a game with this framework: the Launcher, the BaseGame class, and its extensions, the BaseScreen class and its extensions, and the BaseActor class and its extensions.

Relationships between the classes in the final version of the Starfish Collector game

New Imports

import com.badlogic.gdx.utils.array - A resizable, ordered or unordered array of objects. If unordered, this class avoids a memory copy when removing elements (the last element is moved to the removed element's position).

import com.badlogic.gdx.graphics.Color - A color class, holding the r, g, b and alpha component as floats in the range [0,1]. All methods perform clamping on the internal values after execution.

import com.badlogic.gdx.texture.TextureFilter - In computer graphics, texture filtering or texture smoothing is the method used to determine the texture color for a texture mapped pixel, using the colors of nearby texels (pixels of the texture). There are two main categories of texture filtering, magnification filtering and minification filtering.

import com.badlogic.gdx.g2d.Animation - An Animation stores a list of objects representing an animated sequence, e.g. for running or jumping. Each object in the Animation is called a key frame, and multiple key frames make up the animation.

import com.badlogic.gdx.g2d.Animation.PlayMode - Defines possible playback modes for an Animation.

import com.badlogic.gdx.math.Vector2 - Encapsulates a 2D vector. Allows chaining methods by returning a reference to itself

import com.badlogic.gdx.math.MathUtils - Utility and fast math functions.

import com.badlogic.gdx.graphics.Camera - Base class for OrthographicCamera and PerspectiveCamera.

import com.badlogic.gdx.utils.viewport.Viewport - Manages a Camera and determines how world coordinates are mapped to and from the screen.

import com.badlogic.gdx.Screen - Represents one of many application screens, such as a main menu, a settings menu, the game screen and so on.

import com.badlogic.gdx.scenes.scene2d.Action - Actions attach to an Actor and perform some task, often over time.

import com.badlogic.gdx.scenes.scene2d.actions.Actions - Static convenience methods for using pooled actions, intended for static import.

Previous | Next