Pergunta

I want to create a 2D game with a tile based map. My main problem is collision. If there is a tree in my way, how will I make my sprite not move through the tree?

Foi útil?

Solução

Pseudo-code:

some_event() {
    if (bullet.x == monster.x && bullet.y == monster.y) {
        collision_occurs();
    }
}

Of course the semantics such as which event will be fired and whether or not an event handler makes more sense (i.e.: collision_occurs() when the x and y coordinates meet, rather than if they meet while some_event() is fired) depend on the game.

If you were to analyze this more you would notice that the bullet and monster aren't 1 pixel each, so it would look more like:

// While approaching from the left
if ((bullet.x + (bullet.radius)) >= (monster.x + (monster.radius)))

These fine details come after. Essentially you have moving objects and these objects share coordinates. When these coordinates, as properties of their representational objects, are near enough, a "collision" occurs and some methodology follows.

Outras dicas

The general idea behind things like this (and I use the term general loosely) is that each 'object' in your game (whether it be a tree, car, w/e) is a group of polygons. Now, each of these respective polygons have a set of borders to them, which have a (likely) set or known size. So really, you could look at each of the object in your game as a set of hollow polygons. for example, take this 'hollow' car and wall I just whipped up (I'm an awful artist, so bear with me):

enter image description here

Now, even though in reality your car and wall will have nice details, and color filling, the only things you care about as a programmer handling collisions, is the border.

In the program, you'll have a handler attached to each of the objects in the game, something that listens for changes in the program. (A car moving, for example), and it can detect things like, did a collision happen? did the game end? did the car move past me and I should no longer be displayed. Etc. etc. since you care about collisions, you would pay extra special attention to when either: A) The car listener says it collided with something or B) the wall listener says something hit it. From there you could change the logic of the program to handle whatever you want happening (stopping the cars animation motion and adding damage, showing a game-over screen, whatever).

From there, really, the best way to go about it is just to try. Java has a ton of graphical frameworks, and each one has it's own nuances. Try them all! or pick one after some research, the world is your playground.

EDIT

One example of a collision of shapes is in Javafx, and it is handled like so: (credit to jewlesea for this piece of code.)

private void checkBounds(Shape block) {
  boolean collisionDetected = false;
  for (Shape static_bloc : nodes) {
    if (static_bloc != block) {
      static_bloc.setFill(Color.GREEN);

      if (block.getBoundsInParent().intersects(static_bloc.getBoundsInParent())) {
        collisionDetected = true;
      }
    }
  }

  if (collisionDetected) {
    block.setFill(Color.BLUE);
  } else {
    block.setFill(Color.GREEN);
  }
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top