How to change the direction of rotation?

Hi,

I have a display object with physic body, a circle.

The circle will rotate in a clockwise direction as the user touch and drag in a  clockwise direction.

How to make that circle rotate in counterclockwise direction when the user change the drag dicection without lifting his finger?

Somebody helps! Thank you.

Soo Wee Chai

You could do something like this

 

--object is your circle function object:touch(event) if event.phase == "began" then display.getCurrentStage():setFocus(self); self.currPos = self.x; elseif event.phase == "moved" then self.x, self.y = event.x, event.y; if self.x \> self.currPos then self.angularVelocity = -50; elseif self.x \< self.currPos then self.angularVelocity = 50; end self.currPos = self.x; elseif event.phase == "ended" then display.getCurrentStage():setFocus(nil); end end

Basically you store the previous position X of the object (if we want for it to change rotation when the x changes), and then compare it to the current position to change the angularVelocity of the object to the desired direction.

Mr hachisoft,

Sorry. I try the code you post, it seems won’t work or it is not working as I expect.

I need the circle to remain “pinned” at the center of screen, just rotate while the user move his finger on it, something like a roulette.

My main.lua file:

local physics = require(“physics”)

physics.start()

display.setStatusBar( display.HiddenStatusBar )

_W = display.viewableContentWidth;

_H = display.viewableContentHeight;

local object = display.newImage( “images/object.png”  )

physics.addBody( object, “kinematic”, {friction = 0.2, bounce = 0.3} )

object.x = _W * 0.5

object.y = _H * 0.5

function object:touch(event)

  if event.phase == “began” then

    display.getCurrentStage():setFocus(self);

    self.currPos = self.x;

  elseif event.phase == “moved” then

    self.x, self.y = event.x, event.y;

    if self.x > self.currPos then

      self.angularVelocity = -50;

    elseif self.x < self.currPos then

      self.angularVelocity = 50;

    end

    self.currPos = self.x;

  elseif event.phase == “ended” then

    display.getCurrentStage():setFocus(nil);

  end

end

object:addEventListener( “touch”, object )

Help please, thank you

Soo Wee Chai

Aah, so you’d like to be able to rotate the circle directly with your finger?

One way to do that is using trig, but since you use physics for it, this should all be achievable through joints.

local physics = require "physics"; physics.start(); physics.setGravity(0, 0); local circle = display.newCircle(display.contentCenterX, display.contentCenterY, 50); circle.fill = {type = "gradient", color1 = {1, 0, 0, 1}, color2 = {0, 1, 0, 1}, direction = "down"}; local pin = display.newCircle(display.contentCenterX, display.contentCenterY, 5); pin:setFillColor(1, 0, 0); physics.addBody(circle, "dynamic", {friction = 0, bounce = 0, density = 1, radius = 25}); circle.angularDamping = 1; physics.addBody(pin, "static", {friction = 0, bounce = 0, density = 1, radius = 2.5, isSensor = true}); physics.newJoint("pivot", pin, circle, pin.x, pin.y); function circle:touch(event) if event.phase == "began" then display.getCurrentStage():setFocus(self); self.touchJoint = physics.newJoint("touch", circle, event.x, event.y); self.isFocus = true; elseif event.phase == "moved" and self.isFocus then self.touchJoint:setTarget(event.x, event.y); elseif event.phase == "ended" and self.isFocus then display.getCurrentStage():setFocus(nil); self.touchJoint:removeSelf(); self.touchJoint = nil; self.isFocus = nil; end end circle:addEventListener("touch", circle);

Just drag and drop the code in a new project, it should all work out of the box.

Mr hachisoft,

It’s working now !!

That means a lot me.

Thanks for you help.

Soo Wee Chai

You could do something like this

 

--object is your circle function object:touch(event) if event.phase == "began" then display.getCurrentStage():setFocus(self); self.currPos = self.x; elseif event.phase == "moved" then self.x, self.y = event.x, event.y; if self.x \> self.currPos then self.angularVelocity = -50; elseif self.x \< self.currPos then self.angularVelocity = 50; end self.currPos = self.x; elseif event.phase == "ended" then display.getCurrentStage():setFocus(nil); end end

Basically you store the previous position X of the object (if we want for it to change rotation when the x changes), and then compare it to the current position to change the angularVelocity of the object to the desired direction.

Mr hachisoft,

Sorry. I try the code you post, it seems won’t work or it is not working as I expect.

I need the circle to remain “pinned” at the center of screen, just rotate while the user move his finger on it, something like a roulette.

My main.lua file:

local physics = require(“physics”)

physics.start()

display.setStatusBar( display.HiddenStatusBar )

_W = display.viewableContentWidth;

_H = display.viewableContentHeight;

local object = display.newImage( “images/object.png”  )

physics.addBody( object, “kinematic”, {friction = 0.2, bounce = 0.3} )

object.x = _W * 0.5

object.y = _H * 0.5

function object:touch(event)

  if event.phase == “began” then

    display.getCurrentStage():setFocus(self);

    self.currPos = self.x;

  elseif event.phase == “moved” then

    self.x, self.y = event.x, event.y;

    if self.x > self.currPos then

      self.angularVelocity = -50;

    elseif self.x < self.currPos then

      self.angularVelocity = 50;

    end

    self.currPos = self.x;

  elseif event.phase == “ended” then

    display.getCurrentStage():setFocus(nil);

  end

end

object:addEventListener( “touch”, object )

Help please, thank you

Soo Wee Chai

Aah, so you’d like to be able to rotate the circle directly with your finger?

One way to do that is using trig, but since you use physics for it, this should all be achievable through joints.

local physics = require "physics"; physics.start(); physics.setGravity(0, 0); local circle = display.newCircle(display.contentCenterX, display.contentCenterY, 50); circle.fill = {type = "gradient", color1 = {1, 0, 0, 1}, color2 = {0, 1, 0, 1}, direction = "down"}; local pin = display.newCircle(display.contentCenterX, display.contentCenterY, 5); pin:setFillColor(1, 0, 0); physics.addBody(circle, "dynamic", {friction = 0, bounce = 0, density = 1, radius = 25}); circle.angularDamping = 1; physics.addBody(pin, "static", {friction = 0, bounce = 0, density = 1, radius = 2.5, isSensor = true}); physics.newJoint("pivot", pin, circle, pin.x, pin.y); function circle:touch(event) if event.phase == "began" then display.getCurrentStage():setFocus(self); self.touchJoint = physics.newJoint("touch", circle, event.x, event.y); self.isFocus = true; elseif event.phase == "moved" and self.isFocus then self.touchJoint:setTarget(event.x, event.y); elseif event.phase == "ended" and self.isFocus then display.getCurrentStage():setFocus(nil); self.touchJoint:removeSelf(); self.touchJoint = nil; self.isFocus = nil; end end circle:addEventListener("touch", circle);

Just drag and drop the code in a new project, it should all work out of the box.

Mr hachisoft,

It’s working now !!

That means a lot me.

Thanks for you help.

Soo Wee Chai