Applying setLinearVelocity(vx, vy) to a display group

Hi
Is it possible to apply setLinearVelocity(x, y) to a display group? I am trying to achieve that but with no luck. Here is my code:
[lua]local physics = require(“physics”)
physics.start()
physics.setGravity (0, 4)

local group = display.newGroup()
local deck = display.newRoundedRect(100, 200, 200, 18, 10)
deck:setReferencePoint(display.TopLeftReferencePoint);
deck.strokeWidth = 0
deck:setFillColor(140, 140, 140)
deck:setStrokeColor(180, 180, 180)
physics.addBody(deck, "kinematic ", { density = 1.0, friction = 1, bounce = 0 } )

local deck1 = display.newRoundedRect(100, 300, 200, 18, 10)
deck1:setReferencePoint(display.TopLeftReferencePoint);
deck1.strokeWidth = 0
deck1:setFillColor(140, 140, 140)
deck1:setStrokeColor(180, 180, 180)
physics.addBody(deck1, "kinematic ", { density = 1.0, friction = 1, bounce = 0 } )

group:insert(deck)
group:insert(deck1)

group:setLinearVelocity(-500, 0) [/lua] [import]uid: 167063 topic_id: 29714 reply_id: 329714[/import]

Not really like that, no. Corona has done a great job of making the displayObject and the physicBody seem almost as if they are one entity, but really what you have is your displayobject which is on the screen and the physicsBody which is what the Box2d physics engine uses for calculations. That being the case a display group has no physics body(and Box2d doesn’t really have ‘groups’ like that per say).

There are a few ways to achieve what you want, but I guess the way that is closest to your root question is just to loop through your display group and set the velocity on each object.

[lua]local i = 0
while i <group.numchildren do>group[i]:setLinearVelocity(-500, 0)
i = i +1
end [/lua]

[import]uid: 147305 topic_id: 29714 reply_id: 119247[/import] </group.numchildren>

@budershank, Isn’t it should be:

[lua]local i = 1
while i <=group.numChildren do
group[i]:setLinearVelocity(-500, 0)
i = i +1
end [/lua]

?
@info5491, btw if u want to behave ur display group and its children same, then u can add ur display group as physics body and apply force to the group, like this:
[lua]local physics = require(“physics”)
physics.start()
physics.setGravity (0, 4)

local group = display.newGroup()
local deck = display.newRoundedRect(100, 200, 200, 18, 10)
deck:setReferencePoint(display.TopLeftReferencePoint);
deck.strokeWidth = 0
deck:setFillColor(140, 140, 140)
deck:setStrokeColor(180, 180, 180)
– physics.addBody(deck, "kinematic ", { density = 1.0, friction = 1, bounce = 0 } )
– dont add this,
local deck1 = display.newRoundedRect(100, 300, 200, 18, 10)
deck1:setReferencePoint(display.TopLeftReferencePoint);
deck1.strokeWidth = 0
deck1:setFillColor(140, 140, 140)
deck1:setStrokeColor(180, 180, 180)
– physics.addBody(deck1, "kinematic ", { density = 1.0, friction = 1, bounce = 0 } )

group:insert(deck)
group:insert(deck1)

physics.addBody(group, "kinematic ", { density = 1.0, friction = 1, bounce = 0 } )

group:setLinearVelocity(-500, 0) [/lua]

This is what you supposed to want?

[import]uid: 147582 topic_id: 29714 reply_id: 119249[/import]

@neostar - err… yea. Forgetting the whole non-zero based index thing is a common mistake of mine :slight_smile: [import]uid: 147305 topic_id: 29714 reply_id: 119252[/import]

@info5491, yeah.

It becomes interesting when also deal with languages which have indexing from 0, like PHP. :slight_smile: [import]uid: 147582 topic_id: 29714 reply_id: 119253[/import]

Thank you very much for your input guys.

@budershank
I sort of sensed that when I started dealing with this issue. I am already applying the loop solution but was hoping to find an easier way. Thanks for the clarification.

@neostar20
I want each rectangle to have its own physical body, and add the whole group as a physical body does not allow me to do so.

Thanks again [import]uid: 167063 topic_id: 29714 reply_id: 119301[/import]