OOP and physic.

Hi,

As I’ve read on the blog, it is a bad practice to have physic objects from display objects that are in different groups.

So how I can make my classes and encapsulate functionality while they all share one display group? Best option seems to be filters but it’s not enough. [import]uid: 206803 topic_id: 35382 reply_id: 335382[/import]

For example I have two big objects on sides of my screen that react to different objects they collide with. I made a class for them and inside it I handle the collision of them. [import]uid: 206803 topic_id: 35382 reply_id: 140604[/import]

syntax error - line 8 :wink: [import]uid: 62706 topic_id: 35382 reply_id: 140616[/import]

Each object is a table, even display objects. Assign properties to them to identify them:

[lua]function newPerson(name)
local group = display.newGroup()
group.class = “person”
group.name = name
physics.addBody(group)
return group
end[/lua]

This would create a group which is unique, can be placed within other groups and can contain unique content.

The trick with display groups as physics objects is simply to not move their containing group. For example, if the code above added the physics body to an object with ‘group’ and then moved ‘group’, the physics body would get out of sync with the ‘world’.

Its a pain, but you need to treat ALL physics bodies as being relative to the 0,0 of the world, even if they are in their own group. And that means not moving their parent groups at all. [import]uid: 8271 topic_id: 35382 reply_id: 140607[/import]

For example I have two big objects on sides of my screen that react to different objects they collide with. I made a class for them and inside it I handle the collision of them. [import]uid: 206803 topic_id: 35382 reply_id: 140604[/import]

syntax error - line 8 :wink: [import]uid: 62706 topic_id: 35382 reply_id: 140616[/import]

Each object is a table, even display objects. Assign properties to them to identify them:

[lua]function newPerson(name)
local group = display.newGroup()
group.class = “person”
group.name = name
physics.addBody(group)
return group
end[/lua]

This would create a group which is unique, can be placed within other groups and can contain unique content.

The trick with display groups as physics objects is simply to not move their containing group. For example, if the code above added the physics body to an object with ‘group’ and then moved ‘group’, the physics body would get out of sync with the ‘world’.

Its a pain, but you need to treat ALL physics bodies as being relative to the 0,0 of the world, even if they are in their own group. And that means not moving their parent groups at all. [import]uid: 8271 topic_id: 35382 reply_id: 140607[/import]

@horacebury,

Thanks but how would you suggest we treat a table/object created by that function you wrote? [import]uid: 206803 topic_id: 35382 reply_id: 141405[/import]

What do you mean by ‘treat’? It’s a display object so it’s pretty flexible. [import]uid: 8271 topic_id: 35382 reply_id: 141483[/import]

@horacebury,

Thanks but how would you suggest we treat a table/object created by that function you wrote? [import]uid: 206803 topic_id: 35382 reply_id: 141405[/import]

What do you mean by ‘treat’? It’s a display object so it’s pretty flexible. [import]uid: 8271 topic_id: 35382 reply_id: 141483[/import]