1

I am making a small game, 2D, and I have a player.

EDIT:

This is what I have right now:

        int oldX = player.x;
    int oldY = player.y;

    int newX = oldX - player.getSpeedX();
    int newY = oldY - player.getSpeedY();

    if(player.getBounds().intersects(brush1.getBounds())){
        player.x = newX;
        player.y = newY;
    }else{
        player.x = oldX;
        player.y = oldY;
    }

But, it is acting really weird, it changes speed when I go in from one side, etc.

Stan
  • 3,659
  • 14
  • 35
  • 42

2 Answers2

1

For a formula and code that checks the intersection of a line segment and a circle, have a look at this SO answer.

The rest of the code should be quite clear, before you make a move, check if a collision occurs, if it would, don't move.

Depending on the behaviour you prefer, you could also move as close to the wall as possible and then move parallel to it to let the circle "slide" along the wall. This can be done by projecting the movement vector on a line with the same direction as the wall.

EDIT: Some comments on how to use the code from the answer to check for collisions:

The code uses a Dot function that computes the dot product of two vectors. You can either create a Vector class (a good exercise and it is useful for such projects) or compute just the dot product directly using the formulas here.

A vector class will make some of the code easier to read, but you can also operate on floats directly. For example, let's have a look at the computation of d (Direction vector of ray) and f (Vector from center sphere to ray start) as described at the start of the answer. With a vector class, this will be as easy as the formulas used there:

// L, E, C, d and f are of some vector type
d = L - E;
f = E - C;

Without a vector class, you'll have seperate x/y coordinates for all of the variables, bloating the code a bit but doing just the same:

// Twice as much variables, but all of them are usual floats.
dx = Lx - Ex;
dy = Ly - Ey;
fx = Ex - Cx;
fy = Ey - Cy;
Community
  • 1
  • 1
schnaader
  • 49,103
  • 10
  • 104
  • 136
  • Well, i'm not a good coder yet, and I really don't understand any of that. Is it really that hard, or? – Stan Apr 23 '11 at 12:38
  • The hardest thing about it is the math involved. In this case, I'd suggest you should copy/paste the code from the answer and extend it so it works for you. I'll edit my answer and leave some additional comments on this. – schnaader Apr 23 '11 at 12:46
  • Okay, so, would it be easier if I edited the oval to a rectangle, or would that be the same? – Stan Apr 23 '11 at 12:48
  • A rectangle may be a bit easier if two sides are parallel to the wall. A rectangle arbitrarily oriented to the wall will be much harder to process than a circle. – Goblin Alchemist Apr 23 '11 at 13:35
  • I found that the minimum-translation-distance model of collision handling served me pretty well in an axis-aligned system with simple, cuboid shapes. Worth a Google, in any case. – Owen Apr 25 '11 at 06:39
0

I think your code snippet has a few bugs. Suggested fixes:

int oldX = player.x;
int oldY = player.y;

// *add* speed to old pos to get new pos
int newX = oldX + player.getSpeedX();
int newY = oldY + player.getSpeedY();

if(player.getBounds().intersects(brush1.getBounds())){
    // Collision!  Keep old position.
    // Reverse speed (just a suggestion).
    player.setSpeedX(-player.getSpeedX());
    player.setSpeedY(-player.getSpeedY());
}else{
    // No collision.  Set position to *new* pos, not old pos.
    player.x = newX;
    player.y = newY;
}
awhyte
  • 350
  • 2
  • 4