problem with transition.to and physics

Here is my code, and my problem is 

When my bullet spawn, it trigger the movebullet function.

All bullets transition.to the mainCircle. However, I have my mainRect, with physics, keep rotating and tried to avoid the bullet collide with the mainCircle. But when the transition is finished, the bullet will bounce all over the simulator

I think my problem is, when i have my transition.to working, the physics engine will not be detected?

And I can’t think of other thing to replace transition.to my mainCircle.

[lua]display.setStatusBar(display.HiddenStatusBar)

local physics = require “physics”
physics.start()
physics.setGravity ( 0, 0 )

local w = display.contentWidth
local h = display.contentHeight
local cx = display.contentCenterX
local cy = display.contentCenterY

local bulletGroup = display.newGroup ()
bulletGroup.anchorChildren = true

local mainCircle = display.newCircle ( 10, 10, 20 )
mainCircle.x = w * 0.5
mainCircle.y = h * 0.5

local mainRect = display.newRect ( mainCircle.x, mainCircle.y, 15, 130 )
mainRect:setFillColor ( 0.45, 0.1, 1 )
physics.addBody (mainRect, “static”, {bounce = 50.1})

function movebullet ()

     transition.to ( bullet, {time = 5001, x = mainCircle.x, y = mainCircle.y } )

end

function rotate (event)

     mainRect.rotation = mainRect.rotation + 3

end

timer.performWithDelay ( 300, rotate, -1 )

function addbullets ()

     rand = math.random (4)

     if (rand < 2 ) then

          bullet = display.newCircle ( 0, 0, 5 )
          bullet.x = math.random (w * -0.11, w * -0.1); bullet.y = math.random (h * 0, h * 1);
          physics.addBody (bullet, “dynamic”, {bounce = 50.1})
          bulletGroup:insert (bullet)

     else if ( rand < 3 ) then

          bullet = display.newCircle ( 0, 0, 5 )
          bullet.x = math.random (w * 1.1, w * 1.11); bullet.y = math.random (h * 0, h * 1);
          physics.addBody (bullet, “dynamic”, {bounce = 10.1})
          bulletGroup:insert (bullet)

     else if ( rand < 4 ) then

          bullet = display.newCircle ( 0, 0, 5 )
          bullet.x = math.random (w * 0, w * 1); bullet.y = math.random (h * -0.15, h * -0.14);
          physics.addBody (bullet, “dynamic”, {bounce = 20.1})
          bulletGroup:insert (bullet)

     else if ( rand < 5 ) then

          bullet = display.newCircle ( 0, 0, 5 )
          bullet.x = math.random (w * 0, w * 1); bullet.y = math.random (h * 1.15, h * 1.16);
          physics.addBody (bullet, “dynamic”, {bounce = 30.1})
          bulletGroup:insert (bullet)

end
end
end
end

movebullet()
end

–Runtime:addEventListener (“touch”, addbullets)
timer.performWithDelay ( 500, addbullets, -1 )
[/lua]

Thanks Alot

Generally speaking you don’t want to mix transition.to and physics.  In the physics world, box2d needs to handle moving things.  When use transition.to, that’s movement outside of the physics engine and things get really confused.

Try using something like:

obj:applyForce()  – http://docs.coronalabs.com/api/type/Body/applyForce.html

or

obj:setLinearVelocity()  – http://docs.coronalabs.com/api/type/Body/setLinearVelocity.html

Rob

Thanks rob,

But applyforce doesn’t help me to transition the bullet from random x,y to a specific location

Can I? Or can i do it in some other ways

Anyone? please give me some advice to replace transition.to which direct my random x,y bullet to mainCircle

Hi @xming0529,

Is your intention to stop (block) the bullet with “mainRect” before it hits “mainCircle”? If so, you can still use transitions, but you must cancel the transition when the bullet collides with “mainRect”. Otherwise, the transition will continue attempting to resolve itself, which will cause the odd behavior that Rob described.

If you would like to use pure physics (no transitions), you could calculate the X/Y difference between the location of the bullet and “mainCircle” and then use those values to do “:setLinearVelocity()” on the bullet. Then, use collision detection to stop the bullet when it collides with the circle.

Take care,

Brent

Generally speaking you don’t want to mix transition.to and physics.  In the physics world, box2d needs to handle moving things.  When use transition.to, that’s movement outside of the physics engine and things get really confused.

Try using something like:

obj:applyForce()  – http://docs.coronalabs.com/api/type/Body/applyForce.html

or

obj:setLinearVelocity()  – http://docs.coronalabs.com/api/type/Body/setLinearVelocity.html

Rob

Thanks rob,

But applyforce doesn’t help me to transition the bullet from random x,y to a specific location

Can I? Or can i do it in some other ways

Anyone? please give me some advice to replace transition.to which direct my random x,y bullet to mainCircle

Hi @xming0529,

Is your intention to stop (block) the bullet with “mainRect” before it hits “mainCircle”? If so, you can still use transitions, but you must cancel the transition when the bullet collides with “mainRect”. Otherwise, the transition will continue attempting to resolve itself, which will cause the odd behavior that Rob described.

If you would like to use pure physics (no transitions), you could calculate the X/Y difference between the location of the bullet and “mainCircle” and then use those values to do “:setLinearVelocity()” on the bullet. Then, use collision detection to stop the bullet when it collides with the circle.

Take care,

Brent