- #1
techy
- 1
- 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.
Can somebody please help me with this?
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: