I have been using rectangles for collision since i have started making “games” in java, but they are not working i have trued everything to make it work and it doesn’t! So is there any other way of collision in java? I don’t mean collision detection i mean like player cannot pass throu walls and stuff like that! I have been getting the collision and making a rectangle out of it and i take it width and subtract it of the player x, repeat for y! I get player shaking out of the rectangle. Need help!
You will have to use the rectangles as sensors. If they overlap, then you process that collision, like moving the player back to its last spot to keep it from going through walls.
If you don’t want to use rectangles, then google AABB (Axis-Aligned Bounding Box) collision detection. This will give you the same result as the rectangles, but it is a different implementation for you to try out.
Edit: The rectangles will not magically keep from going through each other if that’s what you want. You just have to use them to detect the collision, and then you will process it.
But that is what i have been doing and it’s not working out. Maybe my code is wrong? Can you give me an a example?
What do you mean by “not working out”? Have you tried to debug your game? Do you have at least a slight idea that what is wrong with your code?
Basically all you have to do is make a bounding box for your sprites and when you’re moving them iterate over all the other bounding boxes. If it collides with one of them prevent the movement (or do something like bouncing off). This should be trivial to implement, but here you go:
public void collisionDetection(){
for(Sprite sprite1 : sprites){
// Move sprite1 to it's new position (and it's bounding box too)
// but before you do that you might want to save it's current position
// to restore the player there if a collision happens.
// ...
Rectangle bb1 = sprite1.getBoundingBox();
boolean collided = false;
for(Sprite sprite2 : sprites){
Rectangle bb2 = sprite2.getBoundingBox();
if(sprite1 != sprite2 && bb1.intersects(bb2)){
// Collision happened
// Handle collision (prevent movement, bounce off, etc.)
// ...
collided = true;
break;
}
}
if(!collided){
// The sprite didn't collide with anything else
// so handle movement (increase position, etc.)
}
}
}
I wrote this off the top of my head so hopefully there are no errors in the code. This uses Java’s AWT Rectangle class for checking rectangle intersections. Implementing your own intersection “algorithm” shouldn’t be hard either.
Edit: I forgot to mention that these kind of collision detections will never be perfect because if your sprite’s acceleration goes high it can easily clip through slim walls and such. To prevent this you’ll have to look into raycasting which is considered as a bit of an advanced topic (but it isn’t terribly complex IMO). :point:
// Example code
Rectangle player;
Rectangle wall;
public void update()
{
// These will store the player's last position before it moves.
lastPlayerX = playerX;
lastPlayerY = playerY;
// However you move your player
movePlayer();
// Set the rectangles to enclose the player and the wall
player.set(playerX, playerY, playerWidth, playerHeight);
wall.set(wallX, wallY, wallWidth, wallHeight);
checkCollision();
}
public void checkCollision()
{
// If the player rectangle is intersecting the wall rectangle
if(player.intersects(wall))
handleCollision();
}
// handleCollision could be like setting the player's coordinates to the last coordinates to keep it from going through the wall
private void handleCollision()
{
playerX = lastPlayerX;
playerY = lastPlayerY;
}
Thanks! But i get stuck in place when i am doing this!
Collision systems are all about how people react to them. In other words, the moment the collision happens, you need to react in the right way in order to create a realistic feel. I’ve done collision for both tile based and non-tile based games. For tile based, it is very easy, you just block the tile and the movement stops in its tracks. For non-tile based games, the best way to halt movement is to base collision on direction.
Mario Game Example - Cut & Paste IDE
In other words, if the player is trying to move right, you want to not only stop the player but move them left far enough so the don’t collide with the object. Vice-versa for right. Having player states (like walking, jumping, falling, etc.) can help you, the designer, plan out the movements better.
Remember, it isn’t the colliding boxes that is causing the problem, it is how you tell your program to react to them. The example I put up is a fully working collision example that covers all the basic cases. Collision is only hard if you don’t take the time to solve every case and cover all your edge cases.
The easiest way that someone helped me with this problem is to simply move the bounding rectangle before the player is moved. If the player bounds overlaps something; say a wall in this case, the player can’t move and we use a boolean to stop the movement. Below is a snippet of my movement code for my game, it works great for what I need, though it might not be ideal for every case.
if(Gdx.input.isKeyPressed(Keys.D) && canMoveEast){ // This is testing if the player can move east
player.bounds.set(player.getX()+4, player.getY(), 32, 32); // Set the bounds of the collision rectangle 4 ahead, stops player getting stuck in the wall
for(int i =0; i < CollisionArray.size(); i++){ // Loop the array to check for collisions
if(player.bounds.overlaps(CollisonArray.get(i))){ // If the player does collide with something
canMoveEast = false; //Set the movement of east to false and the player can't move
}
}
if(canMoveEast){ // If the loop runs all the way through and finds no collisions, the player can then move.
player.setX(player.getX() + 4); // Moves the player
}
resetMovement(); // canMoveEast = true; etc. Don't forget to reset the movement at the end :)!
}
I hope this helps in someway