What is the best, smallest, and most accurate (<-- most important) way of detecting collision for the Pengu class? (Please give some example code that could work here.) (The collision has to detect everything and in every way that it is below, just shorter and (more) accurate.)
This is the Mover class which Pengu extends from:
This is the Pengu class (colliding actor):
This is the Box class (collision object, has a variable in it (boolean isThere) that needs to be checked for collision):
This is the Block class (collision object, has a variable in it (boolean isThere) that needs to be checked for collision):
I didn't post the Cliff class because the only thing that it has to do with the collision is the picture.
import greenfoot.*;
/**
* The class Mover provides some basic movement methods. Use this as a superclass
* for other actors that should be able to move left and right, jump up and fall
* down.
*/
public abstract class Mover extends Actor
{
private static final int acceleration = 2; // down (gravity)
private static final int speed = 7; // running speed (sideways)
public int vSpeed = 0; // current vertical speed
private boolean hasHit=false;
public void moveRight()
{
setLocation ( getX() + speed, getY() );
}
public void moveLeft()
{
setLocation ( getX() - speed, getY() );
}
public boolean onTop()
{
return onTop1B()||onTop2B()||onTop3B();
}
public boolean onTop2()
{
return onTop1Bc()||onTop2Bc()||onTop3Bc();
}
private boolean onTop1B()
{
Box box = (Box)getOneObjectAtOffset(-getImage().getWidth()/2+8, -getImage().getHeight()/2 , Box.class);
return box != null&&vSpeed<0;
}
private boolean onTop2B()
{
Box box = (Box)getOneObjectAtOffset(0, -getImage().getHeight()/2 , Box.class);
return box != null&&vSpeed<0;
}
private boolean onTop3B()
{
Box box = (Box)getOneObjectAtOffset(getImage().getWidth()/2-8, -getImage().getHeight()/2 , Box.class);
return box != null&&vSpeed<0;
}
private boolean onTop1Bc()
{
Block block=(Block) getOneObjectAtOffset(-getImage().getWidth()/2+8, -getImage().getHeight()/2 , Block.class);
return block != null&&block.isThere==true;
}
private boolean onTop2Bc()
{
Block block=(Block) getOneObjectAtOffset(0, -getImage().getHeight()/2 , Block.class);
return block != null&&block.isThere==true;
}
private boolean onTop3Bc()
{
Block block=(Block) getOneObjectAtOffset(getImage().getWidth()/2-8, -getImage().getHeight()/2 , Block.class);
return block != null&&block.isThere==true;
}
private boolean onGround1B()
{
Box box = (Box)getOneObjectAtOffset(0, getImage().getHeight()/2 , Box.class);
return box != null&&box.isThere==true;
}
private boolean onGround2B()
{
Box box = (Box)getOneObjectAtOffset(getImage().getWidth()/2-10, getImage().getHeight()/2 , Box.class);
return box != null&&box.isThere==true;
}
private boolean onGround3B()
{
Box box = (Box)getOneObjectAtOffset(-getImage().getWidth()/2+10, getImage().getHeight()/2 , Box.class);
return box != null&&box.isThere==true;
}
private boolean onGroundB()
{
return onGround1B()||onGround2B()||onGround3B();
}
private boolean onGround1Bc()
{
Block block =(Block) getOneObjectAtOffset(0, getImage().getHeight()/2 , Block.class);
return block != null&&block.isThere==true;
}
private boolean onGround2Bc()
{
Block block =(Block) getOneObjectAtOffset(getImage().getWidth()/2-10, getImage().getHeight()/2 , Block.class);
return block != null&&block.isThere==true;
}
private boolean onGround3Bc()
{
Block block =(Block) getOneObjectAtOffset(-getImage().getWidth()/2+10, getImage().getHeight()/2 , Block.class);
return block != null&&block.isThere==true;
}
private boolean onGroundBc()
{
return onGround1Bc()||onGround2Bc()||onGround3Bc();
}
private boolean onGround1CA()
{
Object under = getOneObjectAtOffset(0, getImage().getHeight()/2 , Cliff.class);
return under != null;
}
private boolean onGround2CA()
{
Object under = getOneObjectAtOffset(getImage().getWidth()/2-10, getImage().getHeight()/2 , Cliff.class);
return under != null;
}
private boolean onGround3CA()
{
Object under = getOneObjectAtOffset(-getImage().getWidth()/2+10, getImage().getHeight()/2 , Cliff.class);
return under != null;
}
private boolean onGroundCA()
{
return onGround1CA()||onGround2CA()||onGround3CA();
}
private boolean onGround1P()
{
Object under = getOneObjectAtOffset(0, getImage().getHeight()/2 , Platform.class);
return under != null;
}
private boolean onGround2P()
{
Object under = getOneObjectAtOffset(getImage().getWidth()/2-10, getImage().getHeight()/2 , Platform.class);
return under != null;
}
private boolean onGround3P()
{
Object under = getOneObjectAtOffset(-getImage().getWidth()/2+10, getImage().getHeight()/2 , Platform.class);
return under != null;
}
private boolean onGroundP()
{
return onGround1P()||onGround2P()||onGround3P();
}
public boolean onGround()
{
return onGroundBc()||onGroundB()||onGroundCA()||onGroundP();
}
private boolean touchingLeftSide1B()
{
Box box=(Box)getOneObjectAtOffset(-getImage().getWidth()/2,getImage().getHeight()/2-10,Box.class);
return box!=null&&box.isThere==true;
}
private boolean touchingLeftSide2B()
{
Box box=(Box)getOneObjectAtOffset(-getImage().getWidth()/2,-getImage().getHeight()/2+1,Box.class);
return box!=null&&box.isThere==true;
}
private boolean touchingLeftSide3B()
{
Box box=(Box)getOneObjectAtOffset(-getImage().getWidth()/2,0,Box.class);
return box!=null&&box.isThere==true;
}
private boolean touchingLeftSideB()
{
return touchingLeftSide1B()||touchingLeftSide2B()||touchingLeftSide3B();
}
private boolean touchingLeftSide1Bc()
{
Block block=(Block)getOneObjectAtOffset(-getImage().getWidth()/2,getImage().getHeight()/2-10,Block.class);
return block != null&&block.isThere==true;
}
private boolean touchingLeftSide2Bc()
{
Block block=(Block)getOneObjectAtOffset(-getImage().getWidth()/2,-getImage().getHeight()/2+1,Block.class);
return block != null&&block.isThere==true;
}
private boolean touchingLeftSide3Bc()
{
Block block=(Block)getOneObjectAtOffset(-getImage().getWidth()/2,0,Block.class);
return block != null&&block.isThere==true;
}
private boolean touchingLeftSideBc()
{
return touchingLeftSide1Bc()||touchingLeftSide2Bc()||touchingLeftSide3Bc();
}
private boolean touchingLeftSide1CA()
{
Object side=getOneObjectAtOffset(-getImage().getWidth()/2,getImage().getHeight()/2-10,Cliff.class);
return side!=null;
}
private boolean touchingLeftSide2CA()
{
Object side=getOneObjectAtOffset(-getImage().getWidth()/2,-getImage().getHeight()/2+1,Cliff.class);
return side!=null;
}
private boolean touchingLeftSide3CA()
{
Object side=getOneObjectAtOffset(-getImage().getWidth()/2,0,Cliff.class);
return side!=null;
}
private boolean touchingLeftSideCA()
{
return touchingLeftSide1CA()||touchingLeftSide2CA()||touchingLeftSide3CA();
}
public boolean touchingLeftSide()
{
return touchingLeftSideB()||touchingLeftSideCA()||touchingLeftSideBc();
}
private boolean touchingRightSide1B()
{
Box box=(Box)getOneObjectAtOffset(getImage().getWidth()/2,-getImage().getHeight()/2+1,Box.class);
return box!=null&&box.isThere==true;
}
private boolean touchingRightSide2B()
{
Box box=(Box)getOneObjectAtOffset(getImage().getWidth()/2,getImage().getHeight()/2-10,Box.class);
return box!=null&&box.isThere==true;
}
private boolean touchingRightSide3B()
{
Box box=(Box)getOneObjectAtOffset(getImage().getWidth()/2,0,Box.class);
return box!=null&&box.isThere==true;
}
public boolean touchingRightSideB()
{
return touchingRightSide1B()||touchingRightSide2B()||touchingRightSide3B();
}
private boolean touchingRightSide1Bc()
{
Block block=(Block)getOneObjectAtOffset(getImage().getWidth()/2,-getImage().getHeight()/2+1,Block.class);
return block != null&&block.isThere==true;
}
private boolean touchingRightSide2Bc()
{
Block block=(Block)getOneObjectAtOffset(getImage().getWidth()/2,getImage().getHeight()/2-10,Block.class);
return block != null&&block.isThere==true;
}
private boolean touchingRightSide3Bc()
{
Block block=(Block)getOneObjectAtOffset(getImage().getWidth()/2,0,Block.class);
return block != null&&block.isThere==true;
}
public boolean touchingRightSideBc()
{
return touchingRightSide1Bc()||touchingRightSide2Bc()||touchingRightSide3Bc();
}
private boolean touchingRightSide1CA()
{
Object side=getOneObjectAtOffset(getImage().getWidth()/2,-getImage().getHeight()/2+1,Cliff.class);
return side!=null;
}
private boolean touchingRightSide2CA()
{
Object side=getOneObjectAtOffset(getImage().getWidth()/2,getImage().getHeight()/2-20,Cliff.class);
return side!=null;
}
private boolean touchingRightSide3CA()
{
Object side=getOneObjectAtOffset(getImage().getWidth()/2,0,Cliff.class);
return side!=null;
}
public boolean touchingRightSideCA()
{
return touchingRightSide1CA()||touchingRightSide2CA()||touchingRightSide3CA();
}
public boolean touchingRightSide()
{
return touchingRightSideB()||touchingRightSideCA()||touchingRightSideBc();
}
public void setVSpeed(int speed)
{
vSpeed = speed;
}
public void fall()
{
bump();
setLocation ( getX(), getY() + vSpeed);
vSpeed = vSpeed + acceleration;
if ( atBottom() )
gameEnd();
}
private boolean atBottom()
{
return getY() >= getWorld().getHeight() - 2;
}
public void gameEnd()
{
Scene scene=(Scene)getWorld();
scene.lives--;
scene.reset(true);
scene.levelUp();
}
public void bump()
{
if (onTop()||onTop2())
{
if(!hasHit)
{
vSpeed=-vSpeed;
}
hasHit=true;
}
if(onGround())
{
hasHit=false;
}
}
}
import greenfoot.*;
/**
* A little penguin that wants to get to the other side.
*/
public class Pengu extends Mover
{
private static final int jumpStrength = 23;
private int timer=0;
private int delay=0;
private static final int gunReload=5;
private GreenfootImage right=new GreenfootImage("Pengu/pengu-right2.png");
private GreenfootImage left=new GreenfootImage("Pengu/pengu-left2.png");
private GreenfootImage rightShooting=new GreenfootImage("Pengu/pengu-right2Shooting.png");
private GreenfootImage leftShooting=new GreenfootImage("Pengu/pengu-left2Shooting.png");
public Pengu()
{
setImage(right);
}
public void act()
{
Scene scene=(Scene)getWorld();
if(!scene.pause)
{
if(scene.shooting==true)delay++;
shoot();
shooting();
coins();
checkKeys();
checkFall();
}
}
private void checkKeys()
{
Scene scene=(Scene)getWorld();
if (Greenfoot.isKeyDown("left")&&getX()>=20 )
{
scene.direction=false;
if(!touchingLeftSide())
moveLeft();
}
if (Greenfoot.isKeyDown("left"))
{
if(!scene.shooting)
{
setImage(left);
}
else if(scene.shooting=true)
{
setImage(leftShooting);
}
}
if (Greenfoot.isKeyDown("right")&&scene.move==false)
{
scene.direction=true;
if(!touchingRightSide())
moveRight();
}
if (Greenfoot.isKeyDown("right"))
{
if(!scene.shooting)
{
setImage(right);
}
else if(scene.shooting=true)
{
setImage(rightShooting);
}
}
if (Greenfoot.isKeyDown("up") )
{
if (onGround())
jump();
}
}
private void jump()
{
Scene scene=(Scene)getWorld();
setVSpeed(-jumpStrength);
fall();
}
private void coins()
{
Scene scene=(Scene)getWorld();
Coin coin=(Coin)getOneIntersectingObject(Coin.class);
if(coin!=null)
{
scene.removeObject(coin);
scene.score(100);
scene.coins=scene.coins+1;
}
}
private void shoot()
{
Scene scene=(Scene)getWorld();
if(Greenfoot.isKeyDown("space")&&delay>=gunReload&&scene.shooting==true)
{
if(scene.direction==false)
{
scene.addObject(new Bullet(-8),getX()-26,getY());
}
if(scene.direction==true)
{
scene.addObject(new Bullet(8),getX()+26,getY());
}
delay=0;
}
}
private void shooting()
{
Scene scene=(Scene)getWorld();
if(isTouching(Shooting.class))
{
scene.shooting=true;
scene.score(250);
removeTouching(Shooting.class);
}
if(scene.shooting)
{
if(scene.direction==false)
{
setImage(leftShooting);
}
if(scene.direction==true)
{
setImage(rightShooting);
}
}
else if(!scene.shooting)
{
if(scene.direction==false)
{
setImage(left);
}
if(scene.direction==true)
{
setImage(right);
}
}
}
public void checkFall()
{
if (onGround()) {
setVSpeed(0);
Actor under = getOneObjectAtOffset(0, getImage().getHeight()/2 , null);
if(!checkDoor())
{
if(under!=null)
{
int h=under.getImage().getHeight()/2+getImage().getHeight()/2-5;
int y=under.getY();
setLocation(getX(),y-h);
}
}
}
else {
fall();
}
}
private boolean checkDoor()
{
return isTouching(Door.class)||isTouching(DoorPost.class);
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Box here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Box extends Boxes
{
private GreenfootImage image1=new GreenfootImage("mystery.png");
private GreenfootImage image2=new GreenfootImage("MysteryG.png");
private GreenfootImage image3=new GreenfootImage("Block.png");
private GreenfootImage image4=new GreenfootImage("transparentBox.png");
private GreenfootImage image5=new GreenfootImage("Bricks2.png");
private int timer=0;
private boolean hit=false;
private int counter=0;
private boolean empty=false;
private boolean coin=false;
private boolean shooting=false;
private boolean invisible=false;
private boolean extraLife=false;
private boolean brick=false;
public boolean isThere=true;
public Box(int type)
{
if(type==1)
{
invisible=false;
coin=true;
shooting=false;
extraLife=false;
}
if(type==2)
{
invisible=false;
shooting=true;
coin=false;
extraLife=false;
}
if(type==3)
{
invisible=false;
shooting=false;
coin=false;
extraLife=true;
}
if(type==4)
{
invisible=true;
coin=true;
shooting=false;
extraLife=false;
isThere=false;
}
if(type==5)
{
invisible=true;
coin=false;
shooting=true;
extraLife=false;
isThere=false;
}
if(type==6)
{
invisible=true;
shooting=false;
coin=false;
extraLife=true;
isThere=false;
}
if(invisible==false)
setImage(image1);
if(invisible==true)
setImage(image4);
}
/**
* Act - do whatever the Box wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
Scene scene=(Scene)getWorld();
if(!scene.pause)
{
if(!empty)
{
checkPengu();
hit();
}
remove();
}
}
private boolean checkPengu1()
{
Pengu pengu=(Pengu)getOneObjectAtOffset(0,getImage().getHeight()/2+1,Pengu.class);
return pengu!=null&&pengu.vSpeed<0;
}
private boolean checkPengu2()
{
Pengu pengu=(Pengu)getOneObjectAtOffset(getImage().getWidth()/2-1,getImage().getHeight()/2,Pengu.class);
return pengu!=null&&pengu.vSpeed<0;
}
private boolean checkPengu3()
{
Pengu pengu=(Pengu)getOneObjectAtOffset(-getImage().getWidth()/2+1,getImage().getHeight()/2,Pengu.class);
return pengu!=null&&pengu.vSpeed<0;
}
private void checkPengu()
{
if(checkPengu1()||checkPengu2()||checkPengu3())
{
hit=true;
}
}
private void hit()
{
Scene scene=(Scene)getWorld();
if(hit==true)
{
timer++;
if(counter<6)
{
if(timer==2&&(getImage()==image1||getImage()==image4||getImage()==image5))
{
setImage(image2);
timer=0;
counter++;
}
if(timer==2&&getImage()==image2)
{
setImage(image1);
timer=0;
counter++;
}
}
}
if(counter==5)
{
empty=true;
if(shooting==true)
{
scene.addObject(new Shooting(),getX(),getY());
}
else if(coin==true)
{
scene.coins++;
}
else if(extraLife==true)
{
scene.addObject(new ExtraLife(),getX(),getY());
}
scene.score(100);
counter=0;
isThere=true;
hit=false;
setImage(image3);
}
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Block here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Block extends Boxes
{
private GreenfootImage image1=new GreenfootImage("Block.png");
private GreenfootImage image2=new GreenfootImage("Bricks2.png");
private GreenfootImage image3=new GreenfootImage("EndBricks.png");
private GreenfootImage image4=new GreenfootImage("Tunnel.png");
public boolean isThere=true;
private boolean addObjects=false;
public Block(int type)
{
if(type==1)
{
setImage(image1);
}
if(type==2)
{
setImage(image2);
}
if(type==3)
{
setImage(image3);
}
if(type==4)
{
setImage(image4);
isThere=false;
}
if(type==5)
{
setImage(image1);
addObjects=true;
}
}
/**
* Act - do whatever the Block wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
Scene scene=(Scene)getWorld();
if(!scene.pause)
{
addObjects();
remove();
}
}
private void addObjects()
{
Scene scene=(Scene)getWorld();
if(addObjects)
{
scene.addObjects=getX();
}
}
}
