Keeping an Object's physics/velocity through portals?

Hello,

I’m working on a game where a character (Sophie) falls through a rift or portal device and comes out of a second one the user has placed with his/her finger.  I’m trying to keep her velocity and/or other physics information.  The problem is, when I call the transition.to for her to move from one rift portal to another, all of that physics information is lost.  Some times she arrives at the other rift portal and just sits on top.  I’ve posted the code below.  Does anybody have any ideas?

display.setStatusBar( display.HiddenStatusBar ) local physics = require "physics" physics.start() physics.setGravity (0, 10) local w = display.contentWidth local h = display.contentHeight --graphics --[background] local bg = display.newImage("lab320.png") --[titleView] local title local doorRight local doorLeft local playBtn local creditsBtn local titleView --[creditsView] local creditsView --instructions local ins --Alert local alertView --sounds local whiteNoise = audio.loadStream('whiteNoise.mp3') local riftSound = audio.loadSound("riftOpen.mp3") local introSound = audio.loadSound('startDoorOpen.mp3') --variables local rift1 local rift2 --functions local Main = {} local startButtonListeners = {} local showCredits = {} local hideCredits = {} local showGameView = {} local gameListeners = {} local putRift = {} local addRift = {} local onCollision = {} local alert = {} --rift Block local riftBlock1 local riftBlock2 local riftBeam local riftBeamSound = audio.loadSound("finalRiftBeam.mp3") local riftChargeSound = audio.loadSound("finalRiftCharge.mp3") --rifts local rift1 local rift2 --riftVariable local riftOn = 0 --room 1 local room1 = {} local room1Rift = {} local room1RiftSpace = {} --room items local sophie local catFood local cubeNormal local cubeLedge --rift direction local riftDirection = 0 local vx local vy function Main() audio.play(whiteNoise, {loops = 1}) doorRight = display.newImage("rightDoor.png") doorRight.xScale = .32 doorRight.yScale = .32 doorRight.x = w - 160 doorRight.y = h - 250 doorRight.alpha = 0 doorLeft = display.newImage("leftDoor.png") doorLeft.xScale = .32 doorLeft.yScale = .32 doorLeft.x = w - 168 doorLeft.y = h - 250 doorLeft.alpha = 0 title = display.newImage("logoTouch2.png") title.alpha = 0 transition.to(title, {time = 1000, alpha = 1}) title.xScale = .32 title.yScale = .32 title.x = w - 160 title.y = h - 250 startButtonListeners('add') --code end function startButtonListeners(action) if(action == 'add') then title : addEventListener('tap', showGameView) else title:removeEventListener('tap', showGameView) end end function showGameView:tap(e) title:removeEventListener('tap', showGameView) audio.play(introSound) transition.to(title, {time = 500, alpha = 0}) transition.to(doorLeft, {time = 2000, alpha = 1, x = -400}) transition.to(doorRight, {time = 2000, alpha = 1, x = 430, onComplete = room1}) end --this is sophie's collision detection. DO NOT MOVE THIS INSIDE ROOM FUNCTIONS local function sophieCollision(self, event) if event.phase == "began" then if event.target.type == "sophie" and event.other.type == "rift1" then print("rift") vx, vy = sophie:getLinearVelocity() print(vx, vy) transition.to(sophie, {time = 001, x = rift2.x, y = rift2.y}) else if event.target.type == "sophie" and event.other.type == "rift2" then --sophie:setLinearVeolicty(xVelocity, yVelocity) print("shooint out of rift 2") else print("foo") end end end end function room1() print(riftOn) sophie = display.newRect(130,0,50,50) sophie:setFillColor( 0.5 ) sophie.collision = sophieCollision sophie.type = "sophie" function startExperiment() if riftOn == 1 then physics.addBody(sophie, {density = 1.0, friction = .3, bounce = .3}) end end --[[cubeNormal = display.newImage("cubeStationary.png") cubeNormal.x = w - 160 cubeNormal.y = h - 240 cubeNormal.alpha = 0 transition.to(cubeNormal, {time = 500, alpha = 1}) physics.addBody(cubeNormal, "static", {density = 1.0, friction = .3, bounce = .3}) --]] cubeLedge = display.newImage("landingPad.png") cubeLedge.x = w - 160 cubeLedge.y = h - 10 cubeLedge.alpha = 0 transition.to(cubeLedge, {time = 500, alpha = 1}) physics.addBody(cubeLedge, "static", {density = 1.0, friction = .3, bounce = .3}) catFood = display.newImage("catFood.png") catFood.x = w - 160 catFood.y = h - 50 catFood.alpha = 0 transition.to(catFood, {time = 500, alpha = 1}) audio.play(riftBeamSound) riftBeam = display.newImage("beamLine.png") riftBeam.yScale = .5 riftBeam.x = w - 300 riftBeam.y = h - 225 riftBeam.alpha = 0 function myBeam() --physics collisions must occur in this area audio.play(riftChargeSound) transition.to(riftBeam, {time = 1000, alpha = .5}) end riftBlock1 = display.newImage("riftBlockBall.png") riftBlock1.xScale = .05 riftBlock1.yScale = .05 riftBlock1.x = w + 15 riftBlock1.y = h - 600 transition.to(riftBlock1, {time = 1000, x = w-15, y = h-225}) riftBlock2 = display.newImage("riftBlockBall.png") riftBlock2.xScale = .05 riftBlock2.yScale = .05 riftBlock2.x = w - 300 riftBlock2.y = h - 600 transition.to(riftBlock2, {time = 1000, x = w-300, y = h-225, onComplete = myBeam}) --stand alone rift local sheetData = { width=64, height=32, numFrames=8, sheetContentWidth=128, sheetContentHeight=128 } local mySheet = graphics.newImageSheet( "riftSheet.png", sheetData ) local sequenceData = { { name = "normalRun", start=1, count=8, time=150 }, { name = "fastRun", frames={ 1,2,4,5,6,7 }, time=250 } } rift1 = display.newSprite( mySheet, sequenceData ) --animation.x = display.contentWidth/2 --center the sprite horizontallyanimation.y = display.contentHeight/2 --center the sprite vertically animation:play() rift1.x = display.contentWidth/2 rift1.y = display.contentHeight/3 - 5 rift1:play() --audio.play(riftSound) --rift2 = display.newImage("purple.png") --rift1.xScale = .2 --rift1.yScale = .2 rift1.type = "rift1" physics.addBody(rift1, "static", {density = 0, friction = 0, bounce = 0}) --touch button below title:removeSelf() title = nil print("room1") room1RiftSpace = display.newImage("drawRift.png") room1RiftSpace.y = h - 100 room1RiftSpace : addEventListener("touch", room1Rift) sophie : addEventListener("tap", startExperiment) sophie : addEventListener("collision", sophie) end function room1Rift(event) print("touching") if event.phase == "ended" and riftOn == 0 then --below line creates rift2 and then animates it local sheetData = { width=64, height=32, numFrames=8, sheetContentWidth=128, sheetContentHeight=128 } local mySheet = graphics.newImageSheet( "riftSheet.png", sheetData ) local sequenceData = { { name = "normalRun", start=1, count=8, time=150 }, { name = "fastRun", frames={ 1,2,4,5,6,7 }, time=250 } } rift2 = display.newSprite( mySheet, sequenceData ) --animation.x = display.contentWidth/2 --center the sprite horizontallyanimation.y = display.contentHeight/2 --center the sprite vertically animation:play() rift2.x = event.x --center the sprite horizontally rift2.y = event.y --center the sprite vertically rift2:play() --audio.play(riftSound) --rift2 = display.newImage("purple.png") --rift2.xScale = .2 --rift2.yScale = .2 --rift2.x = event.x --rift2.y = event.y --STOP RIFT CREATION rift2.rotation = -90 riftOn = 1 --this line turns off the ability to generate more rifts physics.addBody(rift2, "static", {density = 0, friction = 0, bounce = 0}) rift2.type = "rift2" function rotateMe() if event.phase == "ended" then rift2.rotation = rift2.rotation + 90 riftDirection = riftDirection + 1 if riftDirection == 5 then riftDirection = 1 end print(riftDirection) audio.play(riftSound) print("rotate") end return true end rift2 : addEventListener("tap", rotateMe) end return true end Main()

Hi @lakelodgegames,

Physics and transitions can be “mixed” but you need to handle it properly. Basically, if you have a movement transition going, and the object collides with something, you need to cancel and possibly “reconfigure” the transition. This is because the transition will still try to “steal the show” and take over after the collision has occurred.

This might not specifically address the issue you’re having, but I generally wanted to caution you on using physics and transitions together. They definitely can be used in harmony, but it usually takes a bit of extra handling.

Brent

Thanks for the reply, Brent.  I understand what you’re saying and I’ll take this into account as I keep trying to plug this problem!

Hi @lakelodgegames,

Physics and transitions can be “mixed” but you need to handle it properly. Basically, if you have a movement transition going, and the object collides with something, you need to cancel and possibly “reconfigure” the transition. This is because the transition will still try to “steal the show” and take over after the collision has occurred.

This might not specifically address the issue you’re having, but I generally wanted to caution you on using physics and transitions together. They definitely can be used in harmony, but it usually takes a bit of extra handling.

Brent

Thanks for the reply, Brent.  I understand what you’re saying and I’ll take this into account as I keep trying to plug this problem!