detect character

Hello! I have question!
I have 2 character, and when i move first character, by using transition.to i have problem.

The problem is:

  1. First char goes through the second, using physics kinematic
  2. If I use dynamic physics, first char knocks down the second char.

The first char must stop before entering into the second char zone. How to realize it properly?

Best regards, Ivan. [import]uid: 211767 topic_id: 35432 reply_id: 335432[/import]

If you are moving your characters with transition.to() then you don’t need them to be kinematic or dynamic.

If you set the character’s physics bodies to static then they will fire the collision event so you can stop the movement when they collide, but the normal transfer of forces that happens with dynamic bodies will not occur. This method will get you into trouble with the standard transition.to() since it can’t be paused. There are pausable transition classes in the code exchange, you should look there. If you are going to cancel the transition then it isn’t an issue.

If you are relying on the physics and can’t use static bodies then you can still use the collision/precollision events along with:
[lua]character:setLinearVelocity( 0,0 )[/lua]

Check here for more information on the collision events:
http://developer.coronalabs.com/content/game-edition-collision-detection [import]uid: 181948 topic_id: 35432 reply_id: 140777[/import]

Hello!
Thanks for answer!

I used preCollison or postCollision for transition.cancel in dynamic character, but if I use transtion.cancel , i can`t use transition.to anymore. [import]uid: 211767 topic_id: 35432 reply_id: 140781[/import]

You’re welcome!

Can you post the code with assigning and canceling the transitions? You should be able to reuse the transitions so there’s probably some error (or omission) in that code.

-Treb [import]uid: 181948 topic_id: 35432 reply_id: 140784[/import]

i using this code (maybe have differents, because my project in working computer):

[lua]local char – here newImageSheet or etc…
local trans

local function moveChar( event )

local function listener (event)
Runtime:addEventListener(“touch”, moveChar)
end
x = event.x
y = event.y
trans = transition.to( char, { time=1500,x = x, y = y, onComplete = listener1 } ) – maybe differents, but – logic saved

Runtime:removeEventListener(“touch”, moveChar)
end

Runtime:addEventListener(“touch”, moveChar)

function char:postCollision( event )
print( “detect collision”)
transition.cancel(trans)
end
char:addEventListener( “postCollision” )[/lua]
And in console (debug) i have 5-10 logs “Detection collision” and not working moveChar.

Lol, i writed this “test code” and found problem, because not using “listener1”, but problem this:

first char in dynamic physics very small knocks down the second char, small 1-2px, but knocks down, what wrong?

I tryed using postCollision and preCollision, but knocks down have.

Sorry for my bad english! [import]uid: 211767 topic_id: 35432 reply_id: 140805[/import]

Hmm it might be that the physics is still being done so there’s a small delay in canceling the transition, which accounts for the 1-2 pixel movement.

One option is storing the X+Y values of the character in the precollision event and resetting the character to that position after cancelling the transition.

Let me know if that works! And you’re English isn’t bad, I get what you’re saying :slight_smile:

-Treb [import]uid: 181948 topic_id: 35432 reply_id: 140819[/import]

“One option is storing the X+Y values of the character in the precollision event and resetting the character to that position after cancelling the transition.”

You mean:
using preCollision, if him “detect” get coordinate char and setup him?

example code:
[lua]function char:preCollision( event )
print( “detect precollision”)
x = char.x
y = char.y

transition.cancel(trans)

char.x = x
char.y = y
end
char:addEventListener( “preCollision” )[/lua]

you mean this? [import]uid: 211767 topic_id: 35432 reply_id: 140822[/import]

Yeah, no guarantees it will work but it’s something to try.

If that doesn’t work (because the transition still hasn’t been cancelled when you reset) then you could add a delay to resetting the XY. For example:

[lua]function char:preCollision( event )
print( “detect precollision”)
x = char.x
y = char.y

transition.cancel(trans)

timer.performWithDelay( 50, function() char.x=x; char.y=y end )
end
char:addEventListener( “preCollision” )[/lua]
[import]uid: 181948 topic_id: 35432 reply_id: 140832[/import]

Hello again!

I use this code:

[lua]

function charSelected:preCollision( event )
if(int == 0) then
print( “detect precollision”)
x = charSelected.x
y = charSelected.y

transition.cancel(trans1)

timer.performWithDelay( 50, function() charSelected.x=x; charSelected.y=y end )
Runtime:addEventListener(“touch”, moveCharacter)
int = int + 1
end
end
charSelected:addEventListener( “preCollision”) [/lua]
int i use for delete duplicate detect.

Here i upload screen, please look!
http://rghost.ru/private/43414550/8f4dbd7207b641a3b7bf1410adde1415

how you can see, detect work, cancel successful, but knocks him…
Wait answer, thanks! [import]uid: 211767 topic_id: 35432 reply_id: 140879[/import]

or how i Can disable “effect rotate by knocks down”? [import]uid: 211767 topic_id: 35432 reply_id: 140892[/import]

i read documentation and found nice function, its: char.isFixedRotation = true
and work super :).
[import]uid: 211767 topic_id: 35432 reply_id: 140895[/import]

Great, glad it’s working! [import]uid: 181948 topic_id: 35432 reply_id: 140920[/import]

If you are moving your characters with transition.to() then you don’t need them to be kinematic or dynamic.

If you set the character’s physics bodies to static then they will fire the collision event so you can stop the movement when they collide, but the normal transfer of forces that happens with dynamic bodies will not occur. This method will get you into trouble with the standard transition.to() since it can’t be paused. There are pausable transition classes in the code exchange, you should look there. If you are going to cancel the transition then it isn’t an issue.

If you are relying on the physics and can’t use static bodies then you can still use the collision/precollision events along with:
[lua]character:setLinearVelocity( 0,0 )[/lua]

Check here for more information on the collision events:
http://developer.coronalabs.com/content/game-edition-collision-detection [import]uid: 181948 topic_id: 35432 reply_id: 140777[/import]

Hello!
Thanks for answer!

I used preCollison or postCollision for transition.cancel in dynamic character, but if I use transtion.cancel , i can`t use transition.to anymore. [import]uid: 211767 topic_id: 35432 reply_id: 140781[/import]

