Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
How to get rotation of an object with respect to x axis — Gideros Forum

How to get rotation of an object with respect to x axis

simransimran Member
edited May 2014 in General questions
I have 2 objects and Both of them are rotating

object1 is rotating at an anchor point and I have done something like this to make it rotate
object1:setRotation(object1:getRotation() + 4) and so if I do

print(object1:getRotation()) in some part of code, all it does is starting from some value, increments it by 4 degrees and goes on till thousands, but what I wanted was it should return the values with in the range of 360 degrees.


2. my object 2 is moving around an eclipse following the equation of eclipse to move and jumps(at a particular event),
now if I check object2:getRotation() at any point of time, even if it jumped, it always always returns 0.

to check the collision of these two object, I need to have their angles so that I can use TNT collision engine ,right? So how do I fix it?
I am attaching an image in case I could not explain it well

Comments

  • petecpetec Member
    To keep the rotation of object1 in the 360 degree range you could do something like:
    if object1:getRotation()>360 then
          object1:setRotation(object1:getRotation()-360)
    end
    If object2 is your character and if that is always upright then its rotation will be 0. I guess you must have an angle value for where the character is around your ellipse in the equation you are using to make it follow that path.
  • simransimran Member
    @petec : Yes, Right. I have used eclipse equation translate the character along the eclipse, its angle is always 0, but how do I get the angle value for the character?
  • petecpetec Member
    Presumably you are using an angle that keeps increasing to move the character around the ellipse. If so, don't you already have an angle value for where the character is around the ellipse?
  • HubertRonaldHubertRonald Member
    edited May 2014
    Hi @simran
    I was reading your case and found it interesting (I love the clock problems).
    so I have developed a code which I hope will help you better manage Gideros :D
    ---------------------------------------------
    -- COLOR: RGB TO HEX
    ---------------------------------------------
    function rgb(r, g, b)
    	return ((r*256)+g)*256+b
    end
     
    ---------------------------------------------
    -- SET BACKGROUND
    ---------------------------------------------
    application:setBackgroundColor(rgb(0, 0, 0))
     
     
    ----------------------------------------
    -- LOCK ORIENTATION
    ----------------------------------------
    stage:setOrientation(Stage.PORTRAIT)
     
     
    ----------------------------------------
    --	FIX DIMENSION DEVICE
    ----------------------------------------
    _W, _H = application:getContentWidth(), application:getContentHeight()
    Wdx = application:getLogicalTranslateX() / application:getLogicalScaleX()
    Hdy = application:getLogicalTranslateY() / application:getLogicalScaleY()
     
     
    --[[
    	----------------------------------------
    	--	CLASS DRAW ARC
    	----------------------------------------
    	-- thanks to <a href="http://forum.gideros.rocks/profile/ndoss" rel="nofollow">@ndoss</a>
    	-- <a href="http://giderosmobile.com/forum/discussion/346/snippet-arccircleellipse#Item_1" rel="nofollow">http://giderosmobile.com/forum/discussion/346/snippet-arccircleellipse#Item_1</a>
    ]]
    Arc = gideros.class(Shape)
    function Arc:init(t)
       local x          = t.x or t.y or 0
       local y          = t.y or t.x or 0
       local fillStyle  = t.fillStyle or { Shape.SOLID, 0x000000, 0.5 }
       local lineStyle  = t.lineStyle or { 2, 0xffffff, 0.5  }
       local xradius    = t.xradius or t.radius or 100
       local yradius    = t.yradius or t.radius or 100
       local sides      = t.sides or (xradius + yradius)/2 
       local startAngle = t.startAngle or 0
       local arcAngle   = t.arcAngle or 1
     
       self:setFillStyle(unpack(fillStyle))
       self:setLineStyle(unpack(lineStyle))
     
       local angleStep = arcAngle / sides
     
       self:setPosition(x,y)
       local xx = math.cos(startAngle*2*math.pi) * xradius
       local yy = math.sin(startAngle*2*math.pi) * yradius
     
       self:beginPath()
       self:moveTo(xx, yy)
       for i = 1,sides do
          local angle = startAngle + i * angleStep
          self:lineTo(math.cos(angle*2*math.pi) * xradius,
                      math.sin(angle*2*math.pi) * yradius)
       end
       self:endPath()
     
       if t.alpha then
          self:setAlpha(t.alpha)
       end
       if t.parent then
          t.parent:addChild(self)
       end
     
       return self
    end
     
     
    ----------------------------------------
    --	DRAW ELLIPSE
    ----------------------------------------
    local radiusY = 300 -- y
    local radiusX = 74 -- x
    Arc.new{x=_W/2+2*radiusX,y=_H/2+radiusY,xradius=radiusX, yradius=radiusY, sides=sides,
                  fillStyle = { Shape.SOLID, rgb(200, 200, 200), 0.3 },
                  lineStyle = { 0, rgb(200, 200, 200), 0.3 },
                  parent = stage}
     
     
     
    ----------------------------------------
    --	CLASS DRAW SEGMENT
    ----------------------------------------
    Segment = Core.class(Shape)
     
    function Segment:init(config)
    	-- Settings to control the line
    	self.conf = {
    		Width =  1.5,
    		Color = rgb(255, 255, 255),
    		Alpha = 1,
    	}
     
    	if config then
    		--copying configuration
    		for key,value in pairs(config) do
    			self.conf[key]= value
    		end
    	end
     
    	self:setVisible(false)
    end
     
    -- Draw line
    function Segment:draw(dots)
    	self:beginPath()
    	self:setLineStyle(self.conf.Width, self.conf.Color, self.conf.Alpha)
    	self:moveTo(dots.x[1],dots.y[1])
    	self:lineTo(dots.x[2],dots.y[2])
    	self:endPath()
    	self:setVisible(true)
    end
     
    -- hide line
    function Segment:hide()
    	self:clear()
    	self:setVisible(false)
    end
     
     
    ----------------------------------------
    --	DRAW NEEDLE
    ----------------------------------------
    local needle = Segment.new()
    stage:addChild(needle)
    needle:draw({x={_W/2+2*radiusX, _W/2+2*radiusX},y={_H/2+radiusY, _H/2}})
     
     
    ----------------------------------------
    --	FRAME CLOCK CONFIG
    ----------------------------------------
    local cos = math.cos
    local sin = math.sin
    local rad = math.rad
    countFPS = 0 -- 60 ~= 1 sec
    theta = 270 -- Reference of axes in mobile is different
     
    stage:addEventListener(Event.ENTER_FRAME, 
    	function()
    		countFPS = countFPS + 1
    		if countFPS == 10 then -- change 10 if you need more or less velocity
    			needle:hide()
    			theta = theta + 1
     
    			needle:draw({
    						x={_W/2+2*radiusX, _W/2+2*radiusX+radiusX*cos(rad(theta))},
    						y={_H/2+radiusY, _H/2+radiusY+radiusY*sin(rad(theta))}
    						})
    			countFPS = 0
    		end
     
    	end)

    you can download this source from here too
    https://github.com/HubertRonald/Clock_Ellipse/blob/master/main.lua

    Bye :)>-
  • simransimran Member
    Hey, thanks a ton for that :) but what I am really stuck at is detecting collision of needle with a character that is translating around an ellipse all the time, and I am totally stuck at it
Sign In or Register to comment.