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

  • Context: Java 
  • Thread starter Thread starter techy
  • Start date Start date
  • Tags Tags
    Circular Game
Click For Summary
SUMMARY

This discussion focuses on creating a circular version of the Pong game in Java, specifically addressing the mechanics of paddle reflection. The provided code includes a custom Vector class that handles vector operations essential for paddle movement and reflection. Key methods such as reflect and reflectP are highlighted for their role in calculating the paddle's response upon collision. The user seeks assistance in refining the paddle's bouncing behavior within a circular trajectory.

PREREQUISITES
  • Understanding of Java programming language
  • Familiarity with vector mathematics and operations
  • Knowledge of game development concepts, particularly collision detection
  • Experience with object-oriented programming principles
NEXT STEPS
  • Explore Java's built-in graphics libraries for game rendering
  • Learn about collision detection algorithms in 2D games
  • Investigate advanced vector mathematics for game physics
  • Study game design patterns relevant to paddle and ball mechanics
USEFUL FOR

Game developers, particularly those interested in implementing physics-based mechanics in Java, and programmers looking to enhance their understanding of vector mathematics in game design.

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

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
9
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 5 ·
Replies
5
Views
5K
  • · Replies 3 ·
Replies
3
Views
2K