Hello,
I have been trying to apply Pythagoras theorem in order to get my project working. What I’m trying to do is,
I want to calculate x and y co ordinates from an angle.(see image). And then I want to move a rect to this position.
Imagine an arrow rotating at a center which stops when user taps on screen and then suddenly it fires a bullet in the direction where arrow is pointing.
local square = display.newRect( 160, 260, 30, 30 ) local widget = require( "widget" ) local a = 0 local angle =0 local x local y local options = { width = 128, height = 128, numFrames = 1, sheetContentWidth = 128, sheetContentHeight = 128 } local spinnerSingleSheet = graphics.newImageSheet( "widget-spinner-single.png", options ) local spinner = widget.newSpinner( { width = 50, height = 50, sheet = spinnerSingleSheet, startFrame = 1, deltaAngle = -2, incrementEvery = 20 } ) spinner.x = 160 spinner.y = 260 spinner:rotate(90) local function Intangle() angle = angle + 2 end local function start() if a == 0 then timer1 = timer.performWithDelay( 20, Intangle, 0 ) spinner:start() a = 1 print("rotating from",angle) elseif a == 1 then timer.cancel( timer1 ) spinner:stop() a = 0 print("rotated to ",angle) y = math.sin( ( angle)) \* 140 ---- sin(angle) = ( Y i.e height / hypo) for right angled triangle x = math.cos( ( angle)) \* 140 print(y ,"= y",x ,"= x") transition.moveBy( square, { x=y, y=x, time=2000 } ) end end Runtime:addEventListener("tap",start)