Question: So I have created a program with a Window class that creates a JFrame and adds a JPanel from my DrawStuff class onto it. The DrawStuff class creates a ball that (is supposed to) bounce around the screen and when it hits the JFrame boundaries, change direction. The ball moves but for some reason the checkbounds part of my move method does not work. Any help would be greatly appreciated. My goal is to keep the ball in bounds.
Code from DrawStuff class:
public class Drawstuff extends JPanel {
private int x = 0;
private int y = 0;
private int dx, dy=0;
public Drawstuff(){
x = 300;
y = 250;
dx = 0;
dy = 0;
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
this.setBackground(Color.BLACK);
g2d.setColor(Color.RED);
g2d.fillOval(x,y,50,50);
}
public void move(){
if (x < 600){
dx = 1;
}
if (y < 500){
dy = 1;
}
if (x > 600){
dx = -1;
}
if (y >500){
dy = -1;
}
x += dx;
y += dy;
}
}
Simple "GameLoop" from Window Class (if needed)
while (true){
stuff.repaint();
stuff.move();
try {
Thread.sleep(5);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Your move is wrong. The logic of it is false. You want the ball to bounce so make its movement reverse when it hits a wall. What you've done is : if it is outside get it inside and when inside try to get it outside! Change to:
public void move(){
if (x < 0) { // left bound
dx = 1; // now move right
}
if (y < 0){ // upper bound
dy = 1; // now move down
}
if (x > 600){// right bound
dx = -1; // now move left
}
if (y >500){ // lower bound
dy = -1; // now move up
}
x += dx;
y += dy;
}
For future use, I can suggest you to do it the following way :
public void move(){
if (x < 0) { // left bound
dx = -dx; // now move right, same speed
x=-x; // bounce inside
}
if (y < 0){ // upper bound
dy = -dy; // now move down, same speed
y=-y; // bounce inside
}
if (x > 600){ // right bound
dx = -dy; // now move left, same speed
x=600-(x-600); // bounce inside
}
if (y >500){ // lower bound
dy = -dy; // now move up, same speed
y=500-(y-500); // bounce inside
}
x += dx;
y += dy;
}
So you can now use any vector speed you want (at least reasonable ones). Vector coordinates are reversed accordingly to the hit, and the ball is relocated inside the bounds.
Related
I want to create an ArrayList of ball objects, which should be in a loop until there are 100 pieces.
Now my problem: I must implement a function hitTest, so that when you click on a ball it gets removed. In the same position, there should appear two balls then, which go into a different direction.
Can someone help me? I am so lost...
Here's my code so far:
Ball b;
ArrayList<Ball> balls;
void setup() {
size(800, 800);
balls = new ArrayList<Ball>();
for (int i = 0; i<100; i++) {
drawBall();
}
}
void draw() {
background(255);
//b.update();
for (int i= 0; i<balls.size(); i++) {
balls.get(i).update();
}
}
void drawBall() {
Ball b = new Ball();
balls.add(b);
}
class Ball {
private float x;
private float y;
private float ballSize;
private float dirX;
private float dirY;
private boolean moving = true;
Ball() {
this.x = width/2;
this.y = height/2;
this.ballSize = random(10.0, 30.0);
this.dirX = random(-3.0, 3.0);
this.dirY = random(-3.0, 3.0);
if (this.dirX<1.0 && this.dirX>1.0) //1 statt -1 macht zufälliger { this.dirX = 1.0; }
if (this.dirY<1.0 && this.dirY>1.0) {
this.dirY = 1.0;
}
}
public void update() {
stroke(255);
fill(random(255), random(255), random(255), random(255));
ellipse( this.x, this.y, this.ballSize, this.ballSize);
if (this.moving == true) {
this.x += this.dirX;
this.y += this.dirY;
}
if (this.x+ this.ballSize/2> width ||this.x- this.ballSize/2<0) {
this.dirX= dirX*-1;
}
if (this.y+ this.ballSize/2> height ||this.y- this.ballSize/2<0) {
this.dirY= dirY*-1;
}
}
}
Break your problem down into smaller, simpler steps.
e.g.
when you click on a ball, it gets removed. In the same position, there should appear two balls then, which go into a different direction.
when you click on a ball: you can mix the dist() function (to check if the distance between the mouse and a ball is smaller then the radius) with mouseClicked() (or mousePressed() / mouseReleased())
it gets removed: you already called balls.add(). Similarly you can call balls.remove() passing the ball object or index to remove (depending on the case)
same position: you need to remember (store the coordinates) of the ball that was clicked to add two balls at the same position
different direction: you already do that in the Ball() constructor: you can apply the same logic on each new ball.
Here's a basic sketch to illustrate point 1, using dist():
void draw(){
background(255);
int ballX = 50;
int ballY = 50;
int ballRadius = 35;
if(dist(ballX, ballY, mouseX, mouseY) < ballRadius){
fill(0,192,0);
}else{
fill(192,0,0);
}
ellipse(ballX,ballY, ballRadius * 2, ballRadius * 2);
}
Paste this in a new sketch, run it and you should get the hang of using dist() in the context of your problem.
Regarding points 2,3,4 here's a modified version of your sketch with comments and a slightly different approach: instead of removing a ball to add a new one at the exact location with a different direction, simply randomise the direction. Visually it will look similar to a new ball (except the random size/colour). With the clicked ball being re-used, only a second one is added:
Ball b;
ArrayList<Ball> balls;
void setup() {
size(800, 800);
balls = new ArrayList<Ball>();
for (int i = 0; i<100; i++) {
drawBall();
}
}
void draw() {
background(255);
//b.update();
for (int i= 0; i<balls.size(); i++) {
// pass the mouse coordinates to each ball to check if it's hovered or not
balls.get(i).update(mouseX, mouseY);
}
}
// on mouse pressed
void mousePressed(){
for (int i= 0; i<balls.size(); i++) {
// make current ball reusable in this loop
Ball ball = balls.get(i);
// if ball is hovered
if(ball.isHovered){
// randomize direction of current ball
ball.setRandomDirection();
// add a new ball from the current location
balls.add(ball.copy());
}
}
}
void drawBall() {
Ball b = new Ball();
balls.add(b);
}
class Ball {
private float x;
private float y;
private float ballSize;
private float dirX;
private float dirY;
private boolean moving = true;
private color fillColor;
// property to keep track if the ball is hovered or not
private boolean isHovered;
Ball() {
this.x = width/2;
this.y = height/2;
this.ballSize = random(10.0, 30.0);
this.setRandomDirection();
this.fillColor = color(random(255), random(255), random(255), random(255));
}
// extract random direction calls into a re-usable function (handy for click / collision)
void setRandomDirection(){
this.dirX = random(-3.0, 3.0);
this.dirY = random(-3.0, 3.0);
if (this.dirX<1.0 && this.dirX>1.0) { //1 statt -1 macht zufälliger { this.dirX = 1.0; }
if (this.dirY<1.0 && this.dirY>1.0) {
this.dirY = 1.0;
}
}
}
public void update(int x, int y) {
// euclidean distance between this ball's coordinates a given x y position (e.g. mouse)
isHovered = dist(this.x, this.y, x, y) < this.ballSize / 2;
// optional: use stroke color to visually display highlighted ball
if(isHovered){
stroke(0);
}else{
stroke(255);
}
fill(fillColor);
ellipse( this.x, this.y, this.ballSize, this.ballSize);
if (this.moving == true) {
this.x += this.dirX;
this.y += this.dirY;
}
if (this.x + this.ballSize / 2 > width ||
this.x - this.ballSize / 2 < 0) {
this.dirX= dirX*-1;
}
if (this.y + this.ballSize / 2 > height ||
this.y - this.ballSize / 2 < 0) {
this.dirY= dirY*-1;
}
}
// utility function: simply copies this ball's x,y position to the new one
Ball copy(){
Ball clone = new Ball();
clone.x = this.x;
clone.y = this.y;
return clone;
}
}
The copy() method is flexible enough that it's ease to remove one ball to add two more if that is an absolute must. For example:
// on mouse pressed
void mousePressed(){
for (int i= 0; i<balls.size(); i++) {
// make current ball reusable in this loop
Ball ball = balls.get(i);
// if ball is hovered
if(ball.isHovered){
// add two new balls from the current location
balls.add(ball.copy());
balls.add(ball.copy());
// remove ball
balls.remove(i);
}
}
}
I am currently working on a code to connect ellipses with lines. I have been able to connect each ellipse with the previous. However, i cannot connect the last ellipse and the original (first) ellipse.
I would like to create an array that will
The Ellipses are drawn with the centre being the co-ordinates of where the mouse was clicked.
(Using Processing to code this program)
PS: sorry for bad formatting on the question, this is my first time asking a query.
I have researched on how to use arrays but it is still a bit confusing to me and thus i am using single integers for each point at the moment.
//SET GLOBAL VARIABLES
final int N_PARTITIONS = 10;
int PrevX = -1;
int PrevY = -1;
int count = 0;
int gridx = 0;
int gridy = 0;
int OriginalX = mouseX;
int OriginalY = mouseY;
//CREATING WINDOW SIZE
void setup() {
size(600, 360);
surface.setResizable(true);
}
void draw() {
}
//DRAWING ELLIPSE AND CONNECTING LINES
void mouseClicked() {
count++;
CallEllipse();
if (PrevX != -1) {
line(PrevX, PrevY, mouseX, mouseY);
}
if (count >= 3) {
line(OriginalX, OriginalY, PrevX, PrevY);
}
PrevX = mouseX;
PrevY = mouseY;
}
void CallEllipse() {
ellipse(mouseX, mouseY, N_PARTITIONS, N_PARTITIONS);
}
this is the result of the coding. I am not sure why the line is coming from the top left corner.
try to Add this code in the mouseClicked() method:
void mouseClicked() {
if(count == 0) {
OriginalX= mouseX;
OriginalY=mouseY;
}
count++;
CallEllipse();
if (PrevX != -1) {
line(PrevX, PrevY, mouseX, mouseY);
}
if (count >= 3) {
line(OriginalX, OriginalY, mouseX, mouseY);
}
PrevX = mouseX;
PrevY = mouseY;
}
Enjoy
So we have to make a ball bounce with the code given. Basically the translate method below is run every 1 second because of a timer method in the tester. Also the tester passes dx as 0 and dy as 1. Initially the ball moves down and I am trying to make it bounce back up once it reaches y = 100. I can't figure out why this code would not work because logically it makes sense to me...
public void translate(int dx, int dy) {
x += dx;
y += dy;
if(y >= 100){
dy = -1 * dy;
}
}
I run it with this code, the ball keeps moving down.
y = y direction of the ball and x = x direction of the ball
Update: Thanks for the answers. So from what I am getting I need to add the if statements inside the method that calls the translate method. The code for the method calling translate is:
private void moveMyShape() {
Timer t = new Timer(DELAY, getActionListener());
t.start();
} //method
private ActionListener getActionListener() {
return new
ActionListener() {
#Override
public void actionPerformed(ActionEvent ae) {
myMoveableShape.translate(0, 1);
myShape.repaint();
}
};
}
So how would I go about adding if statements in this method? Like how can I keep track of the y position of the ball so I can add the if statements above the translate method. By the way this actionListener code is in a different class. It is in a tester class.
2nd Update: Yes, I have a public static int getY() inside the myMoveableShape. In the tester I added this code:
public void actionPerformed(ActionEvent ae) {
if(MyMoveableShape.getY() > 100 || MyMoveableShape.getY() < 0){
myMoveableShape.translate(0,-1);
}
myMoveableShape.translate(0,1);
myShape.repaint();
But the ball just gets to y = 100 position and stops moving.
When you pass the int into the translate method it is 'by value', not 'by reference' so any changes you make to the value will not last beyond the scope of the method.
You need to do the the check of y, and the reversal of it, higher up in your code, likely in the same method where you are calling translate from.
Update:
Add a 'velocity' property to your class, something like
private int velocityY = 1;
public int getVelocityY() {
return velocityY;
}
public void setVelocityY(int vel) {
velocityY = vel;
}
And then you can modify that block to be something similar to
public void actionPerformed(ActionEvent ae) {
if(MyMoveableShape.getY() > 100 || MyMoveableShape.getY() < 0){
myMoveableShape.setVelocityY(-myMoveableShape.getVelocityY());
}
myMoveableShape.translate(0,myMoveableShape.getVelocityY());
myShape.repaint();
}
Update 2: Based on your comments, you could give this a whirl:
private boolean goingUp = false;
public void translate(int dx, int dy) {
x += dx;
if(goingUp){
y += dy;
} else {
y -= dy;
}
if(y >= 100 || y < 0){
goingUp = !goingUp; //Toggle it back and forth
}
}
try putting your if statement above the += statement.
Like:
public void translate(int dx, int dy)
{
if(y >= 100)
{
dy = -1 * dy;
}
x += dx;
y += dy;
}
I'm sure i'm missing something like are y and x static? need more code to do any better.
I am making a 2d rpg game in java and I have run into a problem. I can make the player move around the stage and I have rocks, trees, walls, etc. on the stage as well. I don't know how to detect the collision and make it to where the player can't move through the object. The code that reads map file and draws image on the canvas is as follows:
public void loadLevel(BufferedImage levelImage){
tiles = new int[levelImage.getWidth()][levelImage.getHeight()];
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
Color c = new Color(levelImage.getRGB(x, y));
String h = String.format("%02x%02x%02x", c.getRed(),c.getGreen(),c.getBlue());
switch(h){
case "00ff00"://GRASS Tile - 1
tiles[x][y] = 1;
break;
case "808080"://Stone -2
tiles[x][y] = 2;
break;
case "894627"://Dirt -3
tiles[x][y] = 3;
break;
case "404040"://Rock on Grass -4
tiles[x][y] = 4;
break;
case "00b700"://Tree -5
tiles[x][y] = 5;
break;
case"000000"://Wall -6
tiles[x][y] = 6;
break;
case "cccccc"://Rock on stone -7
tiles[x][y] = 7;
break;
default:
tiles[x][y] = 1;
System.out.println(h);
break;
}
}
}
}
And the player class is as follows:
public class Player {
private int x,y;
public int locx,locy;
private Rectangle playerR;
private ImageManager im;
public boolean up =false,dn = false,lt=false,rt=false,moving = false,canMove = true;
private final int SPEED =2;
public Player(int x, int y, ImageManager im){
this.x = x;
this.y = y;
this.im = im;
locx = x;
locy = y;
playerR = new Rectangle(x,y,16,16);
}
public void tick(){
if (up) {
if(canMove){
y -= SPEED;
locx = x;
locy = y;
playerR.setLocation(locx, locy);
moving = true;
}
else{
y += 1;
canMove=true;
}
}
if (dn) {
y +=SPEED;
locx = x;
locy = y;
moving = true;
}
}
if (lt) {
x -= SPEED;
locx = x;
locy = y;
moving = true;
}
if (rt) {
x+=SPEED;
locx = x;
locy = y;
moving = true;
}
}
if(moving){
System.out.println("PLAYER\tX:"+locx+" Y:"+locy);
moving = false;
}
}
public void render(Graphics g){
g.drawImage(im.player, x, y, Game.TILESIZE*Game.SCALE, Game.TILESIZE*Game.SCALE, null);
}
}
I don't really know how to do collision, but i googled it and people said to make a rectangle for the player and all the objects that the player should collide with, and every time the player moves, move the player's rectangle. Is this the right way to do this?
EDIT EDIT EDIT EDIT
code for when collision is true:
if (rt) {
x+=SPEED;
locx = x;
locy = y;
playerR.setLocation(locx, locy);
for(int i = 0;i<Level.collisions.size();i++){
if(intersects(playerR,Level.collisions.get(i))==true){
x-=SPEED;
locx = x;
playerR.setLocation(locx, locy);
}
}
moving = true;
}
And the intersects method is as follows:
private boolean intersects(Rectangle r1, Rectangle r2){
return r1.intersects(r2);
}
I'm going to focus on your tick method since that is where most of this logic is going. There are a couple changes here. Most notably, we only move the rectangle before checking for collisions. Then loop through all the collideable objects in your level. Once one is found, we reset our x and y and break out of the loop (no sense in looking at any of the other objects since we already found the one we collided with). Then we update our player position. By doing it this way, I centralized the code so it is not being repeated. If you ever see yourself repeating code, there is a pretty good chance that it can be pulled out to a common place, or to a method.
public void tick() {
if (up) {
y -= SPEED;
} else if (dn) {
y += SPEED;
} else if (lt) {
x -= SPEED;
} else if (rt) {
x += SPEED;
}
playerR.setLocation(x, y);
for (Rectangle collideable : Level.collisions) {
if (intersects(playerR, collideable)) {
x = locx;
y = locy;
playerR.setLocation(x, y);
break;
}
}
locx = x;
locy = y;
}
There are different ways to do that. As you talk about a "rpg" i think your view is Isometric (45° top down).
I would do the collision detection in pure 90° top down, as it is easier and, imho, more realistic.
We have 2 possibilities:
Move your Player to the next position. If there is a collision, reset his position.
Calculate the next position, if there would be a collision don't move.
If you want to have a "gliding" collision response, you have to check in which axis the collision will happen, and stop / reset movement for this axis only.
To have a more efficient collision detection only check near objects, which will possibly collide.
Do this by comparing a squared "dangerRadius" with the squared distance between your player and the object:
if ((player.x - object.x)² + (player.y - object.y)² <= dangerRadius²)
// Check for intersection
This will sort out most of the objects by using a simple calculation of:
2 subtractions
1 addition
3 multiplications (the ²)
1 compare (<=)
In your game you should sepparate the logic and the view. So basicly you don't detect, if the two images overlapp, but you check, if the objects in your logic overlap. Then you draw the images on the right position.
Hope this helps.
EDIT: Important: If you update your character, depending on the time between the last and this frame (1/FPS) you have to limit the max timestep. Why? Because if for some reason (maybe slow device?) the FPS are really low, it is possible, that the character moves verry far between 2 frames and for that he could go through an object in 1 frame.
Also if you simply reset the movement on collision or just don't move the distance between you and the object could be big for low FPS. For normal FPS and not to high movementspeed this won't happen/ be noticeable.
I personally am fairly new to Java, though I have worked with C# in the past. I am making a similar game, and for collision detection I just check the locations of the player and objects:
if (z.gettileX() == p.gettileX()){
if (z.gettileY() == p.gettileY()){
System.out.println("Collision!");
}
}
If the player (p) has equal X coordinates and Y coordinates to z(the bad guy), it will send this message and confirm that the two have, in fact, collided. If you can make it inherent in the actual class behind z to check if the coordinates a equal, you can create an unlimited number of in-game objects that detect collision and react in the same way, i.e. walls.
This is probably what your looking for. I've made this class spicificly for collision of multiple objects and for individual side collisions.
abstract class Entity {
private Line2D topLine;
private Line2D bottomLine;
private Line2D leftLine;
private Line2D rightLine;
private Rectangle rectangle;
private Entity entity;
protected boolean top;
protected boolean bottom;
protected boolean left;
protected boolean right;
protected int x;
protected int y;
protected int width;
protected int height;
public Entity(int x, int y, int width, int height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
updateLinesAndRects();
}
public void updateLinesAndRects() {
topLine = new Line(x + 1, y, width - 2, 0);
bottomLine = new Line(x + 1, y + height, width - 2, height);
leftLine = new Line(x, y + 1, 0, height - 2);
rightLine = new Line(x + width, y + 1, 0, height - 2);
rectangle = new Rectangle(x, y, width, height)
}
public void setCollision(Entity entity) {
this.entity = entity;
top = isColliding(new Line2D[]{topLine, bottomLine, leftLine, rightLine});
bottom = isColliding(new Line2D[]{bottomLine, topLine, leftLine, rightLine});
left = isColliding(new Line2D[]{leftLine, topLine, bottomLine, rightLine});
right = isColliding(new Line2D[]{rightLine, topLine, bottomLine, leftLine});
}
public void updateBounds() {
if(top) y = entity.y + entity.height;
if(bottom) y = entity.y - height;
if(left) x = entity.x + entity.width;
if(right) x = entity.x - width;
}
public boolean isColliding() {
return rectangle.intersects(entity.rect);
}
private boolean isLinesColliding(Line2D[] lines) {
Rectangle rect = entity.getRectangle();
return lines[0].intersects(rect) && !lines[1].intersects(rect) && !lines[2].intersects(rect) && !lines[3].intersects(rect);
}
private Line2D line(float x, float y, float width, float height) {
return new Line2D(new Point2D.Float(x, y), new Point2D.Float(x + width, x + height));
}
public Rectangle getRectangle() {
return rectangle;
}
}
Example:
class Player extends Entity{
Entity[] entities;
public Player(int x, int y, int width, int height) {
super(x, y, width, height);
}
public void update() {
updateLinesAndRects();
for(Entity entity : entities) {
setCollision(entity);
if(top) system.out.println("player is colliding from the top!");
if(isColliding()) system.out.println("player is colliding!");
updateBounds(); // updates the collision bounds for the player from the entities when colliding.
}
}
public void setEntities(Entity[] entities) {
this.entities = entities;
}
}
I'm writing a program using Processing but I keep getting Expecting TRIPLE_DOT, found ';'.
What could be wrong?
class Collision {
Ball ball = new Ball();
Block block = new Block();
int ball_xpos;
int ball_rad;
int ball_ypos;
int block_width;
int block_height;
int block_control;
Collision(ball.xpos, ball.rad, ball.ypos, block.width, block.height, block.control){
//
}
void detect_() {
//not done yet
}
}
Ball class:
class Ball {
int rad = 30; // Width of the shape
float xpos, ypos; // Starting position of shape
float xspeed = 2.8; // Speed of the shape
float yspeed = 2.2; // Speed of the shape
int xdirection = 1; // Left or Right
int ydirection = 1; // Top to Bottom
Ball() {
ellipseMode(RADIUS);
// Set the starting position of the shape
xpos = width/2;
ypos = height/2;
}
void display() {
ellipseMode(CENTER);
ellipse(xpos, ypos, 410, 40);
}
void move() {
// Update the position of the shape
xpos = xpos + ( xspeed * xdirection );
ypos = ypos + ( yspeed * ydirection );
// Test to see if the shape exceeds the boundaries of the screen
// If it does, reverse its direction by multiplying by -1
if (xpos > width-rad || xpos < rad) {
xdirection *= -1;
}
if (ypos > height-rad || ypos < rad) {
ydirection *= -1;
}
// Draw the shape
ellipse(xpos, ypos, rad, rad);
}
}
In the parameter names in your constructor, the dots (.) should be replaced with _. And you should give types to those parameters:
Collision(int ball_xpos, int ball_rad, ... so on){
//
}
If you use ball.xpos, then compiler expects a var-args after the 1st dot(.) after ball.
But it seems like want to pass attributes of Ball class, to initialize the fields with the Ball class attribute. In that case, you should just pass a single parameter, that is a reference to Ball:
Collision(Ball ball) {
this.ball = ball;
}
But I don't see why at all you are having those fields (ball_xpos, ball_ypos) in Collision class, given that you also have a Ball type field. You can remove them, and just set ball reference to the reference passed in the above constructor.
Same thing for Block type reference. You are simply having copy of the fields of Block and Ball in Collision class again. Not needed.