Platform Games Drawing Sprites & Detecting Collisions

Similar documents
Adding Depth to Games

Collisions/Reflection

[ the academy_of_code] Senior Beginners

Smoother Graphics Taking Control of Painting the Screen

Pong in Unity a basic Intro

Chapter 8: Implementation- Clipping and Rasterization

February 18, Nintendo. Bob Rost January 14, 2004

To get a copy of this image you right click on the image with your mouse and you will get a menu. Scroll down the menu and select "Save Image As".

Computer Games Development Spring Practical 3 GameCore, Transforms & Collision Detection

Anima-LP. Version 2.1alpha. User's Manual. August 10, 1992

Erasmus+ Project: Yestermorrow Year 1 Maths: Pythagorean Theorem

AN INTRODUCTION TO SCRATCH (2) PROGRAMMING

UI Elements. If you are not working in 2D mode, you need to change the texture type to Sprite (2D and UI)

Velocity: A Bat s Eye View of Velocity

Clip Art and Graphics. Inserting Clip Art. Inserting Other Graphics. Creating Your Own Shapes. Formatting the Shape

Measuring Triangles. 1 cm 2. 1 cm. 1 cm

Bridges To Computing

Announcements. Midterms graded back at the end of class Help session on Assignment 3 for last ~20 minutes of class. Computer Graphics

InfoSphere goes Android Flappy Bird

Working with the BCC 2D Particles Filter

Exploring Projectile Motion with Interactive Physics

How to draw and create shapes

How to create shapes. Drawing basic shapes. Adobe Photoshop Elements 8 guide

Working with the BCC 3D Image Shatter Filter

Overview of Synaptics TouchPad Features

CS 325 Computer Graphics

Scaled representations

Robotics Project. Final Report. Computer Science University of Minnesota. December 17, 2007

SHAPES & TRANSFORMS. Chapter 12 of Pro WPF : By Matthew MacDonald Assist Lect. Wadhah R. Baiee. College of IT Univ.

Web-Friendly Sites. Planning & Design 1

5/27/12. Objectives. Plane Curves and Parametric Equations. Sketch the graph of a curve given by a set of parametric equations.

(C) 2010 Pearson Education, Inc. All rights reserved. Omer Boyaci

10.1 Overview. Section 10.1: Overview. Section 10.2: Procedure for Generating Prisms. Section 10.3: Prism Meshing Options

GstarCAD Complete Features Guide

Displacement-time and Velocity-time Graphs

Einführung in Visual Computing

Accepting that the simple base case of a sp graph is that of Figure 3.1.a we can recursively define our term:

Calibrating SightSensors

Add in a new balloon sprite, and a suitable stage backdrop.

BCC Particle System Generator

DOING MORE WITH WORD: MICROSOFT OFFICE 2010

LAB 1: INTRODUCTION TO DATA STUDIO AND ONE-DIMENSIONAL MOTION

ACS-1805 Introduction to Programming (with App Inventor)

1 Getting started with Processing

Short-Cut MCMC: An Alternative to Adaptation

Session 6. Microsoft and The DigiPen Institute of Technology Webcast Series

Notes 3: Actionscript to control symbol locations

Computational Geometry

An Approach to Content Creation for Trainz

Universiteit Leiden Computer Science

TEAM 12: TERMANATOR PROJECT PROPOSAL. TEAM MEMBERS: Donald Eng Rodrigo Ipince Kevin Luu

Creating Breakout - Part 2

Tutorial 4: Texture Mapping Techniques

QUESTIONS ACTIONS IN GAMEMAKER

Shadows for Many Lights sounds like it might mean something, but In fact it can mean very different things, that require very different solutions.

Computer Science 474 Spring 2010 Viewing Transformation

Unit 21 - Creating a Navigation Bar in Macromedia Fireworks

LECTURE 4. Announcements

15-494/694: Cognitive Robotics

Use lists. Use loops. Use conditionals. Define and use functions. Create and use code modules

Chapter 19- Object Physics

Load your files from the end of Lab A, since these will be your starting point.

View Frustum Culling with Octree

How to create interactive documents

Exit: These control how the object exits the slide. For example, with the Fade animation the object will simply fade away.

DOING MORE WITH WORD: MICROSOFT OFFICE 2013

Real Time Cloth Simulation

Vertex Cover Approximations

