Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 9 – Additional Scenarios. Marbles Collision Detection The Marbles scenario does not use any of the built-in Greenfoot collision detection.

Similar presentations


Presentation on theme: "Chapter 9 – Additional Scenarios. Marbles Collision Detection The Marbles scenario does not use any of the built-in Greenfoot collision detection."— Presentation transcript:

1 Chapter 9 – Additional Scenarios

2 Marbles

3

4

5 Collision Detection The Marbles scenario does not use any of the built-in Greenfoot collision detection methods, since these all work on the rectangular actor images. The Marbles Scenario on the other hand are round and the nature of the game makes precise collision necessary.

6 haveHit(Marble marble ) /* * Check whether we have hit the given marble. We have hit it if its distance from us * (measured at the centre points) is less then our diameter. */ private boolean haveHit(Marble marble) { int dx = Math.abs (this.getX() - marble.getX()); int dy = Math.abs (this.getY() - marble.getY()); double distance = Math.sqrt(dx*dx+dy*dy); return distance < DIAMETER; }

7 Collision Detection this.getX()marble.getX() marble.getY() this.getY() dx dy distance = √ (dx 2 + dy 2 ) distance

8 Boids

9

10

11 Boids Algorithm The term “Boids” comes from a program developed in 1986 by Craig Reynolds that first implemented this flocking algorithm. In it each bird flies according to three rules:  Repulsion: Steer away from other birds or objects if getting too close  Alignment: Steer toward the average heading of other birds in the vicinity  Attraction: Steer to move toward the average position of other birds in the vicinity

12 Boids Algorithm Due to the nature of the Greenfoot Scenario, the specific algorithm used also computes a fourth force:  WallForce: Try to avoid walls, with closer walls exerting a stronger force.

13 Wave

14

15

16 One of the interesting aspects of this scenario is how a fairly simple implementation achieves a quite sophisticated simulation of various aspects of wave propagation. In each act round, each bead simply moves toward the middle of its two neighbors.

17 Pengu Pengu exemplifies a very common style of game: the “platformer”. The player typically controls a game character that has to move from one are on the screen to another, while overcoming various obstacles. This scenario demonstrates how an actor can move along the top of another actor (the penguin on top of the ground), and how jumping and falling might be implemented.

18 Standing “on” Objects

19 Detecting “Ground” public boolean onGround() { Object under = getOneObjectAtOffset(0, getImage().getHeight()/2, null); return under != null; } The getOneObjectAtOffset(int dx, int dy, java.lang.Class cls) method is useful for detecting collisions in a specific location.

20 Standing “on” Objects The x-offset is 0. The y-offset is half the height of the Penguin image. This means we are detecing if the bottom of the Penguin is in contact with anything.

21 Pengu act() Method public void act() { checkKeys(); checkFall(); }

22 Pengu checkFall() Method private void checkFall() { if (onGround()) { setVSpeed(0); } else { fall(); } As long as the Penguin is on the ground, its VSpeed (vertical speed, i.e. movement on the y- axis) is set to 0.

23 Pengu checkFall() Method private void checkFall() { if (onGround()) { setVSpeed(0); } else { fall(); } As soon as the Penguin leaves the ground, it starts to fall.

24 Mover fall() Method public void fall() { setLocation ( getX(), getY() + vSpeed); vSpeed = vSpeed + acceleration; if ( atBottom() ) gameEnd(); } Falling is implemented as movement on the y- axis, with the speed of movement determined by the vSpeed variable. vSpeed increases during a fall by a set value.

25 Pengu checkKeys() Method private void checkKeys() { if (Greenfoot.isKeyDown("left") ) { setImage("pengu-left.png"); moveLeft(); } if (Greenfoot.isKeyDown("right") ) { setImage("pengu-right.png"); moveRight(); } if (Greenfoot.isKeyDown("space") ) { if (onGround()) jump(); }

26 Pengu jump() Method private void jump() { setVSpeed(-jumpStrength); fall(); } Jumping is achieved by setting the vertical speed to a negative value. The penguin this “falls upwards”.

27 Multilevel Games One way to implement multiple levels is to have one World-subclass per Level in combination with an Actor- based „level-check“ method.

28 Level1 class public class Level1 extends World { public Level1() { this (new Pengu()); } public Level1(Pengu pengu) { super(750, 500, 1); // define size and resolution addObject ( new Ground(false), 85, 441); addObject ( new Ground(true), 665, 441); addObject ( new Cloud(), 369, 315 ); addObject ( pengu, 66, 244 ); }

29 Level2 class public class Level2 extends World { public Level2() { this(new Pengu()); } public Level2(Pengu pengu) { super(750, 500, 1); addObject ( new Ground("ground2.png"), 13, 461); addObject ( new Ground("ground2.png"), 326, 345); addObject ( new Ground("ground2.png"), 55, 228); addObject ( new Ground("ground2.png"), 489, 153); addObject ( pengu, 38, 379 ); }

30 Pengu checkNextLevel() Method private void checkNextLevel() { if (getX() == getWorld().getWidth()-1) { if (currentLevel == 1) { currentLevel = 2; getWorld().removeObject(this); Greenfoot.setWorld(new Level2(this)); } else { currentLevel = 1; getWorld().removeObject(this); Greenfoot.setWorld(new Level1(this)); } We consider a level “completed” and go to the next one, once the Penguin has reach the right-hand border of the level.

31 Pengu checkNextLevel() Method private void checkNextLevel() { if (getX() == getWorld().getWidth()-1) { if (currentLevel == 1) { currentLevel = 2; getWorld().removeObject(this); Greenfoot.setWorld(new Level2(this)); } else { currentLevel = 1; getWorld().removeObject(this); Greenfoot.setWorld(new Level1(this)); } We load the new level by using the Greenfoot.setWorld(World world) method.

32 Pengu act() Method public void act() { checkKeys(); checkFall(); checkNextLevel(); }


Download ppt "Chapter 9 – Additional Scenarios. Marbles Collision Detection The Marbles scenario does not use any of the built-in Greenfoot collision detection."

Similar presentations


Ads by Google