Help to solve this physics and transition error.

Hello there…

Please watch video to get the clear idea of my problem. 

https://www.youtube.com/watch?v=7elycc3YMUE&feature=youtu.be

As you can see in the video, the rocket which come at the top of other two does not move to desired position. The all rockets are added to physics and this problem is caused.

When I don’t add these rockets to physics, they move to desired positions on tap and there is no problem…but when I add them to physics they does not move at all.

Also this happens only to rocket which is at top of other two. The bottom rockets to move, though they are added to physics…

Tried to solve myself, but getting confused…

here’s the code…

 local physics = require "physics" physics.start() physics.setGravity(0,0) local imgNum = math.random( 1, 16 ) local fileName = "s" .. imgNum .. ".png" local background = display.newImageRect(fileName, display.actualContentWidth, display.actualContentHeight) background.x = display.contentCenterX background.y = display.contentCenterY background.fileName = fileName print(fileName) local rect = display.newRect(240,100,140,500) rect.alpha = .5 local line = display.newImageRect("sl.png",5,600) line.x = 240 line.y = 280 line:setFillColor(0) line.alpha = .5 local line = display.newImageRect("sl.png",10,600) line.x = 180-12 line.y = 280 line:setFillColor(0) line.alpha = 1 local line = display.newImageRect("sl.png",10,600) line.x = 300+12 line.y = 280 line:setFillColor(0) line.alpha = 1 local bluerk = display.newImageRect("bluerk.png", 25, 25) bluerk.x = 205 bluerk.y = 250 bluerk.xScale = 2 bluerk.yScale = 2 bluerk.surfaceType = "rect1" physics.addBody(bluerk,"static") bluerk.isSensor = true local greenrk = display.newImageRect("greenrk.png", 25, 25) greenrk.x =185 greenrk.y = 280 physics.addBody(greenrk,"static") greenrk.surfaceType = "rect11" greenrk.isSensor = true local redrk = display.newImageRect("redrk.png", 25, 25) redrk.x =225 redrk.y = 280 physics.addBody(redrk,"static") redrk.surfaceType = "rect111" redrk.isSensor = true local sheet1 = graphics.newImageSheet( "fire1.png", { width=(960/5), height=(768/4), numFrames=19 } ) local bluefire = display.newSprite( sheet1, { start=5, count=10, time=1000 } ) bluefire.x = 205 bluefire.y = 285 bluefire.xScale = -.18 bluefire.yScale = .15 bluefire:setFillColor(0.2,.2,.8) bluefire:play() bluefire:rotate(180) local sheet1 = graphics.newImageSheet( "fire1.png", { width=(960/5), height=(768/4), numFrames=19 } ) local greenfire = display.newSprite( sheet1, { start=5, count=10, time=1000 } ) greenfire.x = 185 greenfire.y = 302 greenfire.xScale = -.18 greenfire.yScale = .15 greenfire:setFillColor(0.1,.8,.2) greenfire:play() greenfire:rotate(180) local sheet1 = graphics.newImageSheet( "fire1.png", { width=(960/5), height=(768/4), numFrames=19 } ) local redfire = display.newSprite( sheet1, { start=5, count=10, time=1000 } ) redfire.x = 225 redfire.y = 302 redfire.xScale = -.18 redfire.yScale = .15 redfire:setFillColor(0.8,.2,.2) redfire:play() redfire:rotate(180) local function swipeOk(self,event) if (self.x == 205) then transition.moveTo(self, { x=225, y=280, time=500 } ) self.xScale = .8 self.yScale = .8 elseif (self.x == 225) then transition.moveTo(self, { x=185, y=280, time=500 } ) self.xScale = .8 self.yScale = .8 elseif(self.x == 185) then transition.moveTo(self, { x=205, y=250, time=500 } ) self.xScale =2 self.yScale = 2 elseif (self.x == 275) then transition.moveTo(self, { x=295, y=280, time=500 } ) self.xScale = .8 self.yScale = .8 elseif (self.x == 295) then transition.moveTo(self, { x=255, y=280, time=500 } ) self.xScale = .8 self.yScale = .8 elseif(self.x == 255) then transition.moveTo(self, { x=275, y=250, time=500 } ) self.xScale =2 self.yScale = 2 end end local function ok() bluerk.enterFrame = swipeOk Runtime:addEventListener("enterFrame",bluerk) greenrk.enterFrame = swipeOk Runtime:addEventListener("enterFrame",greenrk) redrk.enterFrame = swipeOk Runtime:addEventListener("enterFrame",redrk) end local function pl() bluerk.enterFrame = swipeOk Runtime:removeEventListener("enterFrame",bluerk) greenrk.enterFrame = swipeOk Runtime:removeEventListener("enterFrame",greenrk) redrk.enterFrame = swipeOk Runtime:removeEventListener("enterFrame",redrk) end local function swipeOkk(self,event) if (self.x == 205) then transition.moveTo(self, { x=185, y=280, time=500 } ) self.xScale = .8 self.yScale = .8 elseif (self.x == 185) then transition.moveTo(self, { x=225, y=280, time=500 } ) self.xScale = .8 self.yScale = .8 elseif(self.x == 225) then transition.moveTo(self, { x=205, y=250, time=500 } ) self.xScale =2 self.yScale = 2 elseif (self.x == 275) then transition.moveTo(self, { x=255, y=280, time=500 } ) self.xScale = .8 self.yScale = .8 elseif (self.x == 255) then transition.moveTo(self, { x=295, y=280, time=500 } ) self.xScale = .8 self.yScale = .8 elseif(self.x == 295) then transition.moveTo(self, { x=275, y=250, time=500 } ) self.xScale =2 self.yScale = 2 end end local function okk() bluerk.enterFrame = swipeOkk Runtime:addEventListener("enterFrame",bluerk) greenrk.enterFrame = swipeOkk Runtime:addEventListener("enterFrame",greenrk) redrk.enterFrame = swipeOkk Runtime:addEventListener("enterFrame",redrk) end local function pll() bluerk.enterFrame = swipeOkk Runtime:removeEventListener("enterFrame",bluerk) greenrk.enterFrame = swipeOkk Runtime:removeEventListener("enterFrame",greenrk) redrk.enterFrame = swipeOkk Runtime:removeEventListener("enterFrame",redrk) end local function move(self,event) if (self.y == 250 and self.x == 205 ) then transition.moveTo(self, { x=275, y= 250, time=500 } ) print(self.surfaceType) elseif (self.y == 280 and self.x == 185) then transition.moveTo(self, { x=255, y= 280, time=500 } ) elseif (self.y == 280 and self.x == 225) then transition.moveTo(self, { x=295, y= 280, time=500 } ) elseif (self.y == 250 and self.x == 275 ) then transition.moveTo(self, { x=205, y= 250, time=500 } ) elseif (self.y == 280 and self.x == 255) then transition.moveTo(self, { x=185, y= 280, time=500 } ) elseif (self.y == 280 and self.x == 295) then transition.moveTo(self, { x=225, y= 280, time=500 } ) elseif (self.y == 285 and self.x == 205) then transition.moveTo(self, { x=275, y= 285, time=500 } ) elseif (self.y == 302 and self.x == 185) then transition.moveTo(self, { x=255, y= 302, time=500 } ) elseif (self.y == 302 and self.x == 225) then transition.moveTo(self, { x=295, y= 302, time=500 } ) elseif (self.y == 285 and self.x == 275) then transition.moveTo(self, { x=205, y= 285, time=500 } ) elseif (self.y == 302 and self.x == 255) then transition.moveTo(self, { x=185, y= 302, time=500 } ) elseif (self.y == 302 and self.x == 295) then transition.moveTo(self, { x=225, y= 302, time=500 } ) end end local function okkp() bluerk.enterFrame = move Runtime:addEventListener("enterFrame",bluerk) greenrk.enterFrame = move Runtime:addEventListener("enterFrame",greenrk) redrk.enterFrame = move Runtime:addEventListener("enterFrame",redrk) bluefire.enterFrame = move Runtime:addEventListener("enterFrame",bluefire) greenfire.enterFrame = move Runtime:addEventListener("enterFrame",greenfire) redfire.enterFrame = move Runtime:addEventListener("enterFrame",redfire) end local function pllp() bluerk.enterFrame = move Runtime:removeEventListener("enterFrame",bluerk) greenrk.enterFrame =move Runtime:removeEventListener("enterFrame",greenrk) redrk.enterFrame = move Runtime:removeEventListener("enterFrame",redrk) bluefire.enterFrame = move Runtime:removeEventListener("enterFrame",bluefire) greenfire.enterFrame =move Runtime:removeEventListener("enterFrame",greenfire) redfire.enterFrame = move Runtime:removeEventListener("enterFrame",redfire) end local beginX local beginY local endX local endY local xDistance local yDistance function checkSwipeDirection() xDistance = math.abs(endX - beginX) yDistance = math.abs(endY - beginY) if xDistance \> yDistance then if beginX \> endX then okk() gametimer = timer.performWithDelay(500,pll,1) else ok() gametimer = timer.performWithDelay(500,pl,1) end else if beginY \> endY then else okkp() gametimer = timer.performWithDelay(500,pllp,1) end end end function swipe(event) if event.phase == "began" then beginX = event.x beginY = event.y end if event.phase == "ended" then endX = event.x endY = event.y checkSwipeDirection(); end end Runtime:addEventListener("touch", swipe)

