Presentation is loading. Please wait.

Presentation is loading. Please wait.

Movement (Paul Klee Signatures)

Similar presentations


Presentation on theme: "Movement (Paul Klee Signatures)"— Presentation transcript:

1 Movement (Paul Klee Signatures)
CBP 2006 Comp3063

2 CBP 2006 Comp3063

3 Threads CBP 2006 Comp3063

4 Using a Thread CBP 2006 Comp3063 public void run() { while (true) {
if(running == true) { .. Call Thing’s move() here .. try { Thread.sleep(100); } catch (InterruptedException e) { System.out.println(e); return; This is an infinite loop Go further only if RUNNING suspend this thread a bit to allow other user actions CBP 2006 Comp3063

5 Game engine Physics : Speeds
Let’s say we have speed vX = 10; X = 50 X = 60 X = 70 X = 80 This says we get the new x by adding on the extra x due to the speed vX x = x + vX CBP 2006 Comp3063

6 Movement (Constant Speed)
public void step() { x = x + vX; if(x > canvas.getCanvasWidth() ) vX = -vX; if(x < 0) vX = -vX; // Something similar for the y-direction } CBP 2006 Comp3063

7 Game engine Physics : Speeds
x y x = x + vX y = y + vY vX vY CBP 2006 Comp3063

8 Bouncing vX vX unchanged vY vY = -vY vX vY Before collision
After collision CBP 2006 Comp3063

9 How to make a “Trap” for balls ?
Bouncing (Soft Walls) vX = 0.5*vX vY = -0.5*vY How to make a “Trap” for balls ? CBP 2006 Comp3063

10 Collision Detection -1-
boundBox Rectangle boundBox = new Rectangle(); boundBox.setRect(x,y,width,height); rectangle1 rectangle2 intersects if(boundBox.intersects(otherBoundBox)) { contact = true; } rectangle1.intersects(rectangle2) CBP 2006 Comp3063

11 Collision Detection -2-
thing1 thing2 (xC,yC) radiusThing separation double radiusThing1 = thing1.getRadius(); double radiusThing2 = thing2.getRadius(); double separation = Math.sqrt( (xCThing1 - xCThing2)*(xCThing1 - xCThing2) + (yCThing1 - yCThing2)*(yCThing1 - yCThing2) ); if ( separation <= (radiusThing1 + radiusThing2) ) collision = true; else collision = false; CBP 2006 Comp3063

12 Philosophy of Interactions
game objects will collide so each must get info where others are game objects ask each other gObj - Or - game objects will collide so each must get info where others are gameCanvas mediates this info canvas CBP 2006 Comp3063

13 Message Passing in DrawCanvas
Iterator it = gameObjectList.iterator(); while(it.hasNext()) GameObject sourceGobj = (GameObject) it.next(); Iterator it2 = gameObjectList.iterator(); while(it2.hasNext()) { GameObject targetGobj = (GameObject) it2.next(); if( targetGobj != sourceGobj) { sourceGobj.xchangeImplicitMessage(targetGobj); } CBP 2006 Comp3063

14 Interact Then Step DrawCanvas Class Send interaction Messages
while (true) { Iterator it = gameObjectList.iterator(); while(it.hasNext()) { GameObject sourceGobj = (GameObject) it.next(); Iterator it2 = gameObjectList.iterator(); while(it2.hasNext()) { GameObject targetGobj = (GameObject) it2.next(); if( targetGobj != sourceGobj) { sourceGobj.xchangeImplicitMessage(targetGobj); } Iterator it3 = gameObjectList.iterator(); while(it3.hasNext()) { GameObject sourceGobj = (GameObject) it3.next(); sourceGobj.step(); Send interaction Messages Call step() to Update positions DrawCanvas Class CBP 2006 Comp3063

15 GameObject class Get interaction Messages step() updates position
public void xchangeImplicitMessage(GameObject otherGobj) { isCollided = isCollision(otherGobj); if(isCollided == true) { collidedGobj = otherGobj; System.out.println("GameObject:: "+otherGobj+" collided w "+this); } Get interaction Messages public void step() { if( collidedGobj != null) { respondToCollision(collidedGobj); } x += vX; y += vY; boundBox.setRect(x,y,width,height); collidedGobj = null; step() updates position GameObject class CBP 2006 Comp3063

16 Sequence Diagram for Messaging
CBP 2006 Comp3063

17 Adding JComponents to the GUI
public Target(double x, double y, double wwidth, double hheight, DrawCanvas canvas){ super(x,y,canvas); this.width = wwidth; this.height = hheight; color = Color.green; boundBox.setRect(x,y,width,height); score = 0; JLabel label1 = new JLabel("Score"); scoreField = new JTextField("0",10); JPanel p1 = new JPanel(); p1.add(label1); p1.add(scoreField); canvas.gui.addScoreBox(p1); } Make GUI component CBP 2006 Comp3063


Download ppt "Movement (Paul Klee Signatures)"

Similar presentations


Ads by Google