for(int i = 0; i < Entities.length; i++)
{
boolean collided = false;
for(int j = 0; j < Entities.length; j++)
{
if(i != j && Entities[i].getBounds().intersects(Entities[j].getBounds()))
{
collided = Entities[i].collision = Entities[j].collision = true;
}
}
if(!collided)
{
Entities[i].collision = false;
}
}
// (on collisions players (entities) position resets to the spot before the collision)
int i;
for(i = 0;i < Entities.length; i++)
{
b.setColor(colors[i]);
Entity temp = Entities[i];
if(temp.collision == true)
{
temp.x = temp.nextX;
temp.y = temp.nextY;
temp.speed = temp.nextSpeed;
}
else
{
temp.nextX = temp.x;
temp.nextY = temp.y;
temp.nextSpeed = temp.speed;
}
b.fillRect(temp.getX(), temp.getY(), temp.getWidth(), temp.getHeight());
}
Collision Detection
Page 1 of 17 Replies - 4799 Views - Last Post: 18 October 2010 - 04:33 AM
#1
Collision Detection
Posted 06 October 2010 - 08:13 AM
I'm working on a project that is to eventually turn into a game where players move through mazes. I currently have the players drawn as rectangles and they do stop when running into other; however, when objects collide they can no longer move. This is because the collision detection just resets the position on where the players are. Is there a way to do the collision detection where movement doesn't stop but, the objects don't run through each other? Here is the code I'm working with so far:
Replies To: Collision Detection
#2
Re: Collision Detection
Posted 06 October 2010 - 01:09 PM
Something that you can do is reverse the direction traveled and set the object back one or two pixels in the new direction. That will not just stop it from moving, but it will move it out of the area where it is colliding, so it won't sit in constant collision.
#3
Re: Collision Detection
Posted 06 October 2010 - 01:51 PM
I could change it to that but, my goal is to just halt movement in that specific direction while maintaining the ability to move in other directions; the movement of an object going forward is also halted by an object colliding on its side or behind it. Also, when the objects collide sometimes they still intersect a little. My assumption is that this happens because the objects move a couple pixels at a time instead of one? How can I make the collisions stop exactly where the objects meet?
#4
Re: Collision Detection
Posted 06 October 2010 - 04:22 PM
If you stop them exactly where they meet, it seems the colliding objects would be stuck in a collision state. The options to exit the collision state are to separate them by at least one pixel or modify the collision detection to 'forgive' a pixel (or more) of overlap until the separation increases. The separation options seems more natural as it would appear to the user as a slight bounce back after collision, if it is perceived by the user it all. Modifying the collision detection for objects in a collision state would not be that hard, just a little more work and a new state to keep track of.
This post has been edited by GregBrannon: 06 October 2010 - 04:23 PM
#5
Re: Collision Detection
Posted 06 October 2010 - 07:38 PM
Yah, like he said and what I said earlier. The collision detection method that you are using only applies when pixel overlap, not if they touch.Thus, they remain in a constant state of collision unless you move the images apart from each other...just slightly. Then, your object will no longer be colliding,and it will be free to move around as usual again.
#6
Re: Collision Detection
Posted 08 October 2010 - 04:33 AM
Alright, well thanks for the idea then. Is there any other methods of collision detection I could use that might work better or is this way the most ideal?
#7
Re: Collision Detection
Posted 18 October 2010 - 01:07 AM
Sorry for a much later reply with some results, I've been busy but, this is what I have now.
I implemented collision detection in this way:
This works for getting stuck against walls except, when pressing two directions at once you freeze. Also, I found that it's very possible to trap another player in their current position. Any different ways I could set this up to enable movement in the directions not collided and to prevent players from trapping each other?
I implemented collision detection in this way:
if(temp.collision == true)
{
if(Entities[i].up)
{
temp.x = (temp.nextX);
temp.y = (temp.nextY + 2);
temp.speed = temp.nextSpeed;
}
else if(Entities[i].down)
{
temp.x = (temp.nextX);
temp.y = (temp.nextY - 2);
temp.speed = temp.nextSpeed;
}
else if(Entities[i].left)
{
temp.x = (temp.nextX + 2);
temp.y = (temp.nextY);
temp.speed = temp.nextSpeed;
}
else if(Entities[i].right)
{
temp.x = (temp.nextX - 2);
temp.y = (temp.nextY);
temp.speed = temp.nextSpeed;
}
else
{
temp.x = (temp.nextX);
temp.y = (temp.nextY);
temp.speed = temp.nextSpeed;
}
}
else
{
temp.nextX = temp.x;
temp.nextY = temp.y;
temp.nextSpeed = temp.speed;
}
b.fillRect(temp.getX(), temp.getY(), temp.getWidth(), temp.getHeight());
}
b.dispose();
}
This works for getting stuck against walls except, when pressing two directions at once you freeze. Also, I found that it's very possible to trap another player in their current position. Any different ways I could set this up to enable movement in the directions not collided and to prevent players from trapping each other?
#8
Re: Collision Detection
Posted 18 October 2010 - 04:33 AM
My usual approach to collision detection is:
1. Save the location of my sprite.
2. Move the sprite. This may be temporary.
3. Check for collisions.
4. For each collision encountered, work out the side effects. This may be game over, scoring points or, in your case, moving the sprite back to its original position that was saved above. As far as the user is concerned, it won't have moved at all since there is no redisplay between steps 2 and 4.
5. Move onto the next sprite. The strategy of checking for collisions after each sprite has moved will prevent them getting stuck together.
1. Save the location of my sprite.
2. Move the sprite. This may be temporary.
3. Check for collisions.
4. For each collision encountered, work out the side effects. This may be game over, scoring points or, in your case, moving the sprite back to its original position that was saved above. As far as the user is concerned, it won't have moved at all since there is no redisplay between steps 2 and 4.
5. Move onto the next sprite. The strategy of checking for collisions after each sprite has moved will prevent them getting stuck together.
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote







|