You’re welcome!

Can you post the code with assigning and canceling the transitions? You should be able to reuse the transitions so there’s probably some error (or omission) in that code.

-Treb [import]uid: 181948 topic_id: 35432 reply_id: 140784[/import]

i using this code (maybe have differents, because my project in working computer):

[lua]local char – here newImageSheet or etc…
local trans

local function moveChar( event )

local function listener (event)
Runtime:addEventListener(“touch”, moveChar)
end
x = event.x
y = event.y
trans = transition.to( char, { time=1500,x = x, y = y, onComplete = listener1 } ) – maybe differents, but – logic saved

Runtime:removeEventListener(“touch”, moveChar)
end

Runtime:addEventListener(“touch”, moveChar)

function char:postCollision( event )
print( “detect collision”)
transition.cancel(trans)
end
char:addEventListener( “postCollision” )[/lua]
And in console (debug) i have 5-10 logs “Detection collision” and not working moveChar.

Lol, i writed this “test code” and found problem, because not using “listener1”, but problem this:

first char in dynamic physics very small knocks down the second char, small 1-2px, but knocks down, what wrong?

I tryed using postCollision and preCollision, but knocks down have.

Sorry for my bad english! [import]uid: 211767 topic_id: 35432 reply_id: 140805[/import]

Hmm it might be that the physics is still being done so there’s a small delay in canceling the transition, which accounts for the 1-2 pixel movement.

One option is storing the X+Y values of the character in the precollision event and resetting the character to that position after cancelling the transition.

Let me know if that works! And you’re English isn’t bad, I get what you’re saying :slight_smile:

-Treb [import]uid: 181948 topic_id: 35432 reply_id: 140819[/import]

“One option is storing the X+Y values of the character in the precollision event and resetting the character to that position after cancelling the transition.”

You mean:
using preCollision, if him “detect” get coordinate char and setup him?

example code:
[lua]function char:preCollision( event )
print( “detect precollision”)
x = char.x
y = char.y

transition.cancel(trans)

char.x = x
char.y = y
end
char:addEventListener( “preCollision” )[/lua]

you mean this? [import]uid: 211767 topic_id: 35432 reply_id: 140822[/import]

Yeah, no guarantees it will work but it’s something to try.

If that doesn’t work (because the transition still hasn’t been cancelled when you reset) then you could add a delay to resetting the XY. For example:

[lua]function char:preCollision( event )
print( “detect precollision”)
x = char.x
y = char.y

transition.cancel(trans)

timer.performWithDelay( 50, function() char.x=x; char.y=y end )
end
char:addEventListener( “preCollision” )[/lua]
[import]uid: 181948 topic_id: 35432 reply_id: 140832[/import]

Hello again!

I use this code:

[lua]

function charSelected:preCollision( event )
if(int == 0) then
print( “detect precollision”)
x = charSelected.x
y = charSelected.y

transition.cancel(trans1)

timer.performWithDelay( 50, function() charSelected.x=x; charSelected.y=y end )
Runtime:addEventListener(“touch”, moveCharacter)
int = int + 1
end
end
charSelected:addEventListener( “preCollision”) [/lua]
int i use for delete duplicate detect.

Here i upload screen, please look!
http://rghost.ru/private/43414550/8f4dbd7207b641a3b7bf1410adde1415

how you can see, detect work, cancel successful, but knocks him…
Wait answer, thanks! [import]uid: 211767 topic_id: 35432 reply_id: 140879[/import]