How to make "gravity spheres" that when placed, will attract certain objects in certain radius.

Hey,

I’m planning to make a ‘gravity sphere’ for my game but don’t know where to start. I have a lot of moving ‘enemies’ on screen and I want my gravity sphere to attract enemies if they are close enough.

I think to do that I should add an enterFrame listener to each enemy to check the distance from any gravity sphere, and if they are close enough do something with physics? Would it affect the performance if I have 40+ enemies at once on screen?

EDIT: After I do this, the physics of the enemies get weird. It becomes extra bouncy… I can’t understand why…

EDIT 2: Turns out I was using self:applyForce( sx/len, sy/len, 0, 0 ) and I should have used self:applyForce( sx/len, sy/len, self.x, self.y ). The enemies were rotating like crazy and that was the reason of weird physics results! :slight_smile:

Final Edit: Okay, this is the code that is working nicely, and I haven’t noticed any performance issues even with 10+ objects are in collision. Here it is if anyone needs it:

 if event.other.name == "gravitySphereSensor" then if event.phase == "began" then if(self.gravityForceTimer ~= nil) then timer.cancel(self.gravityForceTimer) self.gravityForceTimer = nil end self.gravityForceTimer = timer.performWithDelay(20, function() local vx = event.other.x - self.x local vy = event.other.y - self.y local len = math.sqrt(vx \* vx + vy \* vy) local sx = (vx/(len \* 2)) local sy = math.abs((vy/(len \* 4))) local xVel, yVel = self:getLinearVelocity() local speed = math.sqrt(xVel \* xVel + yVel \* yVel) if(speed \< 550) then self:applyLinearImpulse( sx, -sy, self.x, self.y ) end end, 0)

Hi CBT, I’ve used a similar function in a previous game… What you’re asking, is pretty tough… I ended up using Particle Candy (not free). It uses attractors and deflectors ( see: http://www.x-pressive.com/ParticleCandy_Corona/reference.html#F0 )

…I thought it was going to be a long work around, but you can add physics to the particles and give them an infinite life-span, making them no diferent than any other physics body… It will do exactly what you are looking for. It’s not expensive either.

M

But I want my actual ‘enemy’ actors to get effected. They spawn from somewhere else and there are 5+ kinds of them. I never dealt with particles before in Corona but afaik particles are not made for this…?

EDIT: After I do this, the physics of the enemies get weird. It becomes extra bouncy… I can’t understand why…

EDIT 2: Turns out I was using self:applyForce( sx/len, sy/len, 0, 0 ) and I should have used self:applyForce( sx/len, sy/len, self.x, self.y ). The enemies were rotating like crazy and that was the reason of weird physics results! :slight_smile:

Final Edit: Okay, this is the code that is working nicely, and I haven’t noticed any performance issues even with 10+ objects are in collision. Here it is if anyone needs it:

 if event.other.name == "gravitySphereSensor" then if event.phase == "began" then if(self.gravityForceTimer ~= nil) then timer.cancel(self.gravityForceTimer) self.gravityForceTimer = nil end self.gravityForceTimer = timer.performWithDelay(20, function() local vx = event.other.x - self.x local vy = event.other.y - self.y local len = math.sqrt(vx \* vx + vy \* vy) local sx = (vx/(len \* 2)) local sy = math.abs((vy/(len \* 4))) local xVel, yVel = self:getLinearVelocity() local speed = math.sqrt(xVel \* xVel + yVel \* yVel) if(speed \< 550) then self:applyLinearImpulse( sx, -sy, self.x, self.y ) end end, 0)

Hi CBT, I’ve used a similar function in a previous game… What you’re asking, is pretty tough… I ended up using Particle Candy (not free). It uses attractors and deflectors ( see: http://www.x-pressive.com/ParticleCandy_Corona/reference.html#F0 )

…I thought it was going to be a long work around, but you can add physics to the particles and give them an infinite life-span, making them no diferent than any other physics body… It will do exactly what you are looking for. It’s not expensive either.

M

But I want my actual ‘enemy’ actors to get effected. They spawn from somewhere else and there are 5+ kinds of them. I never dealt with particles before in Corona but afaik particles are not made for this…?