Coding the physics engine (without collision)

The PhysicsEngine class is responsible, first, for updating all the game objects and secondly for detecting and responding to collisions. This next code handles updating the game objects in the update method and we will code an empty detectCollisions method ready to add more code in Chapter 25.

Add the PhysicsEngine class and then we will discuss the code.

import android.graphics.PointF; import android.graphics.RectF; import java.util.ArrayList; class PhysicsEngine { void update(long fps, ArrayList<GameObject> objects, GameState gs) { for (GameObject object : objects) { object.update(fps, objects.get(LevelManager.PLAYER_INDEX) .getTransform()); } detectCollisions(gs, objects); } private void detectCollisions(GameState ...

Get Learning Java by Building Android Games - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.