I am making a pong type game in java and I am trying to make the ball bounce off of the walls but whenever the ball hits the ball it just stops, it does not reflect off of the wall and I can't seem to figure out why.
Ball class which handles the ball movement
public class Ball {
private double x;
private double y;
private double time;
private double xreflection=1.0;
private double yreflection=1.0;
private BallTrajectory traj=new BallTrajectory(20, 20);
public Ball(double x, double y) {
this.x=x;
this.y=y;
}
public void tick() {
time+=1.0/60.0;
if(x==0)
xreflection=1.0;
else if(x==Game.Width-15)
xreflection=-1.0;
if(y==0)
yreflection=1.0;
else if(y==Game.Height-15)
yreflection=-1.0;
x+=traj.xvel()*xreflection;
y-=traj.yvel(time)*yreflection;
}
public void render(Graphics g) {
g.setColor(Color.pink);
g.fillOval((int)x, (int)y, 15,15);
}
}
This class handles the trajectory of the ball as it moves in projectile type motion
public class BallTrajectory {
private double initvel;
private double theta;
public BallTrajectory(double initvel, double theta) {
this.initvel=initvel;
this.theta=theta;
}
public double xvel() {
double xvelo=initvel*Math.cos(Math.toRadians(theta));
return xvelo;
}
public double yvel(double time) {
double yvelo=initvel*Math.sin(Math.toRadians(theta))-(9.8*time);
return yvelo;
}
public double xpos(double time) {
double xpos=initvel*Math.cos(Math.toRadians(theta))*time;
return xpos;
}
public double ypos(double time) {
double ypos=initvel*Math.sin(Math.toRadians(theta))*time-.5*9.8*Math.pow(time, 2);
return ypos;
}
Without going through a whole bunch of testing, I would suggest that it is very unlikely that x will ever be exactly equal to Game.Width or 0. Instead, you should be testing that the value is "within bounds" instead, maybe something like...
public void tick() {
time += 1.0 / 60.0;
if (x <= 0) {
xreflection = 1.0;
} else if (x >= Game.Width - 15) {
xreflection = -1.0;
}
if (y <= 0) {
yreflection = 1.0;
} else if (y >= Game.Height - 15) {
yreflection = -1.0;
}
x += traj.xvel() * xreflection;
y -= traj.yvel(time) * yreflection;
}
You should also start taking the time to learn how to debug your code, it is something you will need to do a lot, from desk-checking your logic to using print statements and the debugger
Related
Currently I'm working on a simple 2d platformer, and I decided to work on physics before working on the general concept of the game. I actually haven't learned physics in school or anything, so I'm just using google/youtube tutorials as my main resources. I've currently gotten jumping working pretty nicely, but moving side to side isn't what I would like it to be. I want it to use acceleration/deceleration vs. just incrementing/decrementing the x position by a constant speed. Now I've tried using this website for x motion but that seems to be what I'm using for jumping. Here is my current player class:
import game.Game;
import game.input.PlayerController;
public class Player {
private float dt = 0.18F, gravity = 9.81F;
public float x, y, dx, dy;
private PlayerController controller;
private boolean jumping, onGround;
public float speed = 7.5F;
public float jh = 60F;
public float vy = 120F;
public Player() {
controller = new PlayerController();
}
public void update() {
controller.update(this);
dy += gravity * dt * (jumping ? -1F : 1F);
if(!jumping && !onGround && dy > vy) dy = vy;
y += dy * dt + 0.5F * gravity * dt * dt;
x += dx;
if (y > Game.height - 32) {
dy = 0;
y = Game.height - 32;
onGround = true;
} else
onGround = false;
if(dy < jh) { jumping = false;}
dx = 0;
}
public void render(Graphics g) {
g.setColor(Color.red);
g.fillRect(x, y, 32, 32);
}
public void moveLeft() {
dx -= speed;
}
public void moveRight() {
dx += speed;
}
public void jump() {
if (onGround) {
jumping = true;
dy -=jh;
}
}
}
In the PlayerController class I'm just calling player.moveLeft() and player.moveRight() when the left and right keys are pressed.
If anybody has a good idea on how to make smoother movement, that would be very helpful. Thanks!
This is my test class,
public class Shape2DTester {
public static void main(String[] args) {
GeometricObject2D geoObject1 = new ComparableCircle2D(0, 5, 2);
GeometricObject2D geoObject3 = new ComparableCircle2D(0, 0, 2);
System.out.println("geoObject1 overlaps geoObject3: "
+ geoObject1.intersect(geoObject3));
}
}
This is my circle class,
public class ComparableCircle2D extends GeometricObject2D<ComparableCircle2D> {
public double x, y;
public double radius;
ComparableCircle2D() {
super();
this.radius = 1.0;
}
ComparableCircle2D(double radius) {
super();
this.radius = Math.abs(radius);
}
ComparableCircle2D(double x, double y, double radius) {
super(x, y);
this.radius = Math.abs(radius);
}
public double getArea() {
return Math.PI * getRadius() * getRadius();
}
public double getPerimeter() {
return 2 * Math.PI * getRadius();
}
public void setRadius(double setRadius) {
this.radius = Math.abs(setRadius);
}
public double getRadius() {
return radius;
}
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
#Override
public boolean intersect(GeometricObject2D g) {
ComparableCircle2D other = (ComparableCircle2D) g;
double dx = other.x - getX();
double dy = other.y - getY();
double radi = other.radius + getRadius();
return (dx * dx + dy * dy < radi * radi);
}
}
}
this is my superclass,
public abstract class GeometricObject2D<T extends GeometricObject2D> implements
Comparable<GeometricObject2D> {
public double x, y;
GeometricObject2D() {
this.x = 0;
this.y = 0;
}
GeometricObject2D(double x, double y) {
this.x = x;
this.y = y;
}
public abstract double getArea();
public abstract double getPerimeter();
public abstract boolean intersect(GeometricObject2D g);
#Override
public int compareTo(GeometricObject2D o) {
// TODO Auto-generated method stub
return 0;
}
}
I want to find out possibility of intersecting two circles but there is an error in my code that I didn't realize.
For example I create two circle object coordinates-1(0,0) , radius-1=2 and coordinates-2(0,5) ,radius-2=2. That above method must return false but returns true. I didn't find error.
System.out.println("geoObject1 intersects geoObject3: "
+ geoObject1.intersect(geoObject3));
prints geoObject1 intersects geoObject3: true
As #Pshemo said, your code (now that you've shown it) has an extra } at the end that shouldn't be there.
How, if we paste all that code into IDEONE, and run it, we confirm your error.
If we then DEBUG the code by adding a single print statement, we see:
dx=0.0, dy=0.0, radi=4.0
Hmmm, why is dy = 0 when it should be 5?
Answer: Because you added another set of x and y fields to your subclass, that is hiding the fields from the base class!!!!
Simple debugging would have shown you this yourself. This is what #PeterLawrey was talking about in his comment:
you mistake is it is likely to be; the values are not what you think they are. This is where debugging your code can show this.
Of course, if you had used a good IDE, you wouldn't even need to debug, because the IDE would have warned you about the field hiding.
Rather than Math.pow(x, 2) it is more efficient to do x * x, and instead of using Math.sqrt you can square the sum of the radii.
public boolean intersect(GeometricObject2D g) {
ComparableCircle2D other = (ComparableCircle2D) g;
double dx = other.x - x; // e.g. 0 - 0
double dy = other.y - y; // e.g. 5 - 0
double radii = other.radius + radius; // e.g. 2 + 2
return dx * dx + dy * dy < radii * radii ; // e.g. 0 + 25 < 16 is false.
}
You never assign the fields x and y. Therefore dx = dy = 0.
You have to either assign the field's values or use the fields in the superclass (but you shouldn't have fields with the same information in the same object, so remove the fields created in ComparableCircle2D).
Also if your circle is defined as contour, not as area, then the test for intercepting circles is incorrect. Consider the case where 2 circles with the same center have different radii: dx² + dy² = 0 < dr², but the contours don't intersect; only the areas inside the circles overlap.
So I am trying to move a projectile in the direction that is indicated by the mouse position on the screen. I already have converted the mouse coordinates into in-game coordinates however I can't figure out how to correctly move the projectile into the proper direction. I am trying to use a slope to move the projectile but it doesn't seem to want to grab the correct slope so I end up with them flying in completely wrong directions. Here are some bits of code that I am using.
Any help on this would be greatly appreciated as I am a little over my head in this.
NOTE: The projectile does NOT follow the mouse. It should save the coordinates and then head in that direction noting that it can also go past the given coordinates at the same rate.
Entity Creation
int[] mousePos = MouseManager.getCalculatedMouseCoordinates();
float deltaX = mousePos[0] - GameManager.x;
float deltaY = mousePos[1] - GameManager.y;
float m = deltaY/deltaX;
System.out.println(m);
GameManager.currentWorld.addEntity(new EntityProjectile(GameManager.x, GameManager.y, 30, m, 50, "fireball"));
Projectile Class
package UnNamedRpg.Player.Entity;
public class EntityProjectile {
private double x, y;
private int entityID = -1;
private int speed;
private double headerX, headerY;
private int renderHeading;
private double range, currentRange = 0;
private String texture;
private double factor = -1;
public EntityProjectile(double startX, double startY, int speed, double headerX, double headerY, double range, String texture){
setX(startX);
setY(startY);
setSpeed(speed);
setHeaderX(headerX);
setHeaderY(headerY);
setTexture(texture);
setRange(range);
}
public void doTick(){
double vx = this.x - this.headerX;
double vy = this.y - this.headerY;
if(this.factor == -1){
double length = Math.sqrt((vx*vx) + (vy*vy));
double factor = this.speed / length;
this.factor = factor;
}
vx *= factor;
vy *= factor;
this.x = vx;
this.y = vy;
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
public int getRenderHeading() {
return renderHeading;
}
public void setRenderHeading(int renderHeading) {
this.renderHeading = renderHeading;
}
public int getEntityID() {
return entityID;
}
public void setEntityID(int entityID) {
this.entityID = entityID;
}
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
public String getTexture() {
return texture;
}
public void setTexture(String texture) {
this.texture = texture;
}
public double getRange() {
return range;
}
public void setRange(double range) {
this.range = range;
}
public double getCurrentRange() {
return currentRange;
}
public void setCurrentRange(double currentRange) {
this.currentRange = currentRange;
}
public double getHeaderX() {
return headerX;
}
public void setHeaderX(double headerX) {
this.headerX = headerX;
}
public double getHeaderY() {
return headerY;
}
public void setHeaderY(double headerY) {
this.headerY = headerY;
}
public double getFactor() {
return factor;
}
public void setFactor(double factor) {
this.factor = factor;
}
}
Update Position Method
--Now called in EntityProjectile class every tick instead of it happening in the world tick.
Moving towards a given point is relatively simple with some basic vector math. The vector you want to move along is calculated simply by coordinate subtraction:
vx = objectX - mouseX
vy = objectY - mouseY
But you probably want to move your object a little slower than bam there, so you need to scale the vector to a desired length (equals speed per game tick). The current length of the vector is obtained by the pythagorean sqrt(a * a + b * b). To scale the vector to a given length just multiply the components by the required factor:
double targetLength = 5.0; // chosen arbitrarily
double length = Math.sqrt(vx * vx + vy * vy);
double factor = targetLength / length;
vx *= factor;
vy *= factor;
There you have your speed components x,y to be used as delta per game tick. The 5.0 is the "speed" at which the object will move per tick.
EDIT: #Cyphereion About the length of the vector, thats geometrically speaking the base of a triangle, see https://en.wikipedia.org/wiki/Pythagorean_theorem (considered common knowlegde).
Once you have that, you just need to adjust the length of each component by figuring out a scaling factor that makes the base line come out as the desired "speed" length. The original values of the components (vx, vy) represent a vector (see: https://en.wikipedia.org/wiki/Euclidean_vector#Representations) encoding the direction to move to.
Scaling the vector's length adjusts the speed at which your object moves when you apply the vectors components as delta to its position (which is just vector addition). I swapped around the division length/targetLength initailly (now fixed), so the speed variable had a reversed meaning (larger = slower instead of larger = faster).
If you're having trouble with your sprite then use the setDirection() you have there to augment the trajectory. In these examples you have all good code but you're missing restting the direction when your mouse changes direction. That's all I can see. Good luck!
I have class which represents every object in my simple game (player, enemy, beam etc - they all have many commons like speed, position, dmg). So i made class named Thing. Here is how it looks like:
public abstract class Thing {
private Image image;
private float x;
private float y;
private float speed;
private final int WIDTH;
private final int HEIGHT;
public Thing(String filename, float x, float y, float speed) {
try {
Image image = ImageIO.read(new File(filename));
} catch (Exception e) {}
this.x = x;
this.y = y;
this.speed = speed;
WIDTH = image.getWidth(null);
HEIGHT = image.getHeight(null);
}
//Zwraca ksztalt do sprawdzania czy contains...
public Rectangle2D getShade() {
return new Rectangle2D.Float(x, y, WIDTH, HEIGHT);
}
public Image getImage() {
return image;
}
public Point2D getPoint() {
return new Point2D.Float(x, y);
}
public float getX() {
return x;
}
public float getY() {
return y;
}
}
I have extended the class Player:
public class Player extends Thing {
public Player(String filename, float x, float y, float speed) {
super(filename, x, y, speed);
}
public void moveToPoint(Point2D targetPoint) {
int targetX = (int)targetPoint.getX();
int targetY = (int)targetPoint.getY();
if ( ((int)x+20 < targetX+3) && ((int)x+20 > targetX-3) ) {
return;
}
float distanceX = targetX - x;
float distanceY = targetY - y;
//Dodanie 20px wymiarow statku
distanceX -= 20;
distanceY -= 20;
//Ustalenie wartosci shiftow
float shiftX = speed;
float shiftY = speed;
if (abs(distanceX) > abs(distanceY)) {
shiftY = abs(distanceY) / abs(distanceX) * speed;
}
if (abs(distanceY) > abs(distanceX)) {
shiftX = abs(distanceX) / abs(distanceY) * speed;
}
//Zmiana kierunku shifta w zaleznosci od polozenia
if (distanceX < 0) {
shiftX = -shiftX;
}
if (distanceY < 0) {
shiftY = -shiftY;
}
//Jezeli statek mialby wyjsc poza granice to przerywamy
if ( (((int)x+shiftX < 0) || ((int)x+shiftX > 260)) || ((y+shiftY < 0) || (y+shiftY > 360)) ) {
return;
}
//Zmiana pozycji gracza
x += shiftX;
y += shiftY;
}
}
And here is the problem because my IDE underlines x, y and speed fields red and tells they cannot be accessed from Player class. I tried to change them into private and default but there appears an error after that. What am I doing wrong? When i create new object from class which extends Thing I want to copy all fields and init them as it is said in constructor. So how to repair it?
You need to use getX(), getY() etc., because x,y, speed are private variables for class Thing.
The fact that Player extends Thing doesn't mean Player can access private fields. Thing provided public get... set... to access its private variables.
Change the variables x, y, and speed to protected, or use the accessors getX(), getY(), getSpeed() (getSpeed() needs to be added in this case) to solve the access issues.
The error that appeared after you changed them to default was the fact that you're calling abs(...) instead of Math.abs(...). Change all instances of abs(...) to Math.abs(...) to get rid of the new errors.
So in my programming class we are learning to use draw classes. Basically draw a line and stuff and we did a y=mx+b line in class.
I wanted to jump ahead and start doing more crazy mathematical ones!
I'm having trouble using this one though, which I found on the U of Princeton website.
public class Spiral {
public static void main(String[] args) {
int N = Integer.parseInt(args[0]); // # sides if decay = 1.0
double decay = Double.parseDouble(args[1]); // decay factor
double angle = 360.0 / N;
double step = Math.sin(Math.toRadians(angle/2.0));
Turtle turtle = new Turtle(0.5, 0.0, angle/2.0);
for (int i = 0; i < 10*N; i++) {
step /= decay;
turtle.goForward(step);
turtle.turnLeft(angle);
}
}
}
import java.awt.Color;
public class Turtle {
private double x, y; // turtle is at (x, y)
private double angle; // facing this many degrees counterclockwise from the x-axis
// start at (x0, y0), facing a0 degrees counterclockwise from the x-axis
public Turtle(double x0, double y0, double a0) {
x = x0;
y = y0;
angle = a0;
}
// rotate orientation delta degrees counterclockwise
public void turnLeft(double delta) {
angle += delta;
}
// move forward the given amount, with the pen down
public void goForward(double step) {
double oldx = x;
double oldy = y;
x += step * Math.cos(Math.toRadians(angle));
y += step * Math.sin(Math.toRadians(angle));
StdDraw.line(oldx, oldy, x, y);
}
// pause t milliseconds
public void pause(int t) {
StdDraw.show(t);
}
public void setPenColor(Color color) {
StdDraw.setPenColor(color);
}
public void setPenRadius(double radius) {
StdDraw.setPenRadius(radius);
}
public void setCanvasSize(int width, int height) {
StdDraw.setCanvasSize(width, height);
}
public void setXscale(double min, double max) {
StdDraw.setXscale(min, max);
}
public void setYscale(double min, double max) {
StdDraw.setYscale(min, max);
}
// sample client for testing
public static void main(String[] args) {
double x0 = 0.5;
double y0 = 0.0;
double a0 = 60.0;
double step = Math.sqrt(3)/2;
Turtle turtle = new Turtle(x0, y0, a0);
turtle.goForward(step);
turtle.turnLeft(120.0);
turtle.goForward(step);
turtle.turnLeft(120.0);
turtle.goForward(step);
turtle.turnLeft(120.0);
}
}
Turtle uses this class: StdDraw which is just way too many lines of code for me to paste here.
I keep getting an error when I go to execute spiral:
java.lang.ArrayIndexOutOfBoundsException: 0
at Spiral.main(Spiral.java:4)
Not sure why. Can anyone help me out so I can play around with this?
Did you specify two command-line arguments? It looks like it takes the number of steps and the decay as a parameter and will crash if you don't specify these.
For example:
java Spiral 10 1.1