-
Collision Detection between Sprites and Images?
Hello everyone :)
I am creating a small game and have come to the point where I need to use collision detection. The problem is, I do not exactly know how to create basic collision with rectangles. I now it requires you to import java.awt.Rectangle but thats about it as far as what has worked for me.
Code :
package outrun;
import java.awt.Image;
import javax.swing.ImageIcon;
public class Cobblestone {
private String cobblestone = "/resources/cobblestone_outrun.png";
private Image image;
public Cobblestone() {
ImageIcon i = new ImageIcon(this.getClass().getResource(cobblestone));
image = i.getImage();
}
public Image getImage() {
return image;
}
}
This is my cobblestone class. The image is 20x20 pixels, I need collision for this.
Code :
package outrun;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.Rectangle;
import javax.swing.ImageIcon;
public class Person {
private String person = "/resources/person_outrun.png";
private int dx;
private int dy;
private int x;
private int y;
private Image image;
public Person() {
ImageIcon i = new ImageIcon(this.getClass().getResource(person));
image = i.getImage();
x = 31;
y = 175;
}
public void move() {
x += dx;
y += dy;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public Image getImage() {
return image;
}
public void KeyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_RIGHT) {
dx = 1;
}
if (key == KeyEvent.VK_LEFT) {
dx = -1;
}
if (key == KeyEvent.VK_UP) {
dy = -1;
}
if (key == KeyEvent.VK_DOWN) {
dy = 1;
}
}
public void KeyReleased(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_RIGHT) {
dx = 0;
}
if (key == KeyEvent.VK_LEFT) {
dx = 0;
}
if (key == KeyEvent.VK_UP) {
dy = 0;
}
if (key == KeyEvent.VK_DOWN) {
dy = 0;
}
}
}
This is my person class, I also need collision for this.
Do I determine the collision method in my board.class? What I wish to happen is if the person intersects the cobblestone, the person stops (dx = 0 and dy = 0). How would I go about doing this? Thanks! Any help would be greatly appreciated!
-
Re: Collision Detection between Sprites and Images?
The Rectangle class has methods for detecting intersection that might be useful.
-
Re: Collision Detection between Sprites and Images?
Oh yes, but how would I go about creating a rectangle for the person, it moves? The parameters consist of x and y values. How would I be able to create one if the person is moving, hence constantly changing x and y values? I am confused as to how to use the rectangle class.
-
Re: Collision Detection between Sprites and Images?
Is the person a point without width and height? The person's rectangle would be its x,y & width and height.
-
Re: Collision Detection between Sprites and Images?
The person has width and height, it is an image I created. Would I need to use getWidth() and getHeight() and assign them to two new variables?
-
Re: Collision Detection between Sprites and Images?
Yes, you would need the width and height to define the rectangle that surrounds the person.
-
Re: Collision Detection between Sprites and Images?
Ok I will work on that, Eclipse somehow does not want to open my files stating that the files do not exist. I will resume work once I get that sorted from someone.
-
Re: Collision Detection between Sprites and Images?
Ok, so I have made rectangle classes for both cobblestone.java and person.java (the images I wish to intersect.) Now I am actually determining the collision in my board.java:
Code :
public void checkCollisions() {
Rectangle r1 = person.getBounds();
Rectangle r2 = cobblestone.getBounds();
if (r1.intersects(r2)) {
}
}
The problem is I do not know what to put within my if statement. dx and dy control the 'speed' or translation of the person in my person class through coordinates. In other words, they control the movement of my person. How do I implement these variables in my board.java?
-
Re: Collision Detection between Sprites and Images?
Quote:
what to put within my if statement
It all depends on what you want to happen when there is a collision. Define that and then you can work on the code to implement it.
-
Re: Collision Detection between Sprites and Images?
I know what I want to happen, I just don't know how to show it. If they both collide, I want the variables dx and dy to be equal to zero. The trouble is those two variables are in the person class, not the board class, where I want them to be uses also. How would I be able to do that?
-
Re: Collision Detection between Sprites and Images?
To change variables in a class, add a method to the class that you can call to change them.
Not sure what you mean by "If they both collide". It takes two objects to have a collision.
-
Re: Collision Detection between Sprites and Images?
What I meant by was if they both collide with each other, sorry, I wasn't to clear! I do not want to change the variables. I want to use the variable dx and dy from board.java in person.java though I do not know how to do that.
-
Re: Collision Detection between Sprites and Images?
The position and speed of an object should be contained in the object.
-
Re: Collision Detection between Sprites and Images?
Code :
package outrun;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.Rectangle;
import javax.swing.ImageIcon;
public class Person {
private String person = "/resources/person_outrun.png";
private int dx;
private int dy;
private int x;
private int y;
private int width;
private int height;
private Image image;
public Person() {
ImageIcon i = new ImageIcon(this.getClass().getResource(person));
image = i.getImage();
width = image.getWidth(null);
height = image.getHeight(null);
x = 31;
y = 175;
}
public void move() {
x += dx;
y += dy;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public Image getImage() {
return image;
}
public Rectangle getBounds() {
return new Rectangle(x, y, width, height);
}
public void KeyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_RIGHT) {
dx = 1;
}
if (key == KeyEvent.VK_LEFT) {
dx = -1;
}
if (key == KeyEvent.VK_UP) {
dy = -1;
}
if (key == KeyEvent.VK_DOWN) {
dy = 1;
}
}
public void KeyReleased(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_RIGHT) {
dx = 0;
}
if (key == KeyEvent.VK_LEFT) {
dx = 0;
}
if (key == KeyEvent.VK_UP) {
dy = 0;
}
if (key == KeyEvent.VK_DOWN) {
dy = 0;
}
}
}
This is Person.java. It contains the variables dx and dy that I need. However, I want to use the same dx and dy for board.class below:
Code :
package outrun;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.Rectangle;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Board extends JPanel implements ActionListener {
private Timer timer;
private Person person;
private Cobblestone cobblestone;
public Board() {
addKeyListener(new TAdapter());
setFocusable(true);
setBackground(Color.BLACK);
setDoubleBuffered(true);
setSize(10, 10);
person = new Person();
cobblestone = new Cobblestone();
timer = new Timer(5, this);
timer.start();
}
public void paint(Graphics g) {
super.paint(g);
Graphics2D m = (Graphics2D)g;
m.drawImage(person.getImage(), person.getX(), person.getY(), this);
drawMaze((Graphics2D) g);
}
private void drawMaze(Graphics2D g){
Graphics2D g2 = (Graphics2D)g;
int width = 27;
int height = 17;
int xOffset = 21;
int yOffset = 21;
int currentX = 10;
int currentY = 10;
for(int j = 0; j < height; j++) {
if(j!=8)
g2.drawImage(cobblestone.getImage(), currentX, currentY,this);
currentY += yOffset;
}
for(int k = 0; k < width; k++) {
if(k!=13)
g2.drawImage(cobblestone.getImage(), currentX, currentY,this);
currentX += xOffset;
}
currentY = 10;
currentX = 556;
for(int l = 0; l < height; l++) {
if(l!=8)
g2.drawImage(cobblestone.getImage(), currentX, currentY,this);
currentY += yOffset;
}
currentY = 10;
currentX = 10;
for(int m = 0; m < width; m++) {
if(m!=13)
g2.drawImage(cobblestone.getImage(), currentX, currentY,this);
currentX += xOffset;
}
g.setColor(Color.green);
g.drawString(" ENV 1 LVL 1", 480, 50);
}
public void checkCollisions() {
Rectangle r1 = person.getBounds();
Rectangle r2 = cobblestone.getBounds();
if (r1.intersects(r2)) {
}
}
public void actionPerformed(ActionEvent e) {
person.move();
repaint();
}
private class TAdapter extends KeyAdapter {
public void keyReleased(KeyEvent e) {
person.KeyReleased(e);
}
public void keyPressed(KeyEvent e) {
person.KeyPressed(e);
}
}
}
How do I do this?
-
Re: Collision Detection between Sprites and Images?
The Person class has the dx and dy variables. If the Board class has a reference to a Person object, it can call methods in the Person class to access those variables.
You have not explained why you need access to those variables?
-
Re: Collision Detection between Sprites and Images?
I need to access them as I want to set dx and dy to zero if both the person and cobblestone intersect each other. How do I make that reference? I have tried person.dx though that does not work.
-
Re: Collision Detection between Sprites and Images?
Add a method to Person that you can call to do what you want.
-
Re: Collision Detection between Sprites and Images?
I tried, but then I come up with many errors stating that the variable could not be resolved. Where in person.java shall I create the method?
-
Re: Collision Detection between Sprites and Images?
Quote:
come up with many errors
Please post the full text of any errors you want help with.
Quote:
Where in person.java shall I create the method?
You could put the method(s) next to any of the other methods.
-
Re: Collision Detection between Sprites and Images?
Code :
public Person(int dx, int dy) {
ImageIcon i = new ImageIcon(this.getClass().getResource(person));
image = i.getImage();
width = image.getWidth(null);
height = image.getHeight(null);
x = 31;
y = 175;
}
I created a new method here. The errors are just errors stating 'dx/dy cannot be resolved into a variable'.
-
Re: Collision Detection between Sprites and Images?
What you posted looks like a constructor not a method. A method has a return type in its definition.
Quote:
errors are just errors stating
Please post the full text of the error message. Your description makes no sense.
The posted code does not use the variables dx and dy. It defines new variables as parameters to the method.
-
Re: Collision Detection between Sprites and Images?
Oh apologies, I posted the wrong code, the errors were already resolved, the interface never refreshed to reflect that yet. Here is my code using the methods:
Code :
public int getdx() {
return dx;
}
public int getdy() {
return dy;
}
Now I need to use it for determining collision:
Code :
if (r1.intersects(r2)) {
}
person.getdx()..................
}
I want dx to equal 0, how do I express that? I will do the same for dy in due course.
-
Re: Collision Detection between Sprites and Images?
Quote:
I want dx to equal 0, how do I express that?
Somewhere that dx is in scope:
dx = 0;
-
Re: Collision Detection between Sprites and Images?
I know how to express 'dx = 0;', the problem is dx is not recognized as a variable in board.java. Do I do this? This is in board.class if that helps.
Code :
if (r1.intersects(r2)) {
}
person.getdx()= 0; //This is the problem.
}
-
Re: Collision Detection between Sprites and Images?
If dx is in another class, then you need a reference to that class and a method in that class to call that will do what you want.
aRefToClassWith_dx.methodToClear_dx(); // call a method to clear dx (set to 0)