Hello,

Try to simplify the code plz, it’s un-readable lol. I try to see what fonction really done the translation, but i dont find lol.

I am sure it’s correct but they are so much data on this file that you miss a number lol.

Yvan.

  1. ( Preference suggestion ) I would suggest using the transition.to() function exclusively.  I don’t use any of the others.  I appreciate they are added, but transition.to() and transition.from() (for special cases) the only two functions you need.  Once you know how to

  2. I agree w/ @yvandotet.  This is too much code to read.  If you have more than about 500…100 lines for an example, that involves solving a behavior problem, consider making a standalone test case demonstrating he same problem, or zipping your project.

  3.  Transitions and physics don’t go well together unless you’re careful.  If you’re going to move the rockets, they should at least have “kinematic” and maybe even “dynamic” bodies.  Try “kinematic” first.

By using a ‘static’ body you are telling the physics engine: THIS WILL NOT MOVE EVER

Ok.

here’s the code which only deals with moving rockets to desired position…

 local bluerk = display.newImageRect("bluerk.png", 25, 25) bluerk.x = 205 bluerk.y = 250 bluerk.xScale = 2 bluerk.yScale = 2 bluerk.surfaceType = "rect1" physics.addBody(bluerk,"dynamic") bluerk.isSensor = true local greenrk = display.newImageRect("greenrk.png", 25, 25) greenrk.x =185 greenrk.y = 280 physics.addBody(greenrk,"dynamic") greenrk.surfaceType = "rect11" greenrk.isSensor = true local redrk = display.newImageRect("redrk.png", 25, 25) redrk.x =225 redrk.y = 280 physics.addBody(redrk,"dynamic") redrk.surfaceType = "rect111" redrk.isSensor = true local function move(self,event) if (self.y == 250 and self.x == 205 ) then transition.moveTo(self, { x=275, y= 250, time=500 } ) print(self.surfaceType) elseif (self.y == 280 and self.x == 185) then transition.moveTo(self, { x=255, y= 280, time=500 } ) elseif (self.y == 280 and self.x == 225) then transition.moveTo(self, { x=295, y= 280, time=500 } ) elseif (self.y == 250 and self.x == 275 ) then transition.moveTo(self, { x=205, y= 250, time=500 } ) elseif (self.y == 280 and self.x == 255) then transition.moveTo(self, { x=185, y= 280, time=500 } ) elseif (self.y == 280 and self.x == 295) then transition.moveTo(self, { x=225, y= 280, time=500 } ) end end local function addmove() bluerk.enterFrame = move Runtime:addEventListener("enterFrame",bluerk) greenrk.enterFrame = move Runtime:addEventListener("enterFrame",greenrk) redrk.enterFrame = move Runtime:addEventListener("enterFrame",redrk) end local function removemove() bluerk.enterFrame = move Runtime:removeEventListener("enterFrame",bluerk) greenrk.enterFrame =move Runtime:removeEventListener("enterFrame",greenrk) redrk.enterFrame = move Runtime:removeEventListener("enterFrame",redrk) end function checkSwipeDirection() xDistance = math.abs(endX - beginX) yDistance = math.abs(endY - beginY) if xDistance \> yDistance then if beginX \> endX then else end else if beginY \> endY then else addmove() gametimer = timer.performWithDelay(500,removemove,1) end end end function swipe(event) if event.phase == "began" then beginX = event.x beginY = event.y end if event.phase == "ended" then endX = event.x endY = event.y checkSwipeDirection(); end end Runtime:addEventListener("touch", swipe)
  1. Did you follow my suggestion and try a kinematic body?  I see you modified your code, but I’m not sure if you actually tried this since you didn’t comment.

