I am having trouble with making my orbs bounce off the wall when they hit it. They all run to the bottom right corner of the world and stay there. The instructions state:
9. Your next step is to account for a collision with the walls. You will accomplish this within a method called turnIfNeeded(), which will be called from the act() method of Orb. The turnIfNeeded() method may call four separate methods, which you write in this step. The four methods are: turnAtLeftWall(), turnAtRightWall(), turnAtFloor(), and turnAtRoof().
10. Cause the Orb to make a noise if it bounces off of a wall.
The code for one of the four methods, turnAtFloor(), is provided below. Use it as a model to create the three other methods.
public void turnAtFloor()
{
if (getY() + getImage().getHeight()/2 >= getWorld().getHeight()) {
vel.reverseY();
}
}
Here is my code: Please help me figure out what is wrong with it. Thanks in advance
public class Orb extends Actor
{
private Velocity vel; //instance variable for Orb() constructor.
private double exactX;
private double exactY;
private Velocity movement;
private String sound = "4e.wav";
private boolean touched = false;
/**
* Creates a new velocity object with specified x and y speeds.
*/
public void Orb(int xSpeed, int ySpeed)
{
Velocity vel = new Velocity(0, 0);
}
public void act()//calls for the methods that tell the orb what to do
{
move();
turnIfNeeded();
}
/**
* method that controls the movement of the orb
*/
public void move() //7
{
exactX = exactX + getX();// gets the location of the orb
exactY = exactY + getY();
Velocity velocity = new Velocity();
velocity.getXSpeed();// gets the speed of the orb
velocity.getYSpeed();
super.setLocation((int) exactX, (int) exactY);//sets the new location of the orb
}
/**
* Set the location using exact (double) co-ordinates.
*/
public void setLocation(double x, double y)
{
exactX = x;
exactY = y;
super.setLocation((int) x, (int) y);//converts from double to int
}
/**
* Set location. Redefinition of the standard Greenfoot method to make sure
* the exact co-ordinates are updated in sync.
*/
public void setLocation(int x, int y)
{
exactX = x;
exactY = y;
super.setLocation((int) x, (int) y);//converts from double to int
}
/**
* Return the exact x co-ordinate (as a double).
*/
public double getExactX()
{
return exactX;
}
/**
* Return the exact y co-ordinate (as a double).
*/
public double getExactY()
{
return exactY;
}
/**
* Check whether we have hit any of the edges of the universe. If so, bounce off it.
*/
private void turnIfNeeded()
{
turnAtLeftWall();
turnAtRightWall();
turnAtFloor();
turnAtRoof();
}
/**
* Check whether we have hit the left wall of the universe. If so, bounce off it.
*/
public void turnAtLeftWall()
{
//if touching the left side, turn
if (getX() + getImage().getWidth()/2 >= getWorld().getWidth())
{
vel.reverseX(); //Throws the error:
//java.lang.NullPointerException
//at Orb.turnAtLeftWall(Orb.java:101)
//at Orb.turnIfNeeded(Orb.java:87)
//at Orb.act(Orb.java:29)
Greenfoot.playSound(sound);
}
}
/**
* Check whether we have hit the right edge of the universe. If so, bounce off it.
*/
public void turnAtRightWall()
{
//if touching the right side, turn
if (getX() + getImage().getWidth()/2 >= getWorld().getWidth())
{
vel.reverseX();
Greenfoot.playSound(sound);
}
}
/**
* Check whether we have hit the floor of the universe. If so, bounce off it.
*/
public void turnAtFloor()
{
//if at the bottom, turn
if (getY() + getImage().getHeight()/2 >= getWorld().getHeight())
{
vel.reverseY();
Greenfoot.playSound(sound);
}
}
/**
* Check whether we have hit the roof of the universe. If so, bounce off it.
*/
public void turnAtRoof()
{
//if at the top, turn
if (getY() + getImage().getHeight()/2 >= getWorld().getHeight())
{
vel.reverseY();
Greenfoot.playSound(sound);
}
}
/**
* play this sound when the orb hits any of the edges of the universe
*/
public void playSound()
{
Greenfoot.playSound(sound);
}
}
public class Velocity
{
// instance variables
double dx;
double dy;
int direction;
double length;
private int xSpeed = 0;
private int ySpeed = 0;
/**
* Create a new, neutral velocity.
*/
public Velocity()
{
}
/**
* Constructor for objects of class Velocity
*
*/
public Velocity(int initXSpeed, int initYSpeed)
{
xSpeed = initXSpeed;
ySpeed = initYSpeed;
this.dx = dx;
this.dy = dy;
}
/**
* Create a velocity by specifying the x and y offsets from start to end points.
*/
public Velocity(double dx, double dy)
{
this.dx = dx;
this.dy = dy;
updatePolar();
}
/**
* Set the direction of the velocity, leaving the length intact.
*/
public void setDirection(int direction)
{
this.direction = direction;
updateCartesian();
}
/**
* Add another vector to the velocity.
*/
public void add(Velocity other)
{
dx += other.dx;
dy += other.dy;
updatePolar();
}
/**
* Set the length of the velocity, leaving the direction intact.
*/
public void setLength(double length)
{
this.length = length;
updateCartesian();
}
/**
* Scale the velocity up (factor > 1) or down (factor < 1). The direction
* remains unchanged.
*/
public void scale(double factor)
{
length = length * factor;
updateCartesian();
}
/**
* Set this vector to the neutral velocity
*/
public void setNeutral() {
dx = 0.0;
dy = 0.0;
length = 0.0;
direction = 0;
}
/**
* Revert to horizontal component of this movement velocity.
*/
public void revertHorizontal() {
dx = -dx;
updatePolar();
}
/**
* Revert to vertical component of this movement velocity.
*/
public void revertVertical() {
dy = -dy;
updatePolar();
}
/**
* Return the x offset of the velocity.
*/
public double getX() {
return dx;
}
/**
* Return the y offset of the velocity.
*/
public double getY() {
return dy;
}
/**
* Return the direction of the velocity (in degrees). 0 is EAST.
*/
public int getDirection() {
return direction;
}
/**
* Return the length of the velocity.
*/
public double getLength() {
return length;
}
/**
* Update the direction and length from the current dx, dy.
*/
private void updatePolar()
{
this.direction = (int) Math.toDegrees(Math.atan2(dy, dx));
this.length = Math.sqrt(dx*dx+dy*dy);
}
/**
* Get speed in the X direction
*
* return the speed in X direction
*/
public int getXSpeed()
{
return xSpeed;
}
/**
* Get speed in the Y direction
*
* return the speed in Y direction
*/
public int getYSpeed()
{
return ySpeed;
}
/**
* reverseX - reverse the speed in X direction
*/
public void reverseX()
{
xSpeed = -xSpeed;
}
/**
* reverseY - reverse the speed in X direction
*/
public void reverseY()
{
ySpeed = -ySpeed;
}
/**
* Update dx and dy from the current direction and length.
*/
private void updateCartesian()
{
dx = length * Math.cos(Math.toRadians(direction));
dy = length * Math.sin(Math.toRadians(direction));
}
}

