Want help to do some maths.

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)

not sure if this is exactly what you’re asking, but maybe it’ll offer enough of “clues” for you to finish it:

local arrow = display.newPolygon( display.contentCenterX, display.contentCenterY, {25,0,-25,15,-25,-15}) arrow.radius = 25 local squares = {} local function tap() if (arrow.thandle) then transition.cancel(arrow.thandle) arrow.thandle = nil -- spawn bullet local theta = math.rad(arrow.rotation) local startx = arrow.x + arrow.radius \* math.cos(theta) local starty = arrow.y + arrow.radius \* math.sin(theta) local endx = arrow.x + (arrow.radius + 140) \* math.cos(theta) local endy = arrow.y + (arrow.radius + 140) \* math.sin(theta) local square = display.newRect(startx,starty,20,20) square.thandle = transition.to(square, { time=2000, x=endx, y=endy }) squares[#squares+1] = square else arrow.thandle = transition.to(arrow, { time=5000, rotation=-360, delta=true, iterations=-1 }) end end Runtime:addEventListener("tap", tap)

The cause for the error is mixing radians and degrees as the math functions expect radians while Corona uses degrees for its rotations.

So the core fix in your code is this (there might be other issues, but that should be the reason for your rotation issues)

[lua]

math.sin( ( math.rad(angle)))

math.cos( ( math.rad(angle)))

[/lua]

Take a look at my mathlib.lua: https://gist.github.com/HoraceBury/9431861

Search for rotateTo and lengthOf

There is also a main.lua included which demo’s some of the functions in the math library.

not sure if this is exactly what you’re asking, but maybe it’ll offer enough of “clues” for you to finish it:

local arrow = display.newPolygon( display.contentCenterX, display.contentCenterY, {25,0,-25,15,-25,-15}) arrow.radius = 25 local squares = {} local function tap() if (arrow.thandle) then transition.cancel(arrow.thandle) arrow.thandle = nil -- spawn bullet local theta = math.rad(arrow.rotation) local startx = arrow.x + arrow.radius \* math.cos(theta) local starty = arrow.y + arrow.radius \* math.sin(theta) local endx = arrow.x + (arrow.radius + 140) \* math.cos(theta) local endy = arrow.y + (arrow.radius + 140) \* math.sin(theta) local square = display.newRect(startx,starty,20,20) square.thandle = transition.to(square, { time=2000, x=endx, y=endy }) squares[#squares+1] = square else arrow.thandle = transition.to(arrow, { time=5000, rotation=-360, delta=true, iterations=-1 }) end end Runtime:addEventListener("tap", tap)

The cause for the error is mixing radians and degrees as the math functions expect radians while Corona uses degrees for its rotations.

So the core fix in your code is this (there might be other issues, but that should be the reason for your rotation issues)

[lua]

math.sin( ( math.rad(angle)))

math.cos( ( math.rad(angle)))

[/lua]

Take a look at my mathlib.lua: https://gist.github.com/HoraceBury/9431861

Search for rotateTo and lengthOf

There is also a main.lua included which demo’s some of the functions in the math library.