get position of physics body

Hi all,

I just started with corona and i’m experimenting with the bridge example. I reduced the example to 1 ball and changed the graphic to a wheel. Now i want another graphic stick to that wheel bij setting the x,y to the position of the wheel.

So I tried to d do something like this, but this isn’t working.

Can someone help me with this and tell me why this isn’t working pls

-- function to drop random coconuts and rocks  
local randomBall = function()  
  
 choice = math.random( 100 )  
 local ball  
  
 ball = display.newImage( "coconut.png" )  
 ball.x = 40 + math.random( 380 ); ball.y = -40  
 physics.addBody( ball, { density=1, friction=1, bounce=0.6, radius=104 } )  
 -- ball.angularVelocity = math.random(800) - 400  
 ball.isSleepingAllowed = false  
 balls[#balls + 1] = ball   
  
end  
  
local body = display.newImage( "body.png" )  
body.x = ball.x  
body.y = ball.y  

Thanks! [import]uid: 55005 topic_id: 15805 reply_id: 315805[/import]

Your x and y settings don’t seem to be referencing the wheel - they’re just ball.y is -40 and ball.x is 40 +math.random (380).

You’d need to use something like;
[lua]ball.x = wheel.x
ball.y = wheel.y[/lua]

That would spawn it ontop of the wheel - however if you want it to stay connected you would join it using a joint (physics).

Peach :slight_smile: [import]uid: 52491 topic_id: 15805 reply_id: 58570[/import]

Thanks for pointing me in to the right direction. I managed to connect them both with a Joint
Something like this.

 connectThem = physics.newJoint ( "pivot" , obj1, obj2, obj1.x, obj1.y )   

Cheers! [import]uid: 55005 topic_id: 15805 reply_id: 58745[/import]