hey everybody
i have recently just started using the radial gravity code from the tutorial and i have started having some problems
first of all, basically when an object enters the field it want it to be attracted by the gravity which is what it is doing but you can only apply the radial gravity to one field using the
setTarget( )
and when i try using two of these in the same function it glitches out were as it when an object hit a filed (any two of them, it only attracts to one. so then i tried using
setTarget(self.x,self.y )
and when the object just entered the field it was only attracted to the edges of the field, not the center.
How do i fix this major problem?
here is my code. I only took the relevant parts of the code.
Thanks, Hazza
[lua]
–
– 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 © 2013 Corona Labs Inc. All Rights Reserved.
local physics = require(“physics”) ; physics.start() ; physics.setGravity( 0,0 ) ; physics.setDrawMode( “hybrid” )
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
--set up world and background
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”, 180,180 ) ; field.alpha = 0.3
field.name = “field”
field.x = display.contentCenterX - 60 ; field.y = display.contentCenterY - 60
physics.addBody( field, “static”, { isSensor=true, radius=90, filter=fieldFilter } )
–field.touchJoint:setTarget(field.x,field.y )
field:setReferencePoint( display.CenterReferencePoint )
planet = display.newImageRect( “Earth.png”,40,40 )
planet:scale( -2, -2 )
planet.name = “magnet”
planet.x = field.x ; planet.y = field.y
physics.addBody( planet, “static”, { bounce=0, radius=31.75, filter=magnetFilter } )
secondField = display.newImageRect(“field.png”, 180,180 )
secondField.name = “field”
physics.addBody( secondField, “static”, { isSensor=true, radius=90, filter=fieldFilter } )
secondField.x = 300
secondField.y = 240
secondPlanet = display.newImageRect(“Earth.png”, 40,40 )
secondPlanet:scale(-2,-2)
secondPlanet.name = “magnet”
secondPlanet.x = secondField.x
secondPlanet.y = secondField.y
physics.addBody( secondPlanet, “static”, { bounce=0, radius=31.75, filter=magnetFilter } )
magnet2 = display.newImageRect( “magnet.png”,70,70 )
magnet2.name = “magnet”
magnet2.isSensor = true
magnet2.x = display.contentCenterX - 220 ; magnet2.y = display.contentCenterY
–physics.addBody( magnet2, “static”, { bounce=0,radius = 40, filter=magnetFilter } )
end
setupWorld()
local function objectCollide( self, event )
local otherName = event.other.name
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.0
self.touchJoint:setTarget(self.x,self.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.touchJoint:removeSelf() ; self.touchJoint = nil
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”
elseif ( event.phase == “ended” and otherName == “field” and self.hasJoint == true ) then
local tr = timer.performWithDelay( 10, onDelay ) ; tr.action = “leftField”
end
end
local function newObj ()
obj = display.newImageRect( “object.png”, 48, 48 )
physics.addBody( obj, { density = 15,bounce=0, radius=12, filter=objFilter } )
obj.x = magnet2.x
obj.y = magnet2.y
obj.hasJoint = false
projFiring = falseobj.isBullet = true
obj.isBodyActive = false
obj.isVisible = false
obj.collision = objectCollide ; obj:addEventListener( “collision”, obj )
end
[/lua]