I've been trying to make a mini golf game and i cant seem to get the ball to bounce of the edges of the green, this is the code which i have so far so if anyone could help that would be great!
public void coordinates()
{
if((Greenfoot.mouseDragged(this)&&!isGrabbed)){//checks for beginning of drag
isGrabbed= true;
MouseInfo mi =Greenfoot.getMouseInfo();
mouseX=mi.getX();
mouseY=mi.getY();
return;
}
else if (Greenfoot.mouseDragEnded(this)&&isGrabbed){//checks for end of drag
MouseInfo mouse=Greenfoot.getMouseInfo();
targetX=mouse.getX();
targetY=mouse.getY();
isGrabbed=false;
return;
}
}
public void tings()
{
coordinates();
dx =mouseX-targetX;
dy =mouseY-targetY;
double length=Math.sqrt(dx*dx+dy*dy); //works out length using trig
int distance=(int)Math.ceil(length); //rounds value to whole number
xspeed=((distance*dx)/100);
yspeed=((distance*dy)/100);
if(distance>=60){
i=10;
}
else if((distance>=40)&&(distance<60)){
i=8;
}
else if((distance>20)&&(distance<40)){
i=6;
}
else{
i=4;
}
///x 80-520;
//y 170-330;
}
public void movement()
{
coordinates();
tings();
if(!isGrabbed){
setLocation(getX()+xspeed, getY()+yspeed);
while(i>0){
i--;
xspeed=xspeed--;
yspeed=yspeed--;
}
}
}
public void act(){
movement();
bounce();
Actor actor=getOneIntersectingObject(hole.class);
if(actor!=null){
getWorld().removeObject(this);
}
}
public void bounce(){
if (getX() <= 80) {
if (getRotation() < 180) {
setRotation(180-getRotation());
}
else if (getRotation() >= 180) {
setRotation((180-getRotation()));
}
}
else if (getX() >=520) {
if (getRotation() <= 90) {
setRotation(180-getRotation());
}
else if (getRotation() > 270) {
setRotation(180+(360-getRotation()));
}
}
else if (getY() <= 170) {
if (getRotation() <= 270) {
setRotation(90+(270-getRotation()));
}
else if (getRotation() > 270) {
setRotation(90-(getRotation()-270));
}
}
else if (getY() >=330 ) {
if (getRotation() <= 90) {
setRotation(270+(90-getRotation()));
}
else if (getRotation() > 90) {
setRotation(270-(getRotation()-90));
}
}
}

