Forward Collision Check

*Note: I posted this under the main corona forums with no luck. Hopefully since I’m doing this in Lime, someone here can help. The solution I’m looking for is to be able to look ahead one tile before I start my transition.to(). Maybe someone with grid based top down experience could help.

Repost:
I’ve tried to figure this one out myself and no luck, so I’ll give it a shot here.

In it’s most simple form I have a tile based game where the character’s movement is tied to the grid(example: he can only move 16 pixels in one direction and can’t move again till he has moved that distance). I have walls. All objects are 16x16. Since the walls are 16x16 and so is the character, if they are adjacent they will share a border which will cause collision.

To remedy the situation, I tried changing the walls to isSensor in order to allow that shared border. I didn’t want to resize artwork because that collision is necessary. If that collision happens, I disable the character from moving in that direction. It works great when moving one tile at a time.

The Problem: If the user holds down a button on the D-Pad, the characters movement will be fluid. It’s down by adding a indicator on buttonDown/buttonUp. If my transitionTo completes and buttonHeld is true, it will start another transitionTo in that direction. This causes a problem. Since the wall is a sensor I lose preCollision. My events fire in this order:
transition.to()
completeTransitionCall()
onCollision()

In that onCollision call I say to disable movement, but since it’s firing after the completion, I cant’ set the flag that says don’t move in time. Here is a code example for moving left.

[code]local onButtonLeftEvent = function(event)
if event.phase == “press” then

if(player.isMoving == false and openDirection.openLeft) then
player.direction = DIRECTION_LEFT
player.state = STATE_WALK_LEFT
trans = transition.to(player, {time=PLAYER_SPEED, x=player.x-16, y=player.y, onStart=StartTransition, onComplete=CompleteLeft})
buttonHeld = true
player.isMoving = true
end
else
buttonHeld = false
end
player:prepare(“anim” … player.state)
player:play()
end

CompleteLeft = function(obj)
print(“CompleteLeft”)
if(buttonHeld and openDirection.openLeft) then
trans = transition.to(player, {time=PLAYER_SPEED, x=player.x-16, onStart=StartTransition, y=player.y, onComplete=CompleteLeft})
player.isMoving =true
else
player.isMoving = false
player.state = STATE_IDLE_LEFT
player:prepare(“anim” … player.state)
player:play()
end
end

local function onCollision ( event )
local object1 = event.object1
local object2 = event.object2

if(math.round(object2.x-object1.x) == 24 and player.state ~= STATE_WALK_RIGHT) then
openDirection.openLeft = false
elseif(math.round(object1.x - object2.x) == 24 and player.state ~= STATE_WALK_LEFT) then
openDirection.openRight = false
elseif(math.round(object2.y-object1.y) == 24 and player.state ~= STATE_WALK_DOWN) then
openDirection.openUp = false
elseif(math.round(object1.y - object2.y) == 24 and player.state ~= STATE_WALK_UP) then
openDirection.openDown = false
end
end
[/code]

I’m converting this game from Flash(didn’t like using AIR at all). Over there I could pass in some location parameters and see if my object would collide with anything if my object was in that location. Is it possible to detect if something will collide with an object in it’s transition? Is there any other ways to do this. I’ve been banging my head on this for a week. I set the player to isBullet as well to force the update on the collision. The collision is always hitting at the exact right time. Sorry for the wall of text! [import]uid: 114566 topic_id: 21469 reply_id: 321469[/import]

If I understand you want to be able to check for obstacles in a grid position before starting a transition? If so, the ParallaxAndPlayer sample project does this, you would need to change it slightly for Orthographic but it would be pretty easy to do. [import]uid: 5833 topic_id: 21469 reply_id: 85075[/import]

Thanks for the response. It was actually in the IsometricandPlayer sample project. I went through all of them, very helpful. Thanks for including them. They got me what I needed.

I do have a question though. I’m moving a tile(with either tile:move and transition.to() and I’m having issues with the getTileAt(). Say I have a box tile, and I move that box over a tile. I then try to getTileAt() at it’s new location, it comes back empty. However, if I try to get a tile at it’s original location, it finds it.

I was looking through your code and I don’t see where it updates the tile layer when you move a tile. I was considering a few things like creating a new tile at the new location of the box and then swapping the two tiles, but that seems wrong. Is this intended or is there a way to update the TileLayer?

[import]uid: 114566 topic_id: 21469 reply_id: 86174[/import]

Whoops, I actually meant to type IsometricAndPlayer, no idea why I put ParallaxAndPlayer. My bad.

The updating of tiles and their respective positions is something that I am intending to redo, moving tiles was never really considered when I first wrote that stuff so now it causes issues. It is the next big thing I want to work on as it causes quite a few annoyances.

For now you probably want to look into Tile:updateGridPosition() and the second paramater to the TileLayer:getTileAt() function. [import]uid: 5833 topic_id: 21469 reply_id: 86602[/import]

I can’t get getTileAt(position, true) to work for the life of me. I noticed when I put a print statement inside of getTileAt, full is always nil. I tried hardcoding it for the time being and it didn’t like it. It also always tells me that the size of my tileList is 0. I’m not sure if I’m doing something wrong here or if it’s just a limitation. I can show you some of my code. I’m starting to worry my game won’t be possible. [import]uid: 114566 topic_id: 21469 reply_id: 86750[/import]

Are you able to send me a simple reproducible example or the whole project to my email? graham@grahamranson.co.uk [import]uid: 5833 topic_id: 21469 reply_id: 88061[/import]

I sent you over the complete project. Thanks again for the help! [import]uid: 114566 topic_id: 21469 reply_id: 88137[/import]

Received, will check it out as soon as I can. [import]uid: 5833 topic_id: 21469 reply_id: 88333[/import]