Simple Bobble head effect ?

Wondering if anyone could point me in the right direction on this. I an wondering where I would start to create a simple spring physics example.

What I would like to end up with is something like a bobble head. So I have an image and user shakes the phone and the image on screen bobbles from side to side but remains in the static position that it is in, so in other words I think you would somehow effect the rotation and pivote off of the images bottom center x and y?

thoughts ? suggestions ?

thanks,
Matthew [import]uid: 7501 topic_id: 1421 reply_id: 301421[/import]

One simple way might be to turn on the physics and set a pivot joint near the bottom of the head, joining it to the body. Then, set a limited range of motion on the joint, like (-10, 10) degrees – this should work in Alpha 2.

Finally, create an event listener for the “accelerometer” event, and use the event.xInstant and event.yInstant values to change the gravity vector. See the “ShapeTumbler” sample code for a similar case, except that in your case you want the “instant” values to capture the fast shaking, rather than the slower values you get from tilting the phone.

This might be TOO simple, since it will shake back and forth but may not be as “springy” as a bobblehead, but it’s a start… [import]uid: 3007 topic_id: 1421 reply_id: 4035[/import]

That sounds like a plan … will see what I can do.

thanks,
-m [import]uid: 7501 topic_id: 1421 reply_id: 4039[/import]

Did you have any success with this? Would love to see the effective code!

matt [import]uid: 8271 topic_id: 1421 reply_id: 5988[/import]

any luck on the bobble head affect ? im trying to simulate the way a paramecium moves on the cellular level and this is be perfect for that. [import]uid: 11094 topic_id: 1421 reply_id: 18058[/import]

To get the spring effect (something which we should probably add to the list of demos, in the future) you could use a timer and a touch joint. Every 100 milliseconds or so simply have the touch joint, connected to the centre of the head element, set the touch position back to the centre/starting position (I’m assuming the bobble head will want to default to upright) to pull it back to upright.
m [import]uid: 8271 topic_id: 1421 reply_id: 18063[/import]

So the ‘spring object’ would bounce based on touch or accelerometer movement? Would really like a dead simple example / demo of how this would work. Much appreciated.

thanks,
B. [import]uid: 32101 topic_id: 1421 reply_id: 20177[/import]

I’m still not sure on this one, tbh. Will try for a demo soon.
M [import]uid: 8271 topic_id: 1421 reply_id: 20317[/import]

has anyone got an example of the code needed for the springing?
i am trying to understand how you work it out but am having no joy,
ive managed to make the head on mine move based on the acclerometer, but cannot get it to spring back

please add to this code if possible

local piratebod = display.newImage (“body.png”, 90,290)
–Head
local head = display.newImage(“head.png”, 140,44)
head:setReferencePoint(display.CenterReferencePoint)
head.x = display.contentWidth * 0.6
head.y = display.contentWidth * 0.3 + 140

–skullbutton
local skull = display.newImage (“button.png”, 10,640)

– Listen for Accelerometer Events

local acc = {}
local centerX = display.contentWidth * 0.5

function acc:accelerometer(e)
head.x = centerX + (centerX * e.xGravity)
end

Runtime:addEventListener(“accelerometer”, acc)

local max = math.max
local min = math.min
local centerX = display.contentWidth/2

local function rotate(event)

local r = event.x - centerX – (shift values to between eg -240 to 240)

r = min(r, 30) – no higher than 30
r = max(r, -30) – no lower than -30

head.rotation = r

end

background:addEventListener( “touch”,rotate) [import]uid: 45019 topic_id: 1421 reply_id: 28372[/import]

Short distance joint. Play with the settings. [import]uid: 8271 topic_id: 1421 reply_id: 28378[/import]

how would i implement it in?
i cannot find a tutorial or example code to look at. [import]uid: 45019 topic_id: 1421 reply_id: 28384[/import]

this is what ive come up with so far
http://www.ectomia.co.uk/Androiddevelopment/PirateSI.apk

source code as follows:
– Pirate Si v1
– Developed by Simon James 2011

– Hide Status Bar
display.setStatusBar(display.HiddenStatusBar)
– Background + Objects
local background = display.newImage(“background.png”)
local piratebod = display.newImage (“body.png”, 90,290)
local parrot = display.newImage(“parrot.png”, 70,150)
–skullbutton
local skull = display.newImage (“button.png”, 10,600)
– Head
local indicator = display.newImage(“head.png”)
indicator:setReferencePoint(display.CenterReferencePoint)
indicator.x = display.contentWidth * 0.63
indicator.y = display.contentWidth * .30 + 100
– Listen for Accelerometer Events
local acc = {}
local centerX = display.contentWidth * 0.63
function acc:accelerometer(e)
indicator.x = centerX + (centerX * e.xGravity)
indicator.rotationx = centerX +(centerX * e.xGravity)
end
Runtime:addEventListener(“accelerometer”, acc)
–Headrotate on touch
local max = math.max
local min = math.min
local function rotate(event)
local r = event.x - centerX – (shift values to between eg -240 to 240)
r = min(r, 30) – no higher than 30
r = max(r, -30) – no lower than -30
indicator.rotation = r
end
indicator:addEventListener( “touch”,rotate)
–Kills parrot from skullbutton
local ouch = media.newEventSound (“parrot.mp3”)
local function playouch (event)
media.playEventSound (ouch)
end
skull:addEventListener (“touch”, playouch)
–kill parrot command
local function hidep (event)
parrot.isVisible = false
end
skull:addEventListener (“touch”, hidep)
local function killpar (event)
parrot.isVisible = false
end
–Respawn parrot
local reset = display.newImage (“reset.png”)
reset.x =430
reset.y = 650
local function resetparrot (event)
parrot.isVisible = true
end
reset:addEventListener (“touch”, resetparrot)
[import]uid: 45019 topic_id: 1421 reply_id: 28385[/import]

http://developer.anscamobile.com/content/game-edition-physics-joints#Distance_joint [import]uid: 8271 topic_id: 1421 reply_id: 28389[/import]

could you possibly post an example of the kind of setup i require. I cannot work out the joint settings and physics

body ----- head link

is it possible to set gravity to 0 and the head only move on the acclerometer? or would this be completely different? [import]uid: 45019 topic_id: 1421 reply_id: 28395[/import]

local physics = require( “physics” )
physics.start()
physics.setGravity(0, 2 )
physics.setDrawMode( “hybrid” ) – normal, hybrid, debug

local anchor = display.newImage( “body.png”,100,400 )
physics.addBody( anchor, “static”, { friction=.1, bounce=.1, density=.1,} )

local platform = display.newImage( “head.png”,140,100)
physics.addBody( platform, “dynamic”, { friction=.1, bounce=.1, density=.1 } )
platform.isFixedRotation = true

local joint = physics.newJoint( “distance”, anchor, platform)

this appears not to work [import]uid: 45019 topic_id: 1421 reply_id: 28396[/import]

When you it doe not work do you mean it doesn’t run or doesn’t give the desired result?

Take a look at the accelerometer sample which comes with the sdk. There you’ll find very sime code to handle device motion. From that you can play about with either changing the physics world gravity or just affecting force upon one or more objects directly.

Read the docs on the site then, if you need specific arts, the API.

I’ve done what you’re trying to do in my currentame and it really is as easy as adding 4 lines to handle the accelerometer to your last post. I’ll try to post some code later, but you’ll learn more taking a good lookat the sdk samples.

M [import]uid: 8271 topic_id: 1421 reply_id: 28420[/import]

the result does not come out like id hoped.
Ive read through the coding for the accelerometer example but cannot see how i can link it to a short distance joint.

is there anyone can post a sample code for a bobble head that exists so i can read it and work out from that. [import]uid: 45019 topic_id: 1421 reply_id: 30655[/import]