28
UFCEKU-20-3 Web Games Programming Web Games Programming Game Physics for Collisions and Reactions

UFCEKU-20-3Web Games Programming Game Physics for Collisions and Reactions

  • View
    218

  • Download
    3

Embed Size (px)

Citation preview

UFCEKU-20-3Web Games Programming

Web Games Programming

Game Physics for Collisions and Reactions

UFCEKU-20-3Web Games Programming

Agenda

Revise some basic physics concepts Apply concepts to game elements Collision detection techniques Programming collision reactions Game programming world boundaries

UFCEKU-20-3Web Games Programming

Physics - Some Basic Concepts

Physics of Movement Speed Acceleration Velocity Momentum Friction Inertia

Physics of Collisions Elastic Collisions - energy of colliding objects is conserved Inelastic Collisions - energy of colliding objects is converted

UFCEKU-20-3Web Games Programming

Physics -Vocabulary

Length metre (m) Mass kilogram (kg) Time sec (s) Force mass x acceleration newton (N) Area length x length m2 Volume length x length x length m3

Density mass per unit volume p (Rho) kg / m3

Pressure force per unit area pascal (Pa) N / m2

Gravity (g) determined by mass of planet 9.81 (Earth) Energy force x distance (in metres) joule (J) Nm

Quantity SI Unit

UFCEKU-20-3Web Games Programming

Physics of Motion

Speed = distance traveled / time taken Velocity

vector quantity has magnitude and direction

Acceleration = change in velocity per unit time Acceleration under gravity g = 9.81ms-2

UFCEKU-20-3Web Games Programming

Velocity

A vector quantity has magnitude and direction

Speed = 40 kph

East

North

UFCEKU-20-3Web Games Programming

Velocity Resolving Forces

Buoyant force

gravitational force

wind force

+y

+x

UFCEKU-20-3Web Games Programming

Force = Mass x Acceleration

f = ma

Mass

Acceleration

UFCEKU-20-3Web Games Programming

Motion Under Gravity All Objects Fall at the Same Rate...

UFCEKU-20-3Web Games Programming

Game Physics - ‘Good Enough’

Game physics need only be believable (virtual) Implementing real-world physics may be too resource

intensive and visually unnecessary Main visual physics effects

Speed - good frame-rate (30 FPS in Flash) Acceleration and deceleration of objects Collisions between objects Trajectories

UFCEKU-20-3Web Games Programming

Collision Detection

Detect when an on-screen game element has made contact with another on-screen game element

Collision with game world boundaries Player sprite collides with target enemy sprite Player sprite acquires an in-game resource

Collisions must be accurately detected Collision events must be handled by game logic Game state changed to reflect changes made by collisions -

update score, lives, acquire energy, add time bonus, more ammo etc.

UFCEKU-20-3Web Games Programming

Collision Detection for 3D

3D game scenarios may use bounding spheres A bounding sphere is a hypothetical sphere that

completely encompasses an object. It is defined by a 3D coordinate representing the center of the sphere, and a scalar radius that defines the maximum distance from the center of the sphere to any point in the object

Most game APIs provide some functional collision detection

UFCEKU-20-3Web Games Programming

Collision Detection in Flash

hitTestObject hitTestPoint Code your own function to find the distance between

two points ( Pythagoras)

UFCEKU-20-3Web Games Programming

Sprite Collisions

UFCEKU-20-3Web Games Programming

Sprite Bounding Boxes

UFCEKU-20-3Web Games Programming

False Collisions

Sprite boundaries overlap but sprites not colliding

UFCEKU-20-3Web Games Programming

Invisible Collisions

UFCEKU-20-3Web Games Programming

Invisible Collisions

UFCEKU-20-3Web Games Programming

Collision Reactionsx

y

-x after collision

UFCEKU-20-3Web Games Programming

More Complex Reactionsx

y

UFCEKU-20-3Web Games Programming

Game World Boundaries

600

800

Game World Area Closed

UFCEKU-20-3Web Games Programming

Game World Boundaries

600

800

Game World Area Wrapped

UFCEKU-20-3Web Games Programming

Game Logic: Wrapped World

if the sprite reaches the maximum width of the game worldthen reposition the sprite at the beginning of the game world

// assume game world is 800 x 600if (sprite.x > 800){sprite.x = 0}if (sprite.y > 400){sprite.y = 0}

UFCEKU-20-3Web Games Programming

Game World Boundaries

600

800

Game World Area Closed

UFCEKU-20-3Web Games Programming

Game Logic: Closed World

if the sprite reaches the maximum width of the game worldthen reposition the sprite at the beginning of the game world

// assume game world is 800 x 600if (sprite.x > 800){sprite.x = 800}if (sprite.y > 400){sprite.y = 400} // would need to take account of the registration point of the sprite

UFCEKU-20-3Web Games Programming

Game Logic: Closed World

Assume registration point at top left corner100

80

// assume game world is 800 x 600if (sprite.x > 800){

sprite.x = 700}

if (sprite.y > 400){sprite.y = 320

UFCEKU-20-3Web Games Programming

Extended Game World

Viewport

Game world scrolls to reveal extended boundaries

UFCEKU-20-3Web Games Programming

Further Reading

See the tutorial and investigate the methods of the DisplayObject class

hitTestObject hitTestPoint Pythagoras Also the hitTest method of the BitmapData class

Game Design Demystified book- good coverage ActionScript 3.0 University - good examples Any of the books by Andre Lamothe Stahler W. et al Beginning Math and Physics for Game

Programmers www.gamedev.net - some good articles