Do I need to remove a body that has flown off the screen

I have some physics bodies that purposefully go flying off the visible screen (x and/or y are negative) and are no longer relevant. Do I need to call removeSelf on these objects to clean up the memory related to them? They are only referenced as local variables but I think they might stick around in memory unless I remove them.

I think I need to keep a table that holds them and loop over it to remove any that have negative coordinates. Does that sound right? [import]uid: 27901 topic_id: 6026 reply_id: 306026[/import]

You also can create 4 invisible “static” wall objects around your view so when any of this objects collides with the walls you can remove them. [import]uid: 9975 topic_id: 6026 reply_id: 20664[/import]

Good suggestion. That would work. However, I’ve seen instances where fast traveling bodies can pass through such walls without registering a collision. I think this is the reason for the existence of the isBullet flag, which comes with some warnings about being expensive with respect to performance.

So, I think I’ll stick with my idea about keeping a list of the bodies and using a timer to look at them once per second or so and remove any with the negative coordinates. [import]uid: 27901 topic_id: 6026 reply_id: 20786[/import]

There is a lot of ways to prevent an object go so fast that traverse other physical objects. One that I use is this:

function obj:enterFrame(event)  
 — Apply force of the object here and do what ever you want   
  
  
 — Restrict the max speed of the object  
 local vx, vy = self:getLinearVelocity()  
 local m = math.sqrt((vx\*vx)+(vy\*vy))  
 if (m\>self.maxVelocity) then  
 vx=(vx/m)\*self.maxVelocity  
 vy=(vy/m)\*self.maxVelocity  
 self:setLinearVelocity(vx,vy)  
 end  
end  

You can restrict the object velocity to a certain threshold (or max velocity) [import]uid: 9975 topic_id: 6026 reply_id: 20896[/import]

@xork

If your only concern about using the static walls method is that your object might pass through them undetected then you can just make the walls thicker.

The only way an object can pass through another undetected is if its traveling so fast that it passes through between timesteps. If your walls are thick enough it would be nearly impossible to have something travel so fast that it would be undetected. [import]uid: 10243 topic_id: 6026 reply_id: 20919[/import]