Overview. Animation is a big topic We will concentrate on character animation as is used in many games today. humans, animals, monsters, robots, etc.

Working with the BCC Z Space I Filter

CS 354R: Computer Game Technology

Topic #1: Rasterization (Scan Conversion)

HYPERSTUDIO TOOLS. THE GRAPHIC TOOL Use this tool to select graphics to edit. SPRAY PAINT CAN Scatter lots of tiny dots with this tool.

Template Matching Rigid Motion. Find transformation to align two images. Focus on geometric features

Photocopiable/digital resources may only be copied by the purchasing institution on a single site and for their own use ZigZag Education, 2013

Structured Programming Using C++ Lecture 10 : Graphics Programming with the Dark GDK Library. Dr. Amal Khalifa. Lecture Contents:

Lab 2: Conservation of Momentum

COS 226 Lecture 15: Geometric algorithms. Warning: intuition may mislead (continued)

Math 443/543 Graph Theory Notes 10: Small world phenomenon and decentralized search

Adobe Flash CS4 Part 4: Interactivity

Tutorial: Overview. CHAPTER 2 Tutorial

SlickEdit Gadgets. SlickEdit Gadgets

Making use of other Applications

Sign up for crits! Announcments

Figure 2.1: High level diagram of system.

Making a maze with Scratch

Navigation and Metric Path Planning

Three OPTIMIZING. Your System for Photoshop. Tuning for Performance

Introduction to Computers & Programming

Discrete search algorithms

Collaboration policies!

Adobe Photoshop Sh S.K. Sublania and Sh. Naresh Chand

V-BOX Cloud Configuration

DOING MORE WITH WORD: MICROSOFT OFFICE 2007

Adobe Flash CS4 Part 3: Animation

9. a. 11. A = 30 cm 2, P = 30 cm. 12. A = 29 cm 2, P = 13. A = 72 in. 2, P = 35 in.

Guide to WB Annotations

Fruit Snake SECTION 1

AN AIMING POINT METHOD FOR POOL Don Smith November, 2009

Transcription:

Platform Games Drawing Sprites & Detecting Collisions Computer Games Development David Cairns Contents Drawing Sprites Collision Detection Animation Loop Introduction 1

Background Image - Parallax Scrolling A static background image is a bit dull, speeding up and slowing down the background image and tiles produces a good sense of motion. If we move the background image slower than the foreground tiles, we get parallax scrolling. The background seems further away because it moves slower Racing games use this trick a lot We will not want to create a background image the size of our tile map. Instead, create an isomorphic background that is bigger than the viewable area Display the visible section as the background When the image is about to reach its edge, display the beginning again Alternatively move the background so slowly such that the leftmost side is seen at the start of a map and the rightmost side at the end of a map (DGJ, p34-p36) Multiple backgrounds, each scrolling at different speeds... 3 Create a background image larger than the viewable screen Display the relevant section Use the image as though it were on a roll, repeating itself at an edge Background Image 4

Repeating Background Image 5 Drawing Sprites Drawing Sprites We already know how to draw animated sprites The problem is knowing when to draw them Solutions Section up the play area Ordered horizontal list Unordered list 6 3

Sectioning Drawing Sprites - Sectioning Section up the game area Calculate which sections are currently visible For each visible section, draw the sprites it contains With this approach we must note when a sprite moves into a new section 7 Drawing Sprites - Ordered List Ordered List Create an ordered list of sprites where the leftmost sprite is first and the rightmost sprite is last Scan list from the left looking for first visible sprite Draw sprites from this one until the rightmost non visible sprite is reached Requires constant movement of sprite references up and down the list 8 4

Drawing Sprites - Unordered List Unordered List Maintain an unordered list of sprites Scan through the entire list every frame and work out if the sprite should be displayed Very simple, brute force approach Very inefficient for games with a large number of sprites but fine for only 10-0 sprites 9 Collisions A game which does not interact with the player would be pretty dull If a player jumps on a platform, it would be good for them to stay on it rather than fall straight through An interaction between a player sprite and the background (or a monster) is detected as a collision between the objects Stages Determine which objects to test for collisions Detect the collisions Handle the collisions 10 5

