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!

shaking or accelerometer? its two different features

Both I guess then? :)

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?

I did it just for fun, but i think this is something what you need, just edit it for your own purposes

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
_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)

Ah awesome, thanks! I had started on something similiar but it didn't work :) 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.

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.

Noone knows how I can remove the collision on my two balls? So they are in different layers so to say?

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

views:1752 update:2011/9/27 8:54:05
corona forums © 2003-2011