Physics Forums

Physics Forums (http://www.physicsforums.com/index.php)
-   Programming & Computer Science (http://www.physicsforums.com/forumdisplay.php?f=165)
-   -   Game like circular pong (http://www.physicsforums.com/showthread.php?t=278765)

techy Dec10-08 07:12 PM

game like circular pong
 
hi,

I have been trying to make a game similiar 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.

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???


All times are GMT -5. The time now is 04:23 PM.

Powered by vBulletin Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2014 Physics Forums