hey everybody
i am having a problem with the weld joint
basically, when the planet is moving/transitioning i also want the gravity field to also move with, so i tried using the weld joint trying to connect them except i ran into a problem, when the planet is transitioning for a second or two, the gravity field is joined to the planet but then immediately detaches when it keeps on moving. when i fire a projectile at the field, it seems as if the field immediately updates it self at the planets position but is still not joined to the planet, how can i fix this?
here is my code,hope you can help!
----------------------------------------------------------------------------------------- -- -- main.lua -- ----------------------------------------------------------------------------------------- -- Your code here -- Physics Demo: Radial Gravity | Version: 1.0 -- Sample code is MIT licensed, see http://www.coronalabs.com/links/code/license -- Copyright (C) 2013 Corona Labs Inc. All Rights Reserved. local physics = require("physics") ; physics.start() ; physics.setGravity( 0,0 ) ; physics.setDrawMode( "normal" ) display.setStatusBar( display.HiddenStatusBar ) math.randomseed( os.time() ) local ox, oy = math.abs(display.screenOriginX), math.abs(display.screenOriginY) local cw, ch = display.contentWidth, display.contentHeight --set up collision filters local screenFilter = { categoryBits=2, maskBits=1 } local objFilter = { categoryBits=1, maskBits=14 } local fieldFilter = { categoryBits=4, maskBits=1 } local magnetFilter = { categoryBits=8, maskBits=1 } --set initial magnet pull local magnetPull = 0.25 local centerX = display.contentWidth\*.5 local centerY = display.contentHeight \* .5 local obj local projFiring local magnet2 local displayScore local scoreObj local score = 0 local displayAmmo local ammoObj local Ammo = 6 local field local magnet local secondField local platform local rec1 local rec2 --set up world and background local function objectCollide( self, event ) local otherName = event.other.name local other = event.other local function onDelay( event ) local action = "" if ( event.source ) then action = event.source.action ; timer.cancel( event.source ) end if ( action == "makeJoint" ) then self.hasJoint = true self.touchJoint = physics.newJoint( "touch", self, self.x, self.y ) self.touchJoint.frequency = magnetPull self.touchJoint.dampingRatio = 0 self.touchJoint:setTarget(other.x,other.y ) elseif ( action == "leftField" ) then self.hasJoint = false ; self.touchJoint:removeSelf() ; self.touchJoint = nil else end end if ( event.phase == "ended" and otherName == "screenBounds" ) then local tr = timer.performWithDelay( 10, onDelay ) ; tr.action = "leftScreen" elseif ( event.phase == "began" and otherName == "magnet" ) then transition.to( self, { time=400, alpha=0, onComplete=onDelay } ) self.hasJoint = false self:removeSelf( ) self = nil elseif ( event.phase == "began" and otherName == "field" and self.hasJoint == false ) then local tr = timer.performWithDelay( 10, onDelay ) ; tr.action = "makeJoint" print("i made a joint") elseif ( event.phase == "ended" and otherName == "field" and self.hasJoint == true ) then local tr = timer.performWithDelay( 10, onDelay ) ; tr.action = "leftField" self.hasJoint = false end end local function setupWorld() local back = display.newImage( "background.png" ) ; back.x = cw/2 ; back.y = ch/2 ; local screenBounds = display.newRect( -ox, -oy, display.contentWidth+ox+ox, display.contentHeight+oy+oy ) screenBounds.name = "screenBounds" screenBounds.isVisible = false ; physics.addBody( screenBounds, "static", { isSensor=true, filter=screenFilter } ) --[[field = display.newImageRect( "field.png",160,160 ) ; field.alpha = 0.3 field.name = "field" field.x = display.contentCenterX - 70 ; field.y = display.contentCenterY physics.addBody( field, "static", { isSensor=true, radius=73, filter=fieldFilter } ) planet = display.newImageRect( "Earth.png",30,30 ) planet:scale( -2, -2 ) planet.name = "magnet" planet.x = field.x ; planet.y = field.y physics.addBody( planet, "static", { bounce=0, radius=18.55, filter=magnetFilter } )--]] --[[secondField = display.newImageRect("field.png", 160,160 );secondField.alpha = 0.3 secondField.name = "field" physics.addBody( secondField, "static", { isSensor=true, radius=73, filter=fieldFilter } ) secondField.x = 340 secondField.y = centerY secondPlanet = display.newImageRect("Earth.png", 30,30 ) secondPlanet:scale(-2,-2) secondPlanet.name = "magnet" secondPlanet.x = secondField.x secondPlanet.y = secondField.y physics.addBody( secondPlanet, "static", { bounce=0, radius=18.55, filter=magnetFilter } )--]] magnet2 = display.newImageRect( "magnet.png",70,70 ) magnet2.name = "magnet" magnet2.isSensor = true magnet2.x = display.contentCenterX + 230 ; magnet2.y = display.contentCenterY magnet2:setFillColor( 48, 245, 255 ) --physics.addBody( magnet2, "static", { bounce=0,radius = 40, filter=magnetFilter } ) end setupWorld() local function batter() planet = display.newImageRect( "Earth.png",30,30 ) planet:scale( -2, -2 ) planet.name = "magnet" planet.x = centerX + 95 ; planet.y = centerY - 135 physics.addBody( planet, "static", { bounce=0, radius=18.55, filter=magnetFilter } ) field = display.newImageRect( "field.png",160,160 ) ; field.alpha = 0.3 field.name = "field" field.x = planet.x ; field.y = planet.y physics.addBody( field, "dynamic", { isSensor=true, radius=73, filter=fieldFilter } ) physics.newJoint( "weld", field,planet,planet.x,planet.y ) local move\_timer = nil local trans = nil local function move\_one() print("move\_one") local function move\_two() print("move\_two") trans = transition.to(planet, {time=1000, y=265, onComplete=move\_one}) end trans = transition.to(planet, {time=1000, y=centerY - 105, onComplete=move\_two}) end move\_one() end local function batter2() secondPlanet = display.newImageRect("Earth.png", 30,30 ) secondPlanet:scale(-2,-2) secondPlanet.name = "magnet" secondPlanet.x = centerX - 95 secondPlanet.y = centerY + 135 physics.addBody( secondPlanet, "static", { bounce=0, radius=18.55, filter=magnetFilter } ) secondField = display.newImageRect("field.png", 160,160 );secondField.alpha = 0.3 secondField.name = "field" physics.addBody( secondField, "dynamic", { isSensor=true, radius=73, filter=fieldFilter } ) secondField.x = secondPlanet.x secondField.y = secondPlanet.y physics.newJoint( "weld", secondField,secondPlanet,secondPlanet.x,secondPlanet.y ) local move\_timer = nil local trans = nil local function move\_one() print("move\_one") local function move\_two() print("move\_two") trans = transition.to(secondPlanet, {time=1000, y=50, onComplete=move\_one}) end trans = transition.to(secondPlanet, {time=1000, y=centerY + 105, onComplete=move\_two}) end move\_one() end local function addPoints(points) score = score + points scoreObj.text = score scoreObj:setReferencePoint(display.CenterLeftReferencePoint) end local function minusAmmo(ammoStuff) Ammo = Ammo - ammoStuff ammoObj.text = Ammo ammoObj:setReferencePoint(display.CenterLeftReferencePoint) end local function showStuff() displayAmmo = display.newText("projectiles: ",0,0,native.systemFont,15) --scoreLabel:setFillColor(246, 255, 218) displayAmmo.x = 10 displayAmmo.y = 30 ammoObj = display.newText(tostring(Ammo),0,0,native.systemFont,15) ammoObj.x = displayAmmo.x + 40 ammoObj.y = displayAmmo.y end local function pointsCollide(event,self) local phase = event.phase if (phase == "began") then if (event.other.type == "Melon") then addPoints(10) event.other:removeSelf( ) end end end displayScore = display.newText("Score: ",0,0,native.systemFont,15) --scoreLabel:setFillColor(246, 255, 218) displayScore.x = 450 displayScore.y = 30 scoreObj = display.newText(tostring(score),0,0,native.systemFont,15) scoreObj.x = displayScore.x + 35 scoreObj.y = displayScore.y local function buildPoints() points2 = display.newImageRect( "coin.png",40,40) points2.type = "Melon" physics.addBody(points2,"dynamic",{ radius = 20,filter=magnetFilter}) points2.isSensor = true points2.x = centerX - 230 points2.y = centerY points2.collision = pointsCollide points2:addEventListener( "collision", pointsCollide ) end local function newObj () obj = display.newImageRect( "object.png", 48, 48 ) physics.addBody( obj, { density = 15,bounce=1, radius=12,friction =0.3, filter=objFilter } ) obj.x = magnet2.x obj.y = magnet2.y obj.hasJoint = false projFiring = false obj.isBullet = true obj.isBodyActive = false obj.isVisible = false obj.collision = objectCollide ; obj:addEventListener( "collision", obj ) obj:addEventListener("collision",pointsCollide) end local function touch (event) if (event.phase == "began" and projFiring == false) then minusAmmo(1) projFiring = true obj.isBodyActive = true obj:applyTorque( 200 ) obj.isVisible = true local px,py = event.x-obj.x, event.y-obj.y obj:applyLinearImpulse( px/4.5, py/4.5, obj.x, obj.y ) obj:applyTorque( 200 ) if (Ammo == 0) then projFiring = nil end if (Ammo \> 0) then projFiring = true end if (projFiring == true) then newObj() end end end newObj() buildPoints() showStuff() batter() batter2() Runtime:addEventListener( "touch", touch )