Having trouble working out a code for an Orb and its collision detection with another Orb . I have created collision detection for a paddle and the walls but not another Orb. When I think I have it the Orbs just seem to vibrate in place.
All help is appreciated.
Below is the code for my Orb class.
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Gold flying Orb
*
* @author William Jones
* @version 2.7413
*/
public class Orb extends Actor
{
private Velocity vel;
private Velocity newVel;
private int xSpeed;
private int ySpeed;
private int exactX;
private int exactY;
private GreenfootImage getImage;
private Orb collider;
private Orb getIntersectingOrb;
private int reverseY;
/**
* Creates the constructor for Orb
*/
public Orb(int xSpeed, int ySpeed)
{
vel= new Velocity(xSpeed, ySpeed);
this.vel = vel;
getImage = new GreenfootImage("gold-ball.png");
}
/**
* Act - do whatever the Orb wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
move();
turnIfNeeded();
handleOrbCollision();
removeAtTopandBottom();
handlePaddleCollision();
}
/**
* Enables the Orb to bounce off all walls
*/
public void turnIfNeeded()
{
turnAtRightWall();
turnAtLeftWall();
}
/**
* Add orb
*
*/
public void addedToWorld(World world)
{
exactX = getX();
exactY = getY();
}
/**
* makes Orb bounce off right wall
*/
public void turnAtRightWall()
{
if (getX() + getImage().getWidth()/2 >= getWorld().getWidth())
{
vel.reverseX();
}
}
/**
* removes Orb from World
*/
public void removeAtTopandBottom()
{
if (getY() + getImage().getHeight()/2 >= getWorld().getHeight())
{
World world = getWorld();
world.removeObject(this);
}
else if (getY() - getImage().getHeight()/2 <= 0)
{
World world = getWorld();
world.removeObject(this);
}
}
/**
* makes Orb bounce off left Wall
*/
public void turnAtLeftWall()
{
if (getX() - getImage().getWidth()/2 <= 0)
{
vel.reverseX();
}
}
public void move()
{
exactX = exactX + vel.getXSpeed();
exactY = exactY + vel.getYSpeed();
super.setLocation( (int) exactX, (int) exactY);
}
/** * Return velocity object associated with Orb
*/
public Velocity getVelocity()
{
return vel;
}
public int getXSpeed()
{
return xSpeed;
}
public int getYSpeed()
{
return ySpeed;
}
public void setVelocity(Velocity newvel)
{
vel = newvel;
}
public void reverseY()
{
xSpeed = -xSpeed;
ySpeed = -ySpeed;
}
/**
* Checks to see it Orb is striking another Orb
*/
private void getIntersectingOrb()
{
Actor orb = getOneIntersectingObject(Orb.class);
if (orb != null)
{
vel.reverseY();
}
}
public void handleOrbCollision()
{
collider = getIntersectingOrb;
if (collider == getIntersectingOrb)
{
vel.reverseY();
}
else if(collider == getIntersectingOrb)
{
vel.reverseX();
}
}
private void handlePaddleCollision()
{
Actor paddle = getOneIntersectingObject(Paddle.class);
if (paddle != null)
{
vel.reverseY();
}
}
}