1 / 17

Movement (Paul Klee Signatures )

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) {

reuben
Download Presentation

Movement (Paul Klee Signatures )

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Movement (Paul Klee Signatures) Comp3063

  2. Comp3063

  3. Threads Comp3063

  4. 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

  5. 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

  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 } Comp3063

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

  8. Bouncing vX vX unchanged vY vY = -vY vX vY Before collision After collision Comp3063

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

  10. 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

  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; 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 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); } } } Comp3063

  14. 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

  15. 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

  16. Sequence Diagram for Messaging 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 Comp3063

More Related