Shake + Physics object?

I was wondering if someone have any example code that involves shaking (the phone) so an object moves with physics? I was thinking of 4 walls that it would bounce against and when you stop shaking the phone, the object slows down and stops moving.

I found an example of a Magic Ball on nettuts, but I’m stuck where to start with physics and shaking an object.

Any help or links appreciated! [import]uid: 35378 topic_id: 14007 reply_id: 314007[/import]

shaking or accelerometer? its two different features [import]uid: 16142 topic_id: 14007 reply_id: 51573[/import]

Both I guess then? :slight_smile:

Think of Dice X when you shake the phone dices rolls, something like that. But “all” I need is 10 different images bouncing around on the screen.

It’s both accelerometer and shake function then? Or only accelerometer? [import]uid: 35378 topic_id: 14007 reply_id: 51577[/import]

I did it just for fun, but i think this is something what you need, just edit it for your own purposes
[lua]_W = display.contentWidth
_H = display.contentHeight
local physics = require(“physics”)
physics.start()

local top = display.newRect(10,0,_W,10)
physics.addBody(top, “static”)
top.name = “top”
local left = display.newRect(0,0,10, _H)
physics.addBody(left, “static”)
left.name = “left”
local bottom = display.newRect(0,_H-10,_W, 10)
physics.addBody(bottom, “static”)
bottom.name = “bottom”
local right = display.newRect(_W-10,0, 10, _H)
physics.addBody(right, “static”)
right.name = “right”
local ball1 = display.newCircle(_W/2+50, _H/2, 30)
ball1.name = “ball1”
local ball2 = display.newCircle(_W/2-50, _H/2, 30)
ball2.name = “ball2”
local function onAccelerate( event )

if event.isShake == true then
physics.addBody(ball1, {friction=0.1, bounce =0.5, radius = 30})
ball1:applyLinearImpulse(10,10, ball1.x, ball1.y)
physics.addBody(ball2, {friction=0.1, bounce =0.5, radius = 30})
ball2:applyLinearImpulse(-10,-10, ball2.x, ball2.y)
end
end

Runtime:addEventListener (“accelerometer”, onAccelerate)

local function onCollision(event)
if event.other.name == “top” or event.other.name == “bottom” or event.other.name == “left” or event.other.name == “right” then
event.target:setFillColor(math.random(1,255),math.random(1,255), math.random(1,255))
end
end

ball1:addEventListener(“collision”, onCollision)
ball2:addEventListener(“collision”, onCollision)[/lua] [import]uid: 16142 topic_id: 14007 reply_id: 51581[/import]

Ah awesome, thanks! I had started on something similiar but it didn’t work :slight_smile: If I want the balls not to have collision on eachother and I don’t want any gravity, the perspective should be from above, just like a regular pool game.

But this is a very nice start! Thanks alot! Will have to read more about physics. [import]uid: 35378 topic_id: 14007 reply_id: 51696[/import]

I removed the gravity, any tips on how I can remove the collision detection between the circles? Also looking wondering if it’s possible to apply a “dynamic” force depending on how hard I shake the phone? And if I continue to shake the phone the circles continue to bounce until I stop shaking it. [import]uid: 35378 topic_id: 14007 reply_id: 51699[/import]

Noone knows how I can remove the collision on my two balls? So they are in different layers so to say? [import]uid: 35378 topic_id: 14007 reply_id: 52048[/import]

Managed to fix the collision detection with help from this excellent forum post:
http://developer.anscamobile.com/forum/2010/10/25/collision-filters-helper-chart

But is it possible in someway to decide which object that should be infront of the other?

EDIT: Found this thread http://developer.anscamobile.com/forum/2010/10/09/initializing-groups-layers [import]uid: 35378 topic_id: 14007 reply_id: 52054[/import]