Reducing Collision Tests In a small game with only a few sprites and a tile map, you can easily just check all items for collisions. In a larger game with perhaps 100 moving objects, you would have to make 4950 comparisons per frame (n * (n-1))/ = (n -n)/ (100x99)/ = 4950 Useful Heuristics Do not test static objects, only moving objects Test moving objects that collide with static objects If an object hasn t moved since the last test, don t recheck Reduce tests to only those objects within a given area Divide game area into a grid Use an ordered list where items in proximity are close together in the list 11 Reducing Collision Tests - Grids Dividing Game Area into a Grid Keep track of which objects are in a given grid cell Test collisions for objects in current and surrounding cells The finer the grid resolution, the fewer objects that will be in each cell therefore the fewer tests you need to make Fine resolution requires larger memory requirement There is a general overhead in tracking objects in cells which becomes more beneficial the more objects you have. 1 6

Collision Detection Bounding Box Once we have worked out which objects to test for collisions, we must do the collision test This is not as simple as you might think / hope A simple test is to check the bounding boxes of two objects, however this only helps to determine if there might be a collision Collision detection usually involves making a number of increasingly detailed checks 13 Collision Detection Bounding Box Example public boolean BoundBoxCollision(Sprite sp) { // x,y are current position of this sprite // width,height are size of this sprite // We now compare this sprite s values with those passed // in via the sprite referenced by sp return ((x + width) => sp.x) && (x <= sp.x + sp.width) && (y + height) => sp.y) && (y <= sp.y + sp.height)); } 14 7

Collision Detection Bounding Circle A simple but slightly more sophisticated approach is to use a bounding circle If an initial collision is suspected, then we can test again on smaller circles, and then even smaller circles In 3D graphics this is commonly called a sphere tree Sometimes initial circles are good enough It depends if the user will notice (or get upset) if two objects do not quite visually collide The last resort is to test whether the visible pixels in each image overlap this is very time consuming and generally avoided 15 Collision Detection Bounding Circle Example public boolean BoundingCircleCollision(Sprite one, Sprite two) { int dx,dy,minimum; dx = one.x - two.x; dy = one.y - two.y; minimum = one.radius + two.radius; } return (((dx * dx) + (dy * dy)) < (minimum * minimum)); a = b + c a = b + c 16 8

Detecting collision direction Determining that there has been a collision is relatively straightforward. Working out which point has collided is a lot harder This is needed if you wish to bounce away correctly A simplified method is to check a number of points around the object and find out which is closest to the object that the collision occurred with (e.g. measure from each corner of the sprite). We can use Pythagoras theorem to measure distance between points, then select the shortest We may also wish to do this for testing collisions of sprites with the corners of tiles on our tile map 17 Collision Detection & Discrete Time In most games you will update the sprite positions at discrete points rather than move each sprite a pixel at a time. This can cause a problem! This is an even bigger issue in multiplayer games where different machines run at different speeds 18 9

Collision Detection & Discrete Time Solutions: Compute a bounding shape using the before and after object positions to form the edges Accurate but computationally expensive This shape could be simplified to a rectangle (tilted) Compute more frames than are actually displayed If we had twice the number of animation frames, a collision would be detected in the new frame between frames 5&6 in the old method 19 Collision Handling Once we have detected a collision, we must decide how to deal with it. This is usually done before the colliding objects are drawn. There are two common collision types to handle: Collisions with Sprites (e.g. power ups / baddies) Collisions with physical objects e.g. border tiles Collisions with Sprites In this scenario you will usually remove one of the two colliding sprites (the player or a power up), possibly play an animation and a sound. For example, with the player colliding with a baddie, you might change the player sprite animation to one where the player shrivels up and play a bad sound to go with it. 0 10

Collisions with Objects When a sprite collides with a background object, we may wish to: Change the position and direction of the sprite depending on the angle of incidence Move the sprite back to a pre-collision position Reverse horizontal and / or vertical velocity Alter or remove the tile / object it collided with Changing velocity (flip) 1 Collisions - Changing Position Because we step forward in discrete time intervals (rather than a pixel at a time), we will usually detect a collision when two objects have already overlapped, rather than when they have just touched each other: Our goal is to work out where the collision occurred and place the sprite back at this point... 11

Collisions - Changing Position Having detected the collision, we now move the sprite back to where it started before it moved and then move it a pixel at a time horizontally then vertically (in the direction of its current velocity), until a collision is detected. 3 The Animation Loop The animation loop is where we put all these different components together... Get user input Update sprite positions in game world Detect & handle collisions Update display Display background image Display tile map Display sprites Repeat 4 1