2.Have you tried making a test case to verify that the problem is with bodies and transitions?

 local bluerk = display.newImageRect("bluerk.png", 25, 25) bluerk.x = 205 bluerk.y = 250 bluerk.xScale = 2 bluerk.yScale = 2 bluerk.surfaceType = "rect1" physics.addBody(bluerk,"kinematic") bluerk.isSensor = true transition.to(bluerk { x=275, y= 250, time=500 } ) -- moveTo.... ick. I use transition.to()

You can reduce your code, there is a lot of stuff that looks the same.

I have only watched the video

I think your problem is from transition, you have different transition on same objects and if two transitions modify a same property, the older transition is cancel. 

You can simplify your work, by creating a group for your objects

Like this you will be able to move each object in the group separately (change position of rockets).

And you will move the group (all rocket at the same time), 

Note: I see you are scaling the objects after you add bodies.

Be aware, this won’t change the size of the physics body.  Physics bodies cannot be scaled.

I tried to change body type to kinematic as well I tried using move to command…but it didn’t work too…

may be as you are saying the error is from transition command…I trying to solve the error by the method you are telling…

Also can I detect collision without adding objects to physics?? If so please provide me an small example…

Check this out:

https://coronalabs.com/blog/2013/07/23/tutorial-non-physics-collision-detection/

