Block type detection with x,y position

63 views
Skip to first unread message

Aly

unread,
May 15, 2013, 9:32:04 AM5/15/13
to mel...@googlegroups.com
Hello,

I am trying to detect the type of block present at a certain x and y position but am still unsure how, even after consulting other topics here and the documentation.

My player falls into "water" which kills him and I want him to reappear on the closest solid block. My initial thought was to span the surrounding blocks (I know my tile size so I only check each block once) and find a solid block then move the player there.

Any ideas would be greatly appreciated.

Thanks!

Aaron McLeod

unread,
May 15, 2013, 9:41:53 AM5/15/13
to mel...@googlegroups.com
I suck at doing this kind of thing efficiently, so I'd go for a back and forth check based on position. Let's say tile size is 32x32, and you know what tile he fell into, say: 640x608. So from there, you need to check the tile behind him, and see if there's a solid position there or not. If there isn't, check in front. Then go two tiles behind, two tiles in front, etc. 

When you find one, you then need to check above the tile, until you find an open space.


--
You received this message because you are subscribed to the Google Groups "melonJS - A lightweight HTML5 game engine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to melonjs+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Aaron McLeod
http://agmprojects.com

Aly

unread,
May 15, 2013, 9:45:02 AM5/15/13
to mel...@googlegroups.com
Thanks for the reply Aaron, but my concern is that I don't know how to check if "there's a solid position there". I can figure out the algorithm for placing him later :)

Aaron McLeod

unread,
May 15, 2013, 10:08:58 AM5/15/13
to mel...@googlegroups.com
Did some digging through the source and docs. Can get the collision layer data with this guy: 

me.game.currentLevel.getLayerByName("collision").layerData

Jay Oster

unread,
May 15, 2013, 2:40:26 PM5/15/13
to mel...@googlegroups.com
I would do something less algorithmic, here. Store a reference to the last known "solid" tile the player object was standing on. melonJS collision detection currently only checks the tile types at the corners of the object, so you would just do the same: After you call this.updateMovement(), check the tile directly one pixel below the lower left corner. If that fails to be solid, check the tile one pixel below the lower right corner. Store the reference for the first solid tile found, or do not update if the tile is not solid (eg, the player is jumping or has fallen off a ledge).

When you're ready to reset the position, you already know which tile the player was last standing on... The SNES and GB Zelda series does this when Link jumps into water without the flipper item.

Aaron McLeod

unread,
May 15, 2013, 2:41:20 PM5/15/13
to mel...@googlegroups.com
That's rather brilliant and simplistic. <3 

Aly

unread,
May 16, 2013, 8:53:37 AM5/16/13
to mel...@googlegroups.com
I tried this and it works like a charm! Thank you Jay and Aaron! 
Reply all
Reply to author
Forward
0 new messages