While loop not breaking inside a for statement - java

I am attempting to write a game where a ball generates for three turns and if the ball makes it past the bottom of the screen the turn is ended.
Below is the class I have written and I can get the ball to move but when it moves past the bottom of the screen the ball does not regenerate in the middle of the screen as I would anticipate.
Apologies if this question is rudimentary but I have worked on this for a bit and seem to be stuck.
private void initiateBall() {
double bx = (WIDTH / 2) - BALL_RADIUS;
double by = (HEIGHT / 2) - BALL_RADIUS;
GOval ball = new GOval(bx, by,BALL_RADIUS, BALL_RADIUS);
ball.setFilled(true);
ball.setColor(Color.BLACK);
vx = rgen.nextDouble(1.0, 1.2);
if (rgen.nextBoolean(0.5)) vx = -vx;
vy = 3.0;
for (int i = 0; i <= NTURNS; i++) {
add (ball);
while (ball.getY() < HEIGHT && ball.getX() < HEIGHT) {
ball.move(vx, vy);
pause(DELAY);
}
}

private void initiateBall() {
for (int k = 0; k < NTURNS; k ++) {
double bx = (WIDTH / 2) - BALL_RADIUS;
double by = (HEIGHT / 2) - BALL_RADIUS;
GOval ball = new GOval(bx, by, BALL_RADIUS, BALL_RADIUS);
ball.setFilled(true);
ball.setColor(Color.BLACK);
vx = rgen.nextDouble(1.0, 1.2);
if (rgen.nextBoolean(0.5)){
vx = -vx;
}
vy = 3.0;
double x = bx;
double y = by;
add(ball, (x), y);
while (ball.getY() < HEIGHT) {
ball.move(vx,vy);
pause(DELAY);
}
}
}

Related

Montecarlo method and JFrame (math and java problem)

I made a little program to approximate Pi and I would like to represent it. I started but I thought it would look better if the points outside the circle were red and the ones inside were green. I can't do it and I don't understand why the problem is there. I don't know if it is in pure math or in dev.
for(int i = 0; i<1000; i++) {
double x = Math.random() * (500-250) + 250;
double y = Math.random() * 250;
double applyformula = (x - 250)*(x - 250) + (y - 250) * (y - 250);
if(applyformula == y || (y*y) - 500 * y < applyformula ) {
g.fillOval((int) x,(int) y, 5, 5);
g.setColor(Color.GREEN);
} else {
g.fillOval((int) x,(int) y, 5, 5);
g.setColor(Color.RED);
}
}
If someone could help me, that will be perfect.
It could be done in a simpler way.
for (int i = 0; i < 1000; i++) {
double x = Math.random();
double y = Math.random();
double applyformula = (x * x) + (y * y);
if (applyformula <= 1) {
g.setColor(Color.GREEN);
} else {
g.setColor(Color.RED);
}
// Set the actual coordinates in a 250 pixels wide square.
x *= 250;
y *= 250;
g.fillOval((int) x, (int) y, 5, 5);
}

How to repaint a specific area (e.g. circle) in java?

I am trying to make a simple game where you basically have to hit the circles to get a point. But i faced a tiny problem which i couldn't really solve on my own so here is my question how do I repaint a round surface.I used the repaint(Rectangle r) method but it doesn't workout.
public void objectHit(MouseEvent e) {
int distance = 0, deltaX = 0, deltaY = 0, RadiusSqaured = 0;
for (int i = 0; i < obj.length; i++) {
deltaX = e.getX() - obj[i].getPoint().x;
deltaY = e.getY() - obj[i].getPoint().y;
distance = deltaX * deltaX + deltaY * deltaY;
RadiusSqaured = obj[i].getRadius() * obj[i].getRadius();
if (distance <= RadiusSqaured) {
repaint(obj[i].repaintRect());
x = ThreadLocalRandom.current().nextInt(50 + radius / 2, 850 - radius / 2);
y = ThreadLocalRandom.current().nextInt(60 + radius / 2, 750 - radius / 2);
repaint(obj[i].repaintRect());
}
}
}
In JComponent, there is a method for repainting based on a box area. Does this satisfy your requirements?
https://docs.oracle.com/javase/8/docs/api/javax/swing/JComponent.html#repaint-long-int-int-int-int-

Aiming in LibGDX using the mouse

I am making a game in libGDX and I am having trouble setting up the Bullet class. I am unable to get the projectiles to go to the mouse location.
I have tried to use Math.atan() to find the angle that I need to fire at but I couldn't get that to work. right now I am just using the distance to find velocity on the x and y-axis.
private static final int SPEED = 500;
private static Texture texture;
String path = "C:\\Users\\minicodcraft\\Downloads\\game\\core\\assets\\";
private float x, y; // starting position
private float xVelocity, yVelocity;
private float yPos; // the y position of the mouse input
private float xPos; // the x position of the mouse input
public Bullet(float x, float y, float yPos, float xPos) {
this.x = x;
this.y = y;
this.xPos = xPos;
this.yPos = yPos;
this.xVelocity = 0f;
this.yVelocity = 0f;
calcDirection();
if (texture == null) {
texture = new Texture(path + "Bullet.png");
}
}
private void calcDirection() {
float xDistanceFromTarget = Math.abs(xPos - x);
float yDistanceFromTarget = Math.abs(yPos - y);
float totalDistanceFromTarget = xDistanceFromTarget + yDistanceFromTarget;
xVelocity = xDistanceFromTarget / totalDistanceFromTarget;
yVelocity = yDistanceFromTarget / totalDistanceFromTarget;
if (xPos < x) {
xVelocity *= -1;
}
if (yPos < y) {
yVelocity *= -1;
}
}
public void update(float deltaTime) {
if (x > 0 && y > 0) {
x += xVelocity * SPEED * deltaTime;
y += yVelocity * SPEED * deltaTime;
} else if (x < 0 && y > 0) {
x -= xVelocity * SPEED * deltaTime;
y += yVelocity * SPEED * deltaTime;
} else if (x > 0 && y < 0) {
x += xVelocity * SPEED * deltaTime;
y -= yVelocity * SPEED * deltaTime;
} else if (x < 0 && y < 0) {
x -= xVelocity * SPEED * deltaTime;
y -= yVelocity * SPEED * deltaTime;
}
}
public void render(SpriteBatch batch) {
batch.draw(texture, x, y);
}
The following code gives a velocity towards the mouse position from the player's position:
float diffX = mouse.x - player.x;
float diffY = mouse.y - player.y;
float angle = (float) Math.atan2(diffY, diffX);
float velX = (float) (Math.cos(angle));
float velY = (float) (Math.sin(angle));
Vector2 velocity = new Vector2(velX, -velY);
velocity.nor();
velocity.scl(magnitudeSpeed);
velocity.scl(deltaTime);
Then velocity.x is the x component of the velocity. Respective for y. No need to multiply by speed and deltaTime again, already done above.

Changing speed of ball dropping... In processing

Here is a program that has a ball drop and bounce from wherever the mouse is clicked. Would anyone know how to change the rate that the ball drops to the force of gravity?
Im trying to figure out the proper solution to this... but i am having a little trouble. All help and or input would be much appreciated.
float x;
float y;
float yspeed = 0;
float xspeed = 0;
float balldiameter = 10;
float ballradius = balldiameter/2;
void setup() {
size (400,400);
background (255);
fill (0);
ellipseMode(CENTER);
smooth();
noStroke();
x = width/2;
y = height/2;
}
void draw() {
mouseChecks();
boundaryChecks();
ballFunctions();
keyFunctions();
}
void mouseChecks() {
if (mousePressed == true) {
x = mouseX;
y = mouseY;
yspeed = mouseY - pmouseY;
xspeed = mouseX - pmouseX;
}
}
void boundaryChecks() {
if (y >= height - ballradius) {
y = height - ballradius;
yspeed = -yspeed/1.15;
}
if (y <= ballradius) {
y = ballradius;
yspeed = -yspeed/1.35;
}
if (x >= width -ballradius) {
x = width -ballradius;
xspeed = -xspeed/1.10;
}
if (x <= ballradius) {
x = ballradius;
xspeed = -xspeed/1.10;
}
}
void ballFunctions() {
if (balldiameter < 2) {
balldiameter = 2;
}
if (balldiameter > 400) {
balldiameter = 400;
}
ballradius = balldiameter/2;
background(255); //should this be in here?
ellipse (x,y,balldiameter,balldiameter);
yspeed = yspeed += 0.2;
xspeed = xspeed/1.005;
y = y + yspeed;
x = x + xspeed;
}
void keyFunctions() {
if (keyPressed) {
if(keyCode == UP) {
balldiameter +=1;
}
if (keyCode == DOWN) {
balldiameter -=1;
}
}
}
The acceleration of gravity on earth is 9.81 m/s^2. So, if your ball's velocity is 0 at the point the mouse is clicked, the final velocity will be the acceleration integrated in relation to time. This would be (9.81 * t) / 2. Where t is in seconds. The resulting units would be m/sec. You would have to convert meters to some screen space unit for drawing.

How can I make my image bounce along the x-axis?

I have an image which moves horizontally with a given speed over a delta time(dt). But the problem is,the image doesn't bounces off when it reaches the end of the size of the world.How can i make the image bounces off backward so it would be kept inside the world?
Any help will do.
Here's what i've tried so far:
#Override
public void move(long dt)
{
// v = dx / dt
// dx m = v m/s . dt s
double dt_s = dt / 1e9;
double dx_m = speed * dt_s;
double left_wall = 0;
double right_wall = board.x1_world;
if (x <= right_wall)
{
x += dx_m;
if (x >= right_wall)
{
x = right_wall;
x *= -dx_m;
}
}
}
#Override
public void move(long dt)
{
double dt_s = dt / 1e9;
double dx_m = speed * dt_s;
double left_wall = 0;
double right_wall = board.x1_world;
x += dx_m;
if (x <= 0) speed *= -1.0;
if (x >= right_wall) speed *= -1.0;
}
When the x coordinate of your images reach an border, just change the orientation of the horizontal speed (multiply it for -1). But you should use an condition like this:
if (x >= (right_wall - width_of_image)) speed *= -1.0;
Instead of just x >= right_wall, because doing so, the image will bounce when it "touchs" the end of the world.
In addition to checking each end separately, as suggested by #Oscar, you may need to account for the image's finite width, as shown in this Subway simulation.
private boolean goleft=false;//keep direction
#Override
public void move(long dt)
{
// v = dx / dt
// dx m = v m/s . dt s
double dt_s = dt / 1e9;
double dx_m = speed * dt_s;
double left_wall = 0;
double right_wall = board.x1_world;
if(goleft)x += dx_m;
else x-= dx_m;
if (x >= right_wall)//touching 1 wall
{
x = right_wall;
x += dx_m;
goleft=true;
}else if(x<=left_wall){//touching the other wall
x = left_wall;
x += dx_m;
goleft=false;
}
}

Categories