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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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

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.

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?

views:2172 update:2012/2/13 9:11:28
corona forums © 2003-2011