32
Principles of Computer Game Design and Implementation Lecture 11

Principles of Computer Game Design and Implementationxiaowei/game_materials/lecture11.pdfLecture 11. We already learned •Vector operations –Sum –Subtraction ... state of the

  • Upload
    others

  • View
    5

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Principles of Computer Game Design and Implementationxiaowei/game_materials/lecture11.pdfLecture 11. We already learned •Vector operations –Sum –Subtraction ... state of the

Principles of Computer Game Design and Implementation

Lecture 11

Page 2: Principles of Computer Game Design and Implementationxiaowei/game_materials/lecture11.pdfLecture 11. We already learned •Vector operations –Sum –Subtraction ... state of the

We already learned

• Vector operations– Sum – Subtraction– Dot product – Cross product– A few others about jMonkey, eg. User input,

camera, etc

2

Page 3: Principles of Computer Game Design and Implementationxiaowei/game_materials/lecture11.pdfLecture 11. We already learned •Vector operations –Sum –Subtraction ... state of the

Outline for Today

• jMonkey Bits • Collision detection – overlap test and

intersection test

3

Page 4: Principles of Computer Game Design and Implementationxiaowei/game_materials/lecture11.pdfLecture 11. We already learned •Vector operations –Sum –Subtraction ... state of the

jMonkeEngine Bits and Bobs

Page 5: Principles of Computer Game Design and Implementationxiaowei/game_materials/lecture11.pdfLecture 11. We already learned •Vector operations –Sum –Subtraction ... state of the

Computer Games…

• … are not just about graphics and entity manipulation. One needs (among other things)– Camera control– Keyboard input– Mouse events– Text info– Textures and materials– Audio

We are going to look at these issues

5

Page 6: Principles of Computer Game Design and Implementationxiaowei/game_materials/lecture11.pdfLecture 11. We already learned •Vector operations –Sum –Subtraction ... state of the

Just the Bare Minimum

• Much more information can be found on the jMonkeyEngineweb site & in the Book

• Examples are based on jMEtests and tutorials

6

Page 7: Principles of Computer Game Design and Implementationxiaowei/game_materials/lecture11.pdfLecture 11. We already learned •Vector operations –Sum –Subtraction ... state of the

Simple Camera

Minimal

distance

Position of camera

Direction of looking

Maximal

distance

Viewing angle

Page 8: Principles of Computer Game Design and Implementationxiaowei/game_materials/lecture11.pdfLecture 11. We already learned •Vector operations –Sum –Subtraction ... state of the

HelloCameracam.setFrustumPerspective(45.0f, (float)settings.getWidth() / (float)settings.getHeight(), 1f, 100f);

cam.setLocation(new Vector3f(10, 10, 10));

cam.lookAt(Vector3f.ZERO,Vector3f.UNIT_Y);

Look atDirection “Up”

Camera location

8

Viewing angle

Min distance Max distance

Aspectratio

Page 9: Principles of Computer Game Design and Implementationxiaowei/game_materials/lecture11.pdfLecture 11. We already learned •Vector operations –Sum –Subtraction ... state of the

Key & Mouse Bindings

• Events are mapped to triggers• Triggers call action/analogue listeners• Action/analogue listeners are called from the

main loop

9

Page 10: Principles of Computer Game Design and Implementationxiaowei/game_materials/lecture11.pdfLecture 11. We already learned •Vector operations –Sum –Subtraction ... state of the