Hi. I think you’ve gotten off track.

The problems is not with physics and transitions.  I’m sure there is a bug in your usage of transitions.  

You might need to get personal help from someone or to release a zip of your project for the community to look at.  Trying to debug huge pieces of code standalone is too hard.

-Ed

Hello,

Try to simplify the code plz, it’s un-readable lol. I try to see what fonction really done the translation, but i dont find lol.

I am sure it’s correct but they are so much data on this file that you miss a number lol.

Yvan.

  1. ( Preference suggestion ) I would suggest using the transition.to() function exclusively.  I don’t use any of the others.  I appreciate they are added, but transition.to() and transition.from() (for special cases) the only two functions you need.  Once you know how to

  2. I agree w/ @yvandotet.  This is too much code to read.  If you have more than about 500…100 lines for an example, that involves solving a behavior problem, consider making a standalone test case demonstrating he same problem, or zipping your project.

  3.  Transitions and physics don’t go well together unless you’re careful.  If you’re going to move the rockets, they should at least have “kinematic” and maybe even “dynamic” bodies.  Try “kinematic” first.

By using a ‘static’ body you are telling the physics engine: THIS WILL NOT MOVE EVER

Ok.

here’s the code which only deals with moving rockets to desired position…

 local bluerk = display.newImageRect("bluerk.png", 25, 25) bluerk.x = 205 bluerk.y = 250 bluerk.xScale = 2 bluerk.yScale = 2 bluerk.surfaceType = "rect1" physics.addBody(bluerk,"dynamic") bluerk.isSensor = true local greenrk = display.newImageRect("greenrk.png", 25, 25) greenrk.x =185 greenrk.y = 280 physics.addBody(greenrk,"dynamic") greenrk.surfaceType = "rect11" greenrk.isSensor = true local redrk = display.newImageRect("redrk.png", 25, 25) redrk.x =225 redrk.y = 280 physics.addBody(redrk,"dynamic") redrk.surfaceType = "rect111" redrk.isSensor = true local function move(self,event) if (self.y == 250 and self.x == 205 ) then transition.moveTo(self, { x=275, y= 250, time=500 } ) print(self.surfaceType) elseif (self.y == 280 and self.x == 185) then transition.moveTo(self, { x=255, y= 280, time=500 } ) elseif (self.y == 280 and self.x == 225) then transition.moveTo(self, { x=295, y= 280, time=500 } ) elseif (self.y == 250 and self.x == 275 ) then transition.moveTo(self, { x=205, y= 250, time=500 } ) elseif (self.y == 280 and self.x == 255) then transition.moveTo(self, { x=185, y= 280, time=500 } ) elseif (self.y == 280 and self.x == 295) then transition.moveTo(self, { x=225, y= 280, time=500 } ) end end local function addmove() bluerk.enterFrame = move Runtime:addEventListener("enterFrame",bluerk) greenrk.enterFrame = move Runtime:addEventListener("enterFrame",greenrk) redrk.enterFrame = move Runtime:addEventListener("enterFrame",redrk) end local function removemove() bluerk.enterFrame = move Runtime:removeEventListener("enterFrame",bluerk) greenrk.enterFrame =move Runtime:removeEventListener("enterFrame",greenrk) redrk.enterFrame = move Runtime:removeEventListener("enterFrame",redrk) end function checkSwipeDirection() xDistance = math.abs(endX - beginX) yDistance = math.abs(endY - beginY) if xDistance \> yDistance then if beginX \> endX then else end else if beginY \> endY then else addmove() gametimer = timer.performWithDelay(500,removemove,1) end end end function swipe(event) if event.phase == "began" then beginX = event.x beginY = event.y end if event.phase == "ended" then endX = event.x endY = event.y checkSwipeDirection(); end end Runtime:addEventListener("touch", swipe)
  1. Did you follow my suggestion and try a kinematic body?  I see you modified your code, but I’m not sure if you actually tried this since you didn’t comment.

2.Have you tried making a test case to verify that the problem is with bodies and transitions?

 local bluerk = display.newImageRect("bluerk.png", 25, 25) bluerk.x = 205 bluerk.y = 250 bluerk.xScale = 2 bluerk.yScale = 2 bluerk.surfaceType = "rect1" physics.addBody(bluerk,"kinematic") bluerk.isSensor = true transition.to(bluerk { x=275, y= 250, time=500 } ) -- moveTo.... ick. I use transition.to()

You can reduce your code, there is a lot of stuff that looks the same.

I have only watched the video

I think your problem is from transition, you have different transition on same objects and if two transitions modify a same property, the older transition is cancel.