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

2015/6/1

Trouble Calculating Reflection Angles of Ball and Tilted Paddle

polar568 polar568

2015/6/1

#
I am trying to have a ball reflect off of a tilted paddle. This does not include any gravity. For example: a ball moving left from the left side of the screen would reflect downwards from a paddle tilted at -45 degrees. Ball class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import greenfoot.*;
import java.awt.Color;
 
public class Ball extends SmoothMover
{
    public Ball(String side) {
        GreenfootImage img = new GreenfootImage(22, 22);
        img.setColor(Color.GREEN);
        img.fillOval(0, 0, 22, 22);
        setImage(img);
        if(side.equals("left")) {
            addForce(new Vector(0,3));
        }
        if(side.equals("right")) {
            addForce(new Vector(180,3));
        }
        if(side.equals("top")) {
            addForce(new Vector(90,3));
        }
        if(side.equals("bottom")) {
            addForce(new Vector(270,3));
        }
    }
 
    public void act()
    {
        move();
        collide();
    }   
 
    public void collide() {
        Paddle pad = (Paddle) getOneIntersectingObject(Paddle.class);
        if(pad != null) {              
             
            }           
        }
    }
}
Paddle Class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import greenfoot.*; 
 
 
public class Paddle extends Actor
{
    private int rotation;
    public Paddle(int angle) {
        GreenfootImage img = new GreenfootImage(5, 50);
        img.fill();
        setRotation(angle);
        rotation = angle;
        setImage(img);
    }
     
    public void act()
    {
        
    }   
     
    public int getAngle() {
        return rotation;
    }
}
danpost danpost

2015/6/1

#
You can remove lines 6, 11, and 14 through 22 from the class. The Actor class already has a 'private int rotation' field that will hold the same value and is accessible via the 'getRotation()' method. As far as balls bouncing off the paddles -- what are we really looking at here? will the balls always approach from a standard 0, 90, 180, 270 angle? will the balls always hit near the center of the long face of the paddle or could they hit the corner, or even the short face? and if so, are you wanting accurate bouncing at those points also? You may want to look at my Pong Pinball scenario. It has quite accurate bouncing of a ball off blocks that rotates; however, their rotation is limited to 0 and 90. Still, the code is already quite heavy.
polar568 polar568

2015/6/2

#
Thanks for the reminder! I didn't think about using getRotation(). As for the angles of the approaching balls, yes, they will only approach from 0, 90, 180, or 270 angles. Also they will always hit the center of the paddles. The paddles will always be angled at either 45 or 135 degrees. I looked at your Pong Pinball scenario and while the bouncing of the ball off of the blocks in that game does help slightly, I still cannot figure out my problem.
danpost danpost

2015/6/2

#
I think the same basic formula will work as that which I use in the Pong Pinball scenario for the ball hitting a block -- something like:
1
int newAngle = 540-ballRotation-2*paddleTilt;
You just do not need all the other stuff for contacting the corners and sides of the block.
polar568 polar568

2015/6/2

#
Thank you for the help! That solves my problem!
You need to login to post a reply.