moving a group but a collision still takes place in the original location

I am drawing shapes out of small brick sized particles, grouping then and adding a ‘move’ event handler for the group object. Works fine in that I can move the object around the drawing surface.

However if another object flies through a path that includes the original position of the group object, then my group object receives the collision in its new location. Interesting behaviour but in this case I was expecting to move the group out of ‘harms way’.

The reverse is true. If I created the object outside of the path and move it in, then no collision takes place. The colliding object appears to flies in ‘a layer’ over the group and no impact takes place.

Here’s the block that draws the group object:
[blockcode]
local function drawAsset(brickx,bricky,startx,starty,r,g,b)
local asset = display.newGroup()
for i=1,10 do
for j=1,10 do
local obj = display.newRect( startx+(j*(brickx)+j), starty+(i*(bricky)+i) , brickx, bricky )
obj:setFillColor(r,g,b)
physics.addBody(obj, “dynamic”, {density = 1.0, friction = 0.3, bounce = 0.2})
asset:insert(obj)
end
end
return asset
end
[/blockcode]

As you see i’m wiring the physics in at the ‘brick’ level allowing the group object to ‘explode’ when in harms way.

[blockcode]
asset = drawAsset(4,4,320,200,202,131,70)
asset:addEventListener( “touch”, makeOnTouchShapeEvent() )
[/blockcode]

Here’s the block that implements the event handler
[blockcode]
local function makeOnTouchShapeEvent()
return function ( event )
local t = event.target
local phase = event.phase
if “began” == phase then
display.getCurrentStage():setFocus( t )

– Spurious events can be sent to the target, e.g. the user presses
– elsewhere on the screen and then moves the finger over the target.
– To prevent this, we add this flag. Only when it’s true will “move”
– events be sent to the target.
t.isFocus = true

– Store initial position
t.x0 = event.xStart - t.x
t.y0 = event.yStart - t.y
elseif t.isFocus then
if “moved” == phase then
– Make object move (we subtract t.x0,t.y0 so that moves are
– relative to initial grab point, rather than object “snapping”).
t.x = event.x - t.x0
t.y = event.y - t.y0
elseif “ended” == phase or “cancelled” == phase then
display.getCurrentStage():setFocus( nil )
t.isFocus = false
end
end

return true
end
end
[/blockcode]

Clearly I miss and understanding of what’s happening.

Help please. [import]uid: 114225 topic_id: 25948 reply_id: 325948[/import]

Hi, just check the ‘Note’ section on this page :slight_smile:
http://developer.anscamobile.com/reference/index/physicsaddbody [import]uid: 74676 topic_id: 25948 reply_id: 105007[/import]

thanks for that Jadynut - it explains what i’m seeing but there appears to be no solution, or am I just approaching the problem in a less than suitable way - I simply want to create shapes out of smaller particles and be able to drag them out of harms way, but should my drags not be sufficient, then for those shapes to explode [import]uid: 114225 topic_id: 25948 reply_id: 105014[/import]

I hope ive understood you problem here. The physics world location of a object is determined by its X and Y position. Physics objects which are in a group will have that position determined by the X and Y position relative to that group and not the parent group or stage. It is fine to put objects into a group of but you cannot move the group in order to move the objects because all you will be doing is visually moving them and not moving their physics object. To move objects as a group you will need to join them or cycle through them in a loop. [import]uid: 8271 topic_id: 25948 reply_id: 105041[/import]

Hi Horace,

I think you understand the issue and what you say makes sense. I’ve fiddled and explored on and off over the weekend.

I have ‘cycled through the child objects’ and adjusted the child x,y positions by the amount the group ended up moving after i stop dragging the group object, but adjusting each child’s x,y coord causes an extra ‘kick’ in movement - for sure it takes the objects out of predator flight paths but there is something I’m missing here.

What did you mean I should do to each child object as I ‘cycle through them’?

Thanks

Gary [import]uid: 114225 topic_id: 25948 reply_id: 105310[/import]

I meant that you either move each in a loop or you join them together with weld joints and move one of them.

Directly moving dynamic objects will eventually cause problems because you’re fighting the physics engine’s calculation of where they should be relative to the environment’s forces upon them. See Newton’s laws.

You should use applyForce to move them.
[import]uid: 8271 topic_id: 25948 reply_id: 105381[/import]