Not logged inGosu Forums
Forum back to libgosu.org Help Search Register Login
Up Topic Gosu / Gosu Exchange / Calculating collision with isometric tiles
- - By Naoya Date 2014-11-10 12:35
So, I've been quite lost in this matter for a few days and hoped someone can clarify this thing for me as my math sucks :x

Here is a screenshot of a game I'm making to learn about gosu:



The numbers on the tiles are X / Y of their coordinates in an array[y][x] that describes each tile passability (I did not draw the map by tiles, I just loaded an image).

The blue boxes around the player are his collision boxes (for the sake of this post just consider the light blue one).

The pink tiles are the ones I'm testing the collisions, you can disregard them.

After hours and hours of suffering I could make the code that detects if the player (or any entity) had collided with an specific tile. Setting aside the fact that my way is certainly not the most effective way of doing it, I have two problems:

1. I need to know the axis in which the player collided with the tile (so I can block his movement just on this axis).
2. I need to know in which tile (or which tiles) the player is in so I can calculate the collision with adjacent tiles in the event of his movement.

Also I'm using a rectangular collision box for the player which I'm not certain if is the most efficient way of doing it.

If anyone have an idea on how to do it, please share with me :)
Parent - - By RunnerPack Date 2014-11-10 19:06
You're making a lot of unnecessary work for yourself trying to do collision testing in "screen-space". Just keep the player's position in the same, rectangular coordinate system you're using for the map (the "0/0", etc.), test for collisions with simple array operations, then project the player's position to screen-space for drawing, afterward.
Parent - - By Naoya Date 2014-11-10 19:08
I'm not sure I understood, that would mean that a player is always occupying one square (and when it moves it moves one square at a time)?
Parent - By RunnerPack Date 2014-11-11 00:25
Well, obviously there will have to be some kind of coordinate scaling involved, or you could use floats to hold the sprite positions, and just truncate (floor) to get the integer tile coordinate (and, when projecting to the screen, multiply by how many pixels "wide" a tile is; i.e. from one side to its parallel side, not from corner to corner).
If you want to (or have to) work with sprite positions in pixels, you could reverse-project the pixel position (i.e. the center of the bottom edge of the light-blue rectangle) back to the map coordinates for testing (which will also require some scaling/truncation).
Parent - By jlnr (dev) Date 2014-11-11 05:49
I agree with RunnerPack. This will become very messy if you want to check for collisions between straight and "diamond" boxes.

To block movement, I would not move the player and then check for collisions afterwards. Instead, if the player is moving left at 4px per frame, you could basically do:

4.times do
  self.x -= 1
  if self.blocked?
    self.x += 1
    # ...hit something to the left...
    break
  end
end


...and this for all four directions. Not super efficient, but at least easy to understand :) And the easiest way to implement this would be to make the player a single point. Whether that works depends on the game, I guess:

def blocked?
  game.map.solid_at? self.x, self.y
end
Parent - - By Naoya Date 2014-11-11 22:33
Hmm I got it. But one of the things I'm having difficulty is how to come up with the math to convert pixel coordinates to my isometric tile coordinates (knowing which pixel belong to which tile).

With square tiles is easy you only have to divide the x for the tile width and the y for the tile height, but the isometric grid is kinda different on the format and organization of the tiles :/

Sorry to bother with such a newbie question, by the way D: It's more of a problem with algebra than gosu itself
Parent - By RavensKrag Date 2014-12-04 08:11
Having problems with isometric coordinates is a pretty common thing, and I'm rather frustrated that there's not one super solid tutorial that I can just refer people to in times like this.

However, there are a handful of different articles linked here
http://www-cs-students.stanford.edu/~amitp/gameprog.html
and maybe reading one or more of those will help you to understand.

I personally thought that this one in particular was pretty good:
http://gamedevelopment.tutsplus.com/tutorials/creating-isometric-worlds-a-primer-for-game-developers--gamedev-6511
Up Topic Gosu / Gosu Exchange / Calculating collision with isometric tiles

Powered by mwForum 2.29.7 © 1999-2015 Markus Wichitill