Java How can I make a circular version of pong in Java?

  • Thread starter Thread starter techy
  • Start date Start date
  • Tags Tags
    Circular Game
AI Thread Summary
The discussion centers around the challenge of implementing paddle bouncing mechanics in a Java game inspired by Pong. The user is struggling with how to make the paddle, which moves in a circular motion, interact correctly with the ball upon collision. The provided code includes a Vector class that handles the paddle's position and movement, featuring methods for vector operations such as dot product, reflection, and angle calculations. Key points of concern include the implementation of the reflection logic, particularly in the `reflectP` method, which aims to calculate the new direction of the ball after it hits the paddle. The user seeks assistance in refining this logic to achieve the desired bouncing effect. A link to a related question on Code Review Stack Exchange is also shared for further context.
techy
Messages
1
Reaction score
0
hi,

I have been trying to make a game similar to pong in java. But I do not understand the concept of bouncing of the paddle. the paddle moves around in a circle. Here is the code that I have for the vector class that takes in the position of the paddle as a vector.
Java:
public class Vector {
     double dx, dy;
 
    public Vector(int x, int y) {
        dx = 1.*x;
        dy = 1.*y;
    }
   
    public double dot(Vector V){
        return this.dx * V.dx + this.dy * V.dy;
    }
   
    public static final Vector UX = new Vector(1, 0);
    public static final Vector UY = new Vector(0, 1);
    public static final Vector ZERO = new Vector(0, 0);
   
   
    public Vector(double dr, double theta) {
        setRTheta(dr, theta);
    }
   
    private void setRTheta(double dr, double theta) {
        dx = dr*Math.cos(theta);
        dy = dr*Math.sin(theta);
    }
   
    public void setX(int x){
        dx = x;
    }
   
    public void setY(int y){
        dy = y;
    }
   
    public int getX(){
        return (int)dx;
    }
   
    public int getY(){
        return (int)dy; 
    }

    public double getR() {
        return Math.hypot(dx, dy);
    }

    public double getTheta() {
        return Math.atan2(dy, dx);
    }

    public Vector multiply(double d) {
        return new Vector(this.dx * d, this.dy * d);
    }
    public void add(Vector v) {
        dx += v.dx;
        dy += v.dy;
    }

    public Vector subtract(Vector v)
    {
        return new Vector(this.dx - v.dx, this.dy - v.dy);
    }
   
    public void reflect(double angle) {
        setRTheta(-getR(), 2*angle-getTheta());
    }
   
    public void reflectP(Vector v){
        Vector unitVector = new Vector(v.dx/Math.sqrt(v.dx * v.dx + v.dy * v.dy), v.dy/Math.sqrt(v.dx * v.dx + v.dy * v.dy));
        double dotProduct = this.dot(unitVector);
        Vector result = unitVector.multiply(2 * dotProduct).subtract(this);
        dx = result.dx;
        dy = result.dy;
//        dx = dx - 2*(v.dx/Math.sqrt(v.dx * v.dx + v.dy * v.dy) + this.dx) * //v.dx/Math.sqrt(v.dx * v.dx + v.dy * v.dy);
//        dy = dy - 2*(v.dy/Math.sqrt(v.dx * v.dx + v.dy * v.dy) + this.dy) * //v.dy/Math.sqrt(v.dx * v.dx + v.dy * v.dy);
    }
   
    public String toString(){
        return  " "  + dx + " "  + dy;  
    }
}

Can somebody please help me with this?
 
Last edited by a moderator:
Technology news on Phys.org
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
Thread 'Project Documentation'
Trying to package up a small bank account manager project that I have been tempering on for a while. One that is certainly worth something to me. Although I have created methods to whip up quick documents with all fields and properties. I would like something better to reference in order to express the mechanical functions. It is unclear to me about any standardized format for code documentation that exists. I have tried object orientated diagrams with shapes to try and express the...

Similar threads

Back
Top