This site requires JavaScript, please enable it in your browser!
Greenfoot back
Kieranwilko98
Kieranwilko98 wrote ...

2017/1/25

Bouncing off edges realistically

Kieranwilko98 Kieranwilko98

2017/1/25

#
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));
        }
      
    }
    
            
         
              
   }
            

   
Super_Hippo Super_Hippo

2017/1/25

#
It is quite difficult to guess what all these x and y coordinate stand for. Usually the ball should check if it is touching some wall object and then it can check in which direction it was moving into the wall and calculate in which direction it should bounce off it. I think you have to explain what you have tried there.
danpost danpost

2017/1/25

#
Start by removing 'else' from line 90. You want to check along each axis individually. Your conditions for "bounce" may be too loose. If you find the ball glitches along the boundaries, then continue reading. Not only does the ball have to be out of the set boundaries, but it also has to be moving "toward" it. In other words, you do not want the ball to be turned around when it is out of bounds and has already been turned around once. For example, when the x-coordinate is less than 80, turn around only if the rotation is between 91 and 269; and when the x-coordinate is greater than 520, only if the rotation is not between 90 and 270. For both of these, the new rotation will be (180-rotation). On a vertical bounce, it will be just (-rotation).
Kieranwilko98 Kieranwilko98

2017/1/25

#
First of all the ball doesn't bounce at all it goes straight past the edge, the green is just an object in which the x limits are 80-520 and y limits are 170-330, this is the piece of code for one of the ball classes, I just want the ball to bounce back of the edges with roughly the same angle as entry.
danpost danpost

2017/1/25

#
What I was saying is that there are 3 conditions for bouncing off any edge:
  EDGE               CONDITIONS
LEFT     x<80, rotation>90 and rotation<270
RIGHT   x>520, rotation<90 or rotation>270
TOP     y<170, rotation>180 (last condition is always true:  rotation < 360)
BOTTOM  y>330, rotation<180 and rotation>0
Therefore
if ( (getX() < 80 && getRotation() > 90 && getRotation() < 270) ||
    ( getX() > 520 && (getRotation() < 90 || getRotation() > 270) ) )
{
    setRotation(180-getRotation());
}
if ( (getY() < 170 && getRotation() > 180) ||
    (getY() > 330 && getRotation() < 180 && getRotation() > 0) )
{
    setRotation(-getRotation());
}
Kieranwilko98 Kieranwilko98

2017/1/26

#
okay thanks I get that now, but same problem the ball just carries on going in the same direction of travel and doesn't rotate at all at the edges where I want it to, is it because the x,y coordinates aren't being updated in that class?
danpost danpost

2017/1/26

#
Kieranwilko98 wrote...
okay thanks I get that now, but same problem the ball just carries on going in the same direction of travel and doesn't rotate at all at the edges where I want it to, is it because the x,y coordinates aren't being updated in that class?
Please show your revised code.
danpost danpost

2017/1/26

#
I can see that one major problem is that you are using 'setLocation' and not 'move'. The rotation of the actor does not influence the direction of movement when using 'setLocation'. Also, you are decreasing the x and y speeds by the same amount and not proportionally to have both reach zero at the same time.
Kieranwilko98 Kieranwilko98

2017/1/26

#
Okay so would I have to have a negative x and y speed when coming into contact with an edge ? I'm guessing it would change for which edge it hits I'm just struggling to figure out which of the speeds will change. I think the y speed would just have to become negative of what it was on the top and bottom edges but I'm unsure about the sides.
danpost danpost

2017/1/26

#
Kieranwilko98 wrote...
I think the y speed would just have to become negative of what it was on the top and bottom edges but I'm unsure about the sides.
If the bounce on top and bottom negates the y speed, I think it would make sense what is negated for the left and right sides.
You need to login to post a reply.