adding and removing physics

Just wondering is it possible for physics to be added to objects onscreen and removed to objects off screen? I’m making a pretty big sandbox game that requires free roaming and for objects to be spawned off screen to the player

I have followed danny’s tutorial for spawning objects - the right way!

and my code goes somewhat like this.

[lua]
local function spawnPeeps(params)
xRandom = math.random(1,10000)
yRandom = math.random(1,10000)
local peeps = display.newCircle( xRandom, yRandom, 15)

–Set the objects table to a table passed in by parameters
peeps.objTable = params.objTable

–Automatically set the table index to be inserted into the next available table index
peeps.index = #peeps.objTable + 1

– local peepsNumber = display.newText(peeps.index, xRandom, yRandom, nil, 12)
– peepsNumber:setTextColor(0,0,0)
print(peeps.index, #peeps.objTable )
–Give the object a custom name
peeps.myName = "peeps : " … peeps.index

–If the object should have a body create it, else dont.
if params.hasBody then
–Allow physics parameters to be passed by parameters:
peeps.density = params.density or 0
peeps.friction = params.friction or 0
peeps.bounce = params.bounce or 0
peeps.isSensor = params.isSensor or false
peeps.bodyType = params.bodyType or “dynamic”

physics.addBody(peeps, peeps.bodyType, {density = peeps.density, friction = peeps.friction, bounce = peeps.bounce, isSensor = peeps.isSensor})
end

– checking whether it’s a peep
peeps.isPeep = true

–check if alive
peeps.isAlive = true

–checking collision with phys trig
peeps.isActive = true

–The objects group
peeps.group = params.group or nil

–If the function call has a parameter named group then insert it into the specified group
peeps.group:insert(peeps)

–Insert the object into the table at the specified index
peeps.objTable[peeps.index] = peeps

return peeps
end

–Create a table to hold our spawns
local spawnTable = {}

params = {
objTable = spawnTable,

hasBody=true,
friction = 0.4,
bounce = 0.4,
bodyType = “dynamic”,
group = group,
}
–Spawn two objects
for i = 1, 2000 do
local spawns = spawnPeeps(params)
end
[/lua] [import]uid: 87730 topic_id: 36384 reply_id: 336384[/import]

Hi @jazzarssoul,
You can definitely remove physics bodies when you need to, and you should, especially if your intention is to create 2000 physics objects as in your example. In fact, creating that many would probably crash and burn, or perform terribly slow at least.

Even better would be a method in which you “recycle” similar objects… but the player never sees it happening. Meaning, you find a way to use a very small set of physics objects and swap them in and out of a “repository” as they enter or exit the screen. I did this in a recent game and it gave me the best performance boost of any others I had attempted.

Brent [import]uid: 200026 topic_id: 36384 reply_id: 144493[/import]

Hey Brent my situation may need to be described a little more clearly.

I have a certain amount of xp the character needs to work up to, to level up.
To make up for the fact that the higher the xp required the higher amount of people that get eaten needs to respawn.
So per eat of people spawn either 1/2 and to make up for said numbers I was wondering through the spawning method as mentioned is there a way to add and keep physics to the objects on screen + 1/2 the screen width and remove and keep physics away from the objects outside these bounds [import]uid: 87730 topic_id: 36384 reply_id: 144612[/import]

Hi @jazzarssoul,
Certainly this should be possible. You might also consider a screen “region” which is an invisible sensor object. Objects that move outside that region are either made inactive or the physics body removed. Objects that move inside the bounds are reactivated. Ultimately, the implementation is up to you… but the scenario sounds feasible, absolutely.

Brent [import]uid: 200026 topic_id: 36384 reply_id: 144766[/import]

I had imagined this particular scenario would be the case in which there is an object region that follows the movement of the player and triggering physics as it goes but I however have tried this strategy and failed miserably…

So I kinda ditched the effort and hoped there was an easier way. Is there a way you can provide me with an example or start me in the right direction Brent? [import]uid: 87730 topic_id: 36384 reply_id: 144775[/import]

Have you implemented some kind of “camera” movement? Meaning, the entire world is moving and all of the offscreen objects are entering/exiting the “viewport” at different times? If so this becomes a bit more complex, but should be solvable. If the “camera” is moving around a world, does the character stay centered in the screen or push the scrolling around as it reaches near the screen edges?

Or, do the offscreen objects move independently of each other, moving about at different speeds and velocities or something? And then the screen “sensor” I described could be locked in place?

Brent [import]uid: 200026 topic_id: 36384 reply_id: 144974[/import]

Hey Brent I do have a camera system where it’s just the group.x & y following player.x & y

very basic,

and literally everything on the playing field right now to a 10000 x 10000 sized app with about 200 people are all moving and all have physics.

The view in this scenario is limited to 1280 x 720 so max, I would dealing with about 5 - 10 people on the current screen and hope to not allow other objects to be requiring physics at this point as it really lags it down. Also as mentioned I need to incoroporate some form of one eat will spawns 2 more people off screen with hopefully no physics attached at this point until they become on screen items because it would truly lag it out exponentially

Thanks :slight_smile: [import]uid: 87730 topic_id: 36384 reply_id: 145130[/import]

Hi @jazzarssoul,

I still think you can implement some kind of physics sensor solution, BUT you must remember that you can’t shift display groups around independently (in regards to your camera) without breaking the physics collision environment. In other words, you can’t just put a “screen size sensor” locked in place (in one display group) and then have the “camera” move the underlying world around. It will not detect the collisions properly.

You might try to attach the sensor to the player, so it follows it around, and gets the proper collisions when other peeps come into contact with it.

Brent [import]uid: 200026 topic_id: 36384 reply_id: 145215[/import]

Hi @jazzarssoul,
You can definitely remove physics bodies when you need to, and you should, especially if your intention is to create 2000 physics objects as in your example. In fact, creating that many would probably crash and burn, or perform terribly slow at least.

Even better would be a method in which you “recycle” similar objects… but the player never sees it happening. Meaning, you find a way to use a very small set of physics objects and swap them in and out of a “repository” as they enter or exit the screen. I did this in a recent game and it gave me the best performance boost of any others I had attempted.

Brent [import]uid: 200026 topic_id: 36384 reply_id: 144493[/import]

Hey Brent my situation may need to be described a little more clearly.

I have a certain amount of xp the character needs to work up to, to level up.
To make up for the fact that the higher the xp required the higher amount of people that get eaten needs to respawn.
So per eat of people spawn either 1/2 and to make up for said numbers I was wondering through the spawning method as mentioned is there a way to add and keep physics to the objects on screen + 1/2 the screen width and remove and keep physics away from the objects outside these bounds [import]uid: 87730 topic_id: 36384 reply_id: 144612[/import]

Hi @jazzarssoul,
Certainly this should be possible. You might also consider a screen “region” which is an invisible sensor object. Objects that move outside that region are either made inactive or the physics body removed. Objects that move inside the bounds are reactivated. Ultimately, the implementation is up to you… but the scenario sounds feasible, absolutely.

Brent [import]uid: 200026 topic_id: 36384 reply_id: 144766[/import]

I had imagined this particular scenario would be the case in which there is an object region that follows the movement of the player and triggering physics as it goes but I however have tried this strategy and failed miserably…

So I kinda ditched the effort and hoped there was an easier way. Is there a way you can provide me with an example or start me in the right direction Brent? [import]uid: 87730 topic_id: 36384 reply_id: 144775[/import]

Have you implemented some kind of “camera” movement? Meaning, the entire world is moving and all of the offscreen objects are entering/exiting the “viewport” at different times? If so this becomes a bit more complex, but should be solvable. If the “camera” is moving around a world, does the character stay centered in the screen or push the scrolling around as it reaches near the screen edges?

Or, do the offscreen objects move independently of each other, moving about at different speeds and velocities or something? And then the screen “sensor” I described could be locked in place?

Brent [import]uid: 200026 topic_id: 36384 reply_id: 144974[/import]

Hey Brent I do have a camera system where it’s just the group.x & y following player.x & y

very basic,

and literally everything on the playing field right now to a 10000 x 10000 sized app with about 200 people are all moving and all have physics.

The view in this scenario is limited to 1280 x 720 so max, I would dealing with about 5 - 10 people on the current screen and hope to not allow other objects to be requiring physics at this point as it really lags it down. Also as mentioned I need to incoroporate some form of one eat will spawns 2 more people off screen with hopefully no physics attached at this point until they become on screen items because it would truly lag it out exponentially

Thanks :slight_smile: [import]uid: 87730 topic_id: 36384 reply_id: 145130[/import]

Hi @jazzarssoul,

I still think you can implement some kind of physics sensor solution, BUT you must remember that you can’t shift display groups around independently (in regards to your camera) without breaking the physics collision environment. In other words, you can’t just put a “screen size sensor” locked in place (in one display group) and then have the “camera” move the underlying world around. It will not detect the collisions properly.

You might try to attach the sensor to the player, so it follows it around, and gets the proper collisions when other peeps come into contact with it.

Brent [import]uid: 200026 topic_id: 36384 reply_id: 145215[/import]

Hi @jazzarssoul,
You can definitely remove physics bodies when you need to, and you should, especially if your intention is to create 2000 physics objects as in your example. In fact, creating that many would probably crash and burn, or perform terribly slow at least.

Even better would be a method in which you “recycle” similar objects… but the player never sees it happening. Meaning, you find a way to use a very small set of physics objects and swap them in and out of a “repository” as they enter or exit the screen. I did this in a recent game and it gave me the best performance boost of any others I had attempted.

Brent [import]uid: 200026 topic_id: 36384 reply_id: 144493[/import]

Hey Brent my situation may need to be described a little more clearly.

I have a certain amount of xp the character needs to work up to, to level up.
To make up for the fact that the higher the xp required the higher amount of people that get eaten needs to respawn.
So per eat of people spawn either 1/2 and to make up for said numbers I was wondering through the spawning method as mentioned is there a way to add and keep physics to the objects on screen + 1/2 the screen width and remove and keep physics away from the objects outside these bounds [import]uid: 87730 topic_id: 36384 reply_id: 144612[/import]

Hi @jazzarssoul,
Certainly this should be possible. You might also consider a screen “region” which is an invisible sensor object. Objects that move outside that region are either made inactive or the physics body removed. Objects that move inside the bounds are reactivated. Ultimately, the implementation is up to you… but the scenario sounds feasible, absolutely.

Brent [import]uid: 200026 topic_id: 36384 reply_id: 144766[/import]

I had imagined this particular scenario would be the case in which there is an object region that follows the movement of the player and triggering physics as it goes but I however have tried this strategy and failed miserably…

So I kinda ditched the effort and hoped there was an easier way. Is there a way you can provide me with an example or start me in the right direction Brent? [import]uid: 87730 topic_id: 36384 reply_id: 144775[/import]

Have you implemented some kind of “camera” movement? Meaning, the entire world is moving and all of the offscreen objects are entering/exiting the “viewport” at different times? If so this becomes a bit more complex, but should be solvable. If the “camera” is moving around a world, does the character stay centered in the screen or push the scrolling around as it reaches near the screen edges?

Or, do the offscreen objects move independently of each other, moving about at different speeds and velocities or something? And then the screen “sensor” I described could be locked in place?

Brent [import]uid: 200026 topic_id: 36384 reply_id: 144974[/import]