Example ActionListenerprivate ActionListener actionListener = new ActionListener(){

public void onAction(String name, boolean pressed, float tpf) {

if(name.equals("Move right")){

gBox.move(5*tpf,0,0);

}

else if(name.equals("Move left")) {

gBox.move(-5*tpf,0,0);

}

}

10

Page 11: Principles of Computer Game Design and Implementationxiaowei/game_materials/lecture11.pdfLecture 11. We already learned •Vector operations –Sum –Subtraction ... state of the

Sample AnalogListenerprivate AnalogListener analogListener = new AnalogListener() {

public void onAnalog(String name, float value, float tpf) {

if(name.equals("Move right")){

gBox.move(5*tpf,0,0);

}

else if(name.equals("Move left")) {

gBox.move(-5*tpf,0,0);

}

11

Page 12: Principles of Computer Game Design and Implementationxiaowei/game_materials/lecture11.pdfLecture 11. We already learned •Vector operations –Sum –Subtraction ... state of the

Deceleration

• We will look in more detail later, but for now– Simulate a slowing ball motion

12

Page 13: Principles of Computer Game Design and Implementationxiaowei/game_materials/lecture11.pdfLecture 11. We already learned •Vector operations –Sum –Subtraction ... state of the

HelloDecelerationpublic class Example07 extends SimpleApplication {

Vector3f direction = new Vector3f(1,0,0);float speed = 5;Geometry gBox;

… … … protected void simpleUpdate() {

speed -= 2*tpf;if(speed < 0.01f) {

speed = 0;}

gBox.move(direction.mult(boxSpeed*tpf)); }

13

Direction of motion

Velocity

Reduce the speed gradually

Make sure it zeroes

Page 14: Principles of Computer Game Design and Implementationxiaowei/game_materials/lecture11.pdfLecture 11. We already learned •Vector operations –Sum –Subtraction ... state of the

User Control V Modelling

• In these examples, user controlled completely the state of the world or there was no user input. – How to mix user control and physical modelling?

• Game states

User Auto

Motion simulation stops

User initiates motion simulationUser controlsthe world

14

Page 15: Principles of Computer Game Design and Implementationxiaowei/game_materials/lecture11.pdfLecture 11. We already learned •Vector operations –Sum –Subtraction ... state of the

Game States

• jME3 provides good support for game states• We use a simple switch operator

– enum State {user, auto};

– State state = State.auto;

15

Page 16: Principles of Computer Game Design and Implementationxiaowei/game_materials/lecture11.pdfLecture 11. We already learned •Vector operations –Sum –Subtraction ... state of the

simpleUpdatepublic void simpleUpdate(float tpf) {

switch(state) {

case auto:

boxSpeed -= 2*tpf;

if(boxSpeed < 0.01f) {

boxSpeed = 0;

state = State.user;

}

gBox.move(direction.mult(boxSpeed*tpf));

}

}

16

User Auto

Motion simulation stops

User initiates motion simulationUser controlsthe world

Page 17: Principles of Computer Game Design and Implementationxiaowei/game_materials/lecture11.pdfLecture 11. We already learned •Vector operations –Sum –Subtraction ... state of the

onActionpublic void onAction(String name, boolean isPressed, float tpf){switch(state) {case user:if(name.equals("Move right")){boxSpeed = 5;direction = new Vector3f(1,0,0);state = State.auto;

}else if(name.equals("Move left")) {boxSpeed = 5;direction = new Vector3f(-1,0,0);state = State.auto;

}break;

case auto:// do nothing

}}

17

Page 18: Principles of Computer Game Design and Implementationxiaowei/game_materials/lecture11.pdfLecture 11. We already learned •Vector operations –Sum –Subtraction ... state of the

Text FieldsguiFont =

assetManager.loadFont("Interface/Fonts/Default.fnt");

BitmapText text = new BitmapText(guiFont);

text.setSize(guiFont.getCharSet().getRenderedSize());text.move(settings.getWidth() / 2 + 50,

text.getLineHeight() + 20,0);

text.setText("Ha ha ha!");guiNode.attachChild(text);

18

Page 19: Principles of Computer Game Design and Implementationxiaowei/game_materials/lecture11.pdfLecture 11. We already learned •Vector operations –Sum –Subtraction ... state of the

Collisions• Collision detection– Do moving entities collide?– Mostly geometry and algorithms

• Collision response– How to react to a collision– Mostly physics

• One of common tasks in game development– Source of errors and “glitches”

19

Page 20: Principles of Computer Game Design and Implementationxiaowei/game_materials/lecture11.pdfLecture 11. We already learned •Vector operations –Sum –Subtraction ... state of the

Video Evidence

• Add a youtube video showing the error of collision

• https://www.youtube.com/watch?v=mYhNvOg5yJ0

20

Page 21: Principles of Computer Game Design and Implementationxiaowei/game_materials/lecture11.pdfLecture 11. We already learned •Vector operations –Sum –Subtraction ... state of the

Static vs Dynamic Objects

• Static objects don’t move; dynamic objects do

• Collision between a static anddynamic objects– Easier

• Collision between two (or more) dynamic objects – Harder

21

Page 22: Principles of Computer Game Design and Implementationxiaowei/game_materials/lecture11.pdfLecture 11. We already learned •Vector operations –Sum –Subtraction ... state of the

Collision Detection: The Problem

• For moving objects– Did/will they collide? (bullet and target) – When did/will they collide? (cars)– First collision / all collisions (snooker balls / bricks)– Compute the collision normal vector (for response)

– Depends on the game

Given speed, shape, and time

?

22

Page 23: Principles of Computer Game Design and Implementationxiaowei/game_materials/lecture11.pdfLecture 11. We already learned •Vector operations –Sum –Subtraction ... state of the

Main LoopNaïve approach:

for(i=0;i<num_obj-1;i++)for(j=i+1;j<num_obj;j++)

if(collide(i,j)){react;

}

• Issues:– How– Can be very slow

23

Start

Initialise

Update Game

Draw Scene

Are we done?

Cleanup

End

Page 24: Principles of Computer Game Design and Implementationxiaowei/game_materials/lecture11.pdfLecture 11. We already learned •Vector operations –Sum –Subtraction ... state of the

Collision Detection: How

Two basic techniques• Overlap testing– Detecting whether a collision has already occurred– Most common technique

• Intersection testing– Predicting a collision

?

?

24

Page 25: Principles of Computer Game Design and Implementationxiaowei/game_materials/lecture11.pdfLecture 11. We already learned •Vector operations –Sum –Subtraction ... state of the

B B

t1

t0.375

t0.25

B

t0

Iteration 1Forward 1/2

Iteration 2Backward 1/4

Iteration 3Forward 1/8

Iteration 4Forward 1/16

Iteration 5Backward 1/32

Initial OverlapTest

t0.5t0.4375 t0.40625

BB B

A

A

A

A A A

25

Overlap Testing:Collision Time

• Collision time can be calculated by moving object “back in time” until right before collision– Bisection is an effective technique

Page 26: Principles of Computer Game Design and Implementationxiaowei/game_materials/lecture11.pdfLecture 11. We already learned •Vector operations –Sum –Subtraction ... state of the

26

Limitations

• Fails with objects that move too fast– Unlikely to catch time slice during overlap

t0t-1 t1 t2bullet

window

Leads to interpenetration and tunnelling

Page 27: Principles of Computer Game Design and Implementationxiaowei/game_materials/lecture11.pdfLecture 11. We already learned •Vector operations –Sum –Subtraction ... state of the

Glitches in Games

• Players/objects falling through • Projectiles passing through targets• Players getting where they should not get• Players missing a trigger boundary

Hard to preventdue to the

discrete motion

Caused by faults in collision detection

27

Page 28: Principles of Computer Game Design and Implementationxiaowei/game_materials/lecture11.pdfLecture 11. We already learned •Vector operations –Sum –Subtraction ... state of the

Possible Solutions

• Possible solutions:– Design constraint on speed of objects

• May not always be feasible (bullets, etc.)

– Reduce simulation step size• Hardware limitations, odd shapes

– Intersection testing

28

Page 29: Principles of Computer Game Design and Implementationxiaowei/game_materials/lecture11.pdfLecture 11. We already learned •Vector operations –Sum –Subtraction ... state of the

29

Intersection Testing

• Predict future collisions• When predicted:– Move simulation to time of collision– Resolve collision– Simulate remaining time step

• Assume constant speed (over some time)– Ideal for dynamic-static object collision

Page 30: Principles of Computer Game Design and Implementationxiaowei/game_materials/lecture11.pdfLecture 11. We already learned •Vector operations –Sum –Subtraction ... state of the

Example: Moving Sphere

• Extrude geometry in direction of movement– sphere turns into a �capsule� shape

• Then, test for overlap!

t0

t1

30

Page 31: Principles of Computer Game Design and Implementationxiaowei/game_materials/lecture11.pdfLecture 11. We already learned •Vector operations –Sum –Subtraction ... state of the

31

Limitations• Issue with networked games– Future predictions rely on exact state of world at present

time– Due to packet latency, current state not always coherent

• Assumes constant velocity and zero acceleration over simulation step– Has implications for physics model

Page 32: Principles of Computer Game Design and Implementationxiaowei/game_materials/lecture11.pdfLecture 11. We already learned •Vector operations –Sum –Subtraction ... state of the

Making It Work

• It is not feasible to test for every pair of entities if they collide– N2 tests

• Therefore, usually we consider– Detailed view (colliding triangles and meshes)– Mid-level view (simplified geometry)– Global view (data structures to partition the

entities)

32