Fixed circle but constantly rotating?

Hey there,

I’ve been messing with this basic idea for a while now and can’t seem to get it right. Must be missing something.

All I want to do is have a circle (image) be fixed in the middle of the screen, but constantly be rotating one way. I want it so that if something hits it, it doesn’t move but could hinder it’s rotating or even make it rotate the other way.

Eventually, I actually want to make it so that it uses the accelerometer to rotate it more right if tilted right, or rotate it left if tilted left.

Hope this is clear. And all help so greatly appreciated.

Thanks!
Mike [import]uid: 42777 topic_id: 7473 reply_id: 307473[/import]

Any help with this?

Just a circle in the middle of the screen that has the ability to rotate left and right… (rather than static or falling down) [import]uid: 42777 topic_id: 7473 reply_id: 26580[/import]

Tried just applying force?

physics.addBody(object, "dynamic", {})  
object.isFixedRotation = true  
object:applyForce(0, 0, 10, 0)  

Maybe change from dynamic to static or kinematic [import]uid: 14018 topic_id: 7473 reply_id: 26614[/import]

Thanks NahirC,

OK so what I want to be able to do is have the ball not rotating at the beginning but: the ball rotation increases to the right when the right side of the screen is tapped, whilst the ball rotation increases to the left when the left side of the screen is tapped.

Any advice?

Thanks so much,
M [import]uid: 42777 topic_id: 7473 reply_id: 26924[/import]

A lazy way of doing it would be having 2 invisible rectangles covering each side of the screen. One the left part, one the right part. Assign each rect’s a listener and on each tap a variable increases. This variable should be used to apply force to the ball within an enterFrame-listener.

First create 2 touch listeners for each rectangle that either increase or decrease “rectVariable” with 1. Then have a similar function as the one below

local function increaseBallForce()  
  
 local variable = rectVariable  
  
 ball:applyForce(0, 0, variable, 0)  
end  
ball.enterFrame = increaseBallForce  
Runtime:addEventListener("enterFrame", ball)  

If rectVariable reaches a negative number the ball will spin to the left, a positive number it’ll spin to the right.
I’m not sure if this code will work though, I’m pretty tired right now :stuck_out_tongue: [import]uid: 14018 topic_id: 7473 reply_id: 26938[/import]

Thanks for this help NahirC!

So I’m slightly adapting this idea, (just moving the force in a different place):

So, higher up my code I create ‘bigball’ and now at the bottom of my code I’ve tried your instructions like so:

local function incLeft ()  
 rectVariable == rectVariable + 1  
end  
  
-- add a touch listener to the bigball  
bigball:addEventListener( "touch", incLeft )  
  
local function increaseBallForce ()  
 local variable = rectVariable  
 bigball:applyForce(variable, 0, 0, 0)  
end  
bigball.enterFrame = increaseBallForce  
Runtime:addEventListener("enterFrame", bigball)  
  

I’m messing up somewhere though as this no work… any ideas?

Thanks for your help! [import]uid: 42777 topic_id: 7473 reply_id: 27222[/import]

Here’s some basic rotation (for post#3) not using physics, this could be added later for collisions etc

[code]
– load the image
local image = display.newImage(“CoronaBadge_150x144.png”, 0, 0)

– center the image
image.x = display.contentCenterX
image.y = display.contentCenterY

– initial rotation direction 0 = none, positive = clockwise, negative = anti-clockwise
local direction = 0

– touch
local function touchDown(event)

if event.phase == “began” or event.phase == “moved” then

– touch on left of screen
if event.x < display.contentCenterX then

direction = direction - 1 – increase anti-clockwise

– touch on right of screen
else

direction = direction + 1 – increase clockwise

end

end

end

– enter frame loop
local function loop(event)

– set image rotation
image.rotation = image.rotation + direction

end

– start listeners
Runtime:addEventListener(“touch”, touchDown)
Runtime:addEventListener(“enterFrame”, loop)
[/code] [import]uid: 8366 topic_id: 7473 reply_id: 27285[/import]

Just an idea, but what if you did:

1.Have a static object off screen to act as an anchor
2.Place your dynamic circular object in the centre of the screen
3.Use a pivot joint between the two, with the world coordinate of the centre of the screen
4.Set the pivot joint motor to turn the wheel
5.Create an accelerometer listener to respond to tilt events
6.In the listener use the accelerometer sample to de/increase the speed of the pivot joint motor

If that’s not good, you could replace steps 4-6 with:

4.Create a display group centred at the centre of your physics circle
5.Create an invisible circle somewhere in the display group, near the edge of the physics circle
6.Create a touch joint on the physics circle at the location of the circle in the group
7.Create a timer to rotate the display group
8.In the timer use the world coordinates of the circle in the display group, use the touch joint to rotate the physics circle (play with the settings of the joint to get more/less precise turning)
9.Use the accelerometer, as before, to control the amount of rotation applied to the display group each time the first timer fires

Let me know what you think…

m [import]uid: 8271 topic_id: 7473 reply_id: 27292[/import]

You are assigning bigball a touch listener instead of assigning each rect one. I assume you’ve created 2 rect’s covering each side of the screen.

If you want the left rect to decrease rectVariable, assign the left rectangle a touch listener decreasing rectVariable, and the opposit for the right rectangle. Once you’ve done that, everytime the user taps the left/right rectangle, rectVariable should either increase or decrease. The other function, increaseBallForce, looks fine to me.

Try this and see if it works [import]uid: 14018 topic_id: 7473 reply_id: 27296[/import]

@NahirC, Does the code to increase and decrease rectVariable look to work ok? something like this:

local function increment ()  
 rectVariable == rectVariable + 1  
end  
  
local function decrement ()  
 rectVariable == rectVariable - 1  
end  
  
-- add a touch listener to the left rect  
leftRect( "touch", increment )  
  
-- add a touch listener to the right rect  
rightRect( "touch", decrement )  
  

@evs, Thanks for this idea! I’ll mess with it.

@horacebury, this is interesting. I guess I could also use the accelerometer to flip the direction of the pivot joint motor somehow, as well as de/increase the speed…this may work, if I can make it feel not too mechanical in the end [import]uid: 42777 topic_id: 7473 reply_id: 27302[/import]

Should work, except you just forgot to add the listener

[code]
leftRect:addEventListener(“touch”, increment)

rightRect:addEventListener(“touch”, decrement)
[/code] [import]uid: 14018 topic_id: 7473 reply_id: 27364[/import]

From reading the comments above, I’m not sure if this is what you’re looking for, but your original comment about the accelerometer is dealt with here:

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

local anchor = display.newCircle( -100, -100, 5 )
physics.addBody( anchor, “static” )

local dial = display.newGroup()
dial.x, dial.y = display.contentCenterX, display.contentCenterY

dial.wheel = display.newCircle( dial, 0, 0, 75 )
dial.dot = display.newCircle( dial, 0, -60, 5 )
dial.dot:setFillColor( 255,0,0 )

physics.addBody( dial, “dynamic”, {radius=75} )

local pivot = physics.newJoint( “pivot”, anchor, dial, dial.x, dial.y )

pivot.isMotorEnabled = true
pivot.motorSpeed = 100
pivot.maxMotorTorque = 1000000

function accelerometer( event )
pivot.motorSpeed = 100 + event.xGravity * display.contentCenterX * 1.2
end

Runtime:addEventListener( “accelerometer”, accelerometer )[/lua]

m [import]uid: 8271 topic_id: 7473 reply_id: 27373[/import]