Physics nooby question

Hi, I’m giving a try to corona and lua, and in general to game programing (coming from IT development). So I’m having hard times trying to understand why my display objects are not getting piled when they collide, but instead they are getting overlapped.

The goal is simply to get the rects piled, as if they were stones falling from sky. I would also like to sometimes rotate the floor (base) 90º, and still get the rects pilled and glued. However sometimes the rects bounce a little and they join in a weird angle, although I set the bounce property to 0.

Thanks in advance.

[code]

display.setStatusBar( display.HiddenStatusBar )

local obj_fisica = require(“physics”);
obj_fisica.start();
obj_fisica.setDrawMode(“hybrid”)

ANCHO = display.contentWidth;
ALTO = display.contentHeight;

base = display.newRect(0, (ALTO-100) , ANCHO, 200)
obj_fisica.addBody(base, “static”, { friction=0.5, bounce=0})

function unir(self)
t_weld = {}
table.insert(t_weld, obj_fisica.newJoint(“weld”, self, base, base.x, base.y))
print(self.nombre)
end

local function onCollision(self, event)

local closure = function() return unir( self ) end
timer.performWithDelay( 10, closure )

end

function nuevaPieza()

local posAleatoria = 100 + math.random(200)
nuevaPieza = display.newRect(base.x -39, 10, base.contentHeight / 2, 10);
nuevaPieza:setReferencePoint(display.TopCenterReferencePoint);
obj_fisica.addBody(nuevaPieza, “dinamyc”,{ density=2, bounce=0 });
nuevaPieza.nombre = “Pieza”
nuevaPieza.collision = onCollision
nuevaPieza:addEventListener(“collision”, nuevaPieza)

end

timer.performWithDelay(2000, nuevaPieza, 10)
[/code] [import]uid: 8933 topic_id: 7198 reply_id: 307198[/import]

it’s “dynamic” not “dinamyc”
[import]uid: 6645 topic_id: 7198 reply_id: 25323[/import]

Thanks for pointing that out, however It didn’t fix all the mess, I’ve re-writed some of the code, because I figured out that maybe joining all the pieces to the base was the problem, so I coded to make a joint only for the pieces that originally collided but, if you rotate the base everything gets messed.

I also changed the base position and shape in order to let you see the problem clearer.

Thanks in advance.
David.

[code]

display.setStatusBar( display.HiddenStatusBar )

local obj_fisica = require(“physics”);
obj_fisica.start();
obj_fisica.setDrawMode(“hybrid”)

ANCHO = display.contentWidth;
ALTO = display.contentHeight;
ANGULO = 90;
numPieza = 0;

base = display.newRect((ANCHO / 2) - 32, (ALTO / 2) - 32, 75, 75);
base.nombre = “Base”;
base.tipo = “base”;
base.id = 0
obj_fisica.addBody(base, “static”, { friction=0.5, bounce=0});

function unir(self)
t_weld = {}
table.insert(t_weld, obj_fisica.newJoint(“weld”, self, self.unidoCon, self.unidoCon.x, self.unidoCon.y))
print(“joint->”…self.nombre…" with “… self.unidoCon.nombre)
print(”--------")
end
local function onCollision(self, event)
if ( event.phase == “began”) then
self.unidoCon = event.other
if (self.id > self.unidoCon.id) then
print(self.nombre …“will join with “…self.unidoCon.nombre)
print(”--------”)
local closure = function() return unir( self ) end
timer.performWithDelay( 10, closure )
end
end
end

function nuevaPieza()

numPieza = numPieza +1

local posAleatoria = 100 + math.random(200)
nuevaPieza = display.newRect(base.x -39, 10, base.contentHeight / 2, 10);
nuevaPieza:setReferencePoint(display.TopCenterReferencePoint);
obj_fisica.addBody(nuevaPieza, “dynamic”,{ density=2, bounce=0 });
nuevaPieza.nombre = “Pieza”…numPieza
nuevaPieza.tipo = “pieza”
nuevaPieza.id = numPieza
nuevaPieza.unidoCon = nil
nuevaPieza.collision = onCollision
nuevaPieza:addEventListener(“collision”, nuevaPieza)

end

timer.performWithDelay(2000, nuevaPieza, 10)
[/code] [import]uid: 8933 topic_id: 7198 reply_id: 25386[/import]

El problema esta en onCollision. – Fijate bien en el orden numerico de como las piezas caen.

Carlos [import]uid: 24 topic_id: 7198 reply_id: 25428[/import]

Antes de nada muchas gracias por contestar!, what I’ve seen so far is that at some point when the 3rd or 4th piece falls on to the prior one, it also collides with another one and so the onCollision event is triggered again.

I also noted that when the second piece falls onto the first one, it seems that it does not fall on top of it but it overlaps the previous a bit

Pieza1 will join with Base

Pieza2 will join with Pieza1

Pieza3 will join with Pieza2

Pieza4 will join with Pieza3

Pieza5 will join with Pieza4

Pieza5 will join with Pieza3

Pieza6 will join with Pieza5 --------
Pieza6 will join with Pieza4

Pieza7 will join with Pieza6

Pieza7 will join with Pieza5

Pieza7 will join with Pieza4

Pieza7 will join with Pieza5

Pieza8 will join with Pieza7

Pieza8 will join with Pieza6

Pieza9 will join with Pieza8

Pieza10 will join with Pieza9

[import]uid: 8933 topic_id: 7198 reply_id: 25480[/import]

Any helping hand out there :slight_smile: [import]uid: 8933 topic_id: 7198 reply_id: 25669[/import]