170 likes | 308 Views
Movement (Paul Klee Signatures ). Threads. Using a Thread. public void run() { while (true) { if(running == true) { .. Call Thing’s move() here .. try { Thread.sleep(100); } catch (InterruptedException e) {
E N D
Movement (Paul Klee Signatures) Comp3063
Threads Comp3063
Using a Thread 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 Comp3063
X = 50 X = 60 X = 70 X = 80 Game engine Physics : Speeds Let’s say we have speed vX = 10; This says we get the new x by adding on the extra x due to the speed vX x = x + vX Comp3063
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 } Comp3063
Game engine Physics : Speeds x y x = x + vX y = y + vY vX vY Comp3063
Bouncing vX vX unchanged vY vY = -vY vX vY Before collision After collision Comp3063
Bouncing (Soft Walls) vX = 0.5*vX vY = -0.5*vY How to make a “Trap” for balls ? Comp3063
rectangle1 boundBox rectangle2 intersects Collision Detection -1- Rectangle boundBox = new Rectangle(); boundBox.setRect(x,y,width,height); if(boundBox.intersects(otherBoundBox)) { contact = true; } rectangle1.intersects(rectangle2) Comp3063
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; Comp3063
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 Comp3063
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); } } } Comp3063
InteractThen Step 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 Comp3063
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 Comp3063
Sequence Diagram for Messaging Comp3063
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 Comp3063