Jump to content
  • Advertisement
Sign in to follow this  
xAzrael

Collision

This topic is 2544 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey guys,

I posted early on how to build a program after learning the syntax. And so I decided too, so far I have gotten a bunch of shapes moving across a JFrame in Java. Now, I really want to add collision. After extensive searching on GameDev, I couldn't find exactly what I was looking to do, maybe someone could help me?


Here's my code:


import javax.swing.*;
import java.awt.*;
public class SimpleAnimation {
int x = 150;
int y = 250;

int xPos = 1150;
int yPos = 80;

int aPos = 150;
int bPos = 50;

int rPos = 10;
int zPos = 250;

private static final int WIDTH = 1280, HEIGHT = WIDTH / 2;
public void go() {
JFrame frame = new JFrame();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MyDrawPanel drawPanel = new MyDrawPanel();
frame.getContentPane().add(drawPanel);
frame.setSize(WIDTH, HEIGHT);
frame.setResizable(false);
frame.setVisible(true);
// speed
for (int i = 0; i < 270; i++) {
x += 55;
x -= 40;
yPos += 55;
yPos -= 40;

if (yPos > 800) {
yPos = yPos + 10;
}

aPos+= 20;
bPos++;

rPos += 5;


drawPanel.repaint();
try {
Thread.sleep(50);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
class MyDrawPanel extends JPanel {

private static final long serialVersionUID = 1L;
public void paintComponent(Graphics g) {
g.setColor(Color.BLACK);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
g.setColor(Color.GREEN);
g.fillOval(x, y, 40, 40);
g.setColor(Color.WHITE);
g.fillRect(xPos, yPos, 40, 75);

g.setColor(Color.YELLOW);
g.fillArc(aPos, bPos, 1000, 500, 50, 50);

g.setColor(Color.WHITE);
g.fillRect(xPos - 500, yPos - xPos / 2, 40, 75);

g.setColor(Color.WHITE);
g.fillRect(xPos * 2, yPos * 2, 40, 75);

g.setColor(Color.WHITE);
g.fillRect(rPos, zPos, 40, 75);

g.setColor(Color.YELLOW);
g.fillOval(xPos - 100, yPos - 1000, 75, 75);

g.setColor(Color.BLACK); // top wall
g.fillRect(0, 0, 1280, 2);

g.setColor(Color.BLACK); // bottom wall
g.fillRect(1, 610, 1280, 2);

g.setColor(Color.BLACK); // left wall
g.fillRect(0, 0, HEIGHT, 615);

g.setColor(Color.BLACK); // Right wall
g.fillRect(1273, 1, 2, 615);
}
}
public static void main(String... args) {
SimpleAnimation gui = new SimpleAnimation();
gui.go();
}
}


In the paintComponent at the bottom I tried to draw 4 new shapes and make them as walls...Beyond that, I have no idea what I am doing or even if that was the right thing to do...The plan is, to have every shape bounce off the wall it collides with, so maybe someone could help me understand how to do it?

Share this post


Link to post
Share on other sites
Advertisement
If all you want to do is make them bounce off walls, you don't need to create objects for them to bounce off of. All you need to do is check the bounds of the screen against the bound of the shape and change the way it is moving:


import javax.swing.*;
import java.awt.*;
public class SimpleAnimation {
private boolean has_quit = false; // set to true when you want to quit

private int x = 150;
private int y = 250;
private int w = 50;
private int h = 50;
private int[] vel = {10, 10};

private static final int WIDTH = 1280, HEIGHT = WIDTH / 2;

public void go() {
JFrame frame = new JFrame();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MyDrawPanel drawPanel = new MyDrawPanel();
frame.getContentPane().add(drawPanel);
frame.setSize(WIDTH, HEIGHT);
frame.setResizable(false);
frame.setVisible(true);
while (!has_quit) {
// move the object
x += vel[0];
y += vel[1];

// check to see if the object went off screen and which side it went off
// - check left and right
if (x < 0) { // if the left side of the box went off the left side of the screen
x = 0; // put it back on screen
vel[0] *= -1; // reverse the x velocity

} else if (x + w > drawPanel.getWidth()) { // if the right side of the box went off the right side of the screen
x = drawPanel.getWidth()- w; // put it back on screen
vel[0] *= -1; // reverse the x velocity
}
// - check top and bottom
if (y < 0) { // if the top side of the box went off the top side of the screen
y = 0; // put it back on screen
vel[1] *= -1; // reverse the y velocity

} else if (y + h > drawPanel.getHeight()) { // if the bottom side of the box went off the bottom side of the screen
y = drawPanel.getHeight()- h; // put it back on screen
vel[1] *= -1; // reverse the y velocity
}
drawPanel.repaint();
try {
//Thread.sleep(50); // sleeping for 50 is way too long. That only allows for, at most, 20 frames per second
Thread.sleep(20);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}


class MyDrawPanel extends JPanel {

private static final long serialVersionUID = 1L;

public void paintComponent(Graphics g) {
g.setColor(Color.BLACK);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
g.setColor(Color.WHITE);
g.fillRect(x, y, w, h);

}
}

public static void main(String... args) {
SimpleAnimation gui = new SimpleAnimation ();
gui.go();
}
}

Share this post


Link to post
Share on other sites

Hey guys,

I posted early on how to build a program after learning the syntax. And so I decided too, so far I have gotten a bunch of shapes moving across a JFrame in Java. Now, I really want to add collision. After extensive searching on GameDev, I couldn't find exactly what I was looking to do, maybe someone could help me?



The answer depends on what you want to do. Are you doing this to begin general modeling of terrain/structures? If so, one really simple way this can be done is the following:

- Create a couple of classes to represent entities in your game. For the things you want your objects to bounce off of, a concept such as Terrain or Platform would be appropriate.
- Each entity would have x, y, width, and height.
- Since this is Java, you can use Rectangle (http://docs.oracle.c.../Rectangle.html) to represent the geometry object.
- Maintain a list of platforms and a list of your moving objects.
- On every iteration of your game loop, for each object that is moving, loop through the list of platforms and check if they are intersecting (you can use Rectangle.intersects() and .intersection() for this: http://docs.oracle.c...a.awt.Rectangle).
- Then you need to figure out how to respond to a collision. Depending on what you want to do, this can be very simple or very complex.

This is pattern stupidly simple and will not scale well without modification, but the concept works and should be good enough for a prototype.

Hope that helps.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!