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.
I often struggle with how to say this correctly. I guess I meant not the traditional Cartesian system we are taught in school.
I like your specific correction: Quadrant 4 swapped with Quadrant 1.
Angles
I did it again. I didn’t mean to say the math changes, but rather we need to adjust our math to reflect the way things are oriented in Corona, and to match the way we (as developer) may treat orientations.
When I make games in Corona, I use these vectors to represent these angles (and vice-versa):
< 0, -1 > 0-degrees (UP)
< 1, 0 > 90-degrees (RIGHT)
< 0, 1 > 180-degrees (DOWN)
< -1, 0 > 270-degrees (LEFT)
I do this to save myself effort. Why? Because most art I buy (or get for free in some cases) is presented like this:
fwiw, if you instead rotate your art to “point to” the right, then it’ll match trig, and you probably wouldn’t need your clock-face system.
btw, here’s an interesting (but useless) experiment, restoring “schoolbook” coordinates:
local stage = display.getCurrentStage() stage.yScale = -1 stage.y = display.screenOriginY + display.actualContentHeight -- now you can use as if schoolbook: -- draw a line from origin at bottom-left, up-and-right at 45-degrees local line = display.newLine(0,0, 100\*math.cos(math.pi/4), 100\*math.sin(math.pi/4))
just don’t expect composer/widget/etc to make any sense after that!
@mariasmile509, back to the OP: what are we given? the orientation of the arrow, presumably a display object, so it’s .rotation??? what are we determining? a point in that direction, by a certain distance from the arrow??? if so:
local theta = math.rad(arrow.rotation) local radius = 100 -- or whatever local answerX = arrow.x + radius \* math.cos(theta) local answerY = arrow.y + radius \* math.sin(theta)
…as with the side-discussion with roaminggamer above, this presumes that your “arrow” points to the right when its .rotation=0 (otherwise you’d want to also add/subtract whatever rotation is implied by its visuals)
Thanks Dave. I know that I could to that… but, for me it just seems weird.
When I think in terms of the game world, specifically shooters and action games like 1942, lunar lander, etc., the top of the screen is UP. Up is also the primary facing so it is ideal to make that 0-degrees.
If we further consider that 0-degress is the default rotation in corona and by positive values we rotate to the right… this all comes together and leads me to the orientation choices I outline above.
Having < 1, 0 > as 0-degrees is just weird for me.
I tend to use the math as a tool to get the results I want or that fit the rules of the ‘world’ I’m operating in.
Whatever the case, I admit freely, you’re a much more math savvy individual than I am. I know the general ideas and can find the solutions, but I get the sense you remember the fundamental rules and operations better than I do.
PS - I’ll stop now … I feel I’ve started hijacking this thread.
print("Q: what is the unit vector of 0 degrees? A: ", math.cos(0), math.sin(0)) -- always, doesn't matter the orientation of the frame
BUT, it turns out that the better you know that, the less it actually matters. (fe, if someone told me to invert the x-axis and rotate the frame by arbitrary 30-degrees, i’d still “sense” where the trig was going to end up) So, who cares really, use whatever is convenient, no further justification needed – tho using <1,0> does tend to save some calcs of the form “theTrigAngle = 90 - theClockAngle” (or vice-versa).
(btw, this angle stuff is implied by the OP, so it’s not TOO far off-topic)
@mariasmile509 will you be moving the object using display transitions or physics? If you use physics, you will have to ues trigonometry to move the physics object correctly. If you are moving a display object, the pythagorean method will work fine.
It’s fun to see how different minds tackle the same problem.
Because of my art background I have to start 0 rotation at 12 o’clock - that’s how all of the graphics programs construe rotation. If only the first clock went anit-clockwise. Obviously they weren’t thinking of trigonometry and quadrants when they set that up
I often struggle with how to say this correctly. I guess I meant not the traditional Cartesian system we are taught in school.
I like your specific correction: Quadrant 4 swapped with Quadrant 1.
Angles
I did it again. I didn’t mean to say the math changes, but rather we need to adjust our math to reflect the way things are oriented in Corona, and to match the way we (as developer) may treat orientations.
When I make games in Corona, I use these vectors to represent these angles (and vice-versa):
< 0, -1 > 0-degrees (UP)
< 1, 0 > 90-degrees (RIGHT)
< 0, 1 > 180-degrees (DOWN)
< -1, 0 > 270-degrees (LEFT)
I do this to save myself effort. Why? Because most art I buy (or get for free in some cases) is presented like this:
fwiw, if you instead rotate your art to “point to” the right, then it’ll match trig, and you probably wouldn’t need your clock-face system.
btw, here’s an interesting (but useless) experiment, restoring “schoolbook” coordinates:
local stage = display.getCurrentStage() stage.yScale = -1 stage.y = display.screenOriginY + display.actualContentHeight -- now you can use as if schoolbook: -- draw a line from origin at bottom-left, up-and-right at 45-degrees local line = display.newLine(0,0, 100\*math.cos(math.pi/4), 100\*math.sin(math.pi/4))
just don’t expect composer/widget/etc to make any sense after that!
@mariasmile509, back to the OP: what are we given? the orientation of the arrow, presumably a display object, so it’s .rotation??? what are we determining? a point in that direction, by a certain distance from the arrow??? if so:
local theta = math.rad(arrow.rotation) local radius = 100 -- or whatever local answerX = arrow.x + radius \* math.cos(theta) local answerY = arrow.y + radius \* math.sin(theta)
…as with the side-discussion with roaminggamer above, this presumes that your “arrow” points to the right when its .rotation=0 (otherwise you’d want to also add/subtract whatever rotation is implied by its visuals)
Thanks Dave. I know that I could to that… but, for me it just seems weird.
When I think in terms of the game world, specifically shooters and action games like 1942, lunar lander, etc., the top of the screen is UP. Up is also the primary facing so it is ideal to make that 0-degrees.
If we further consider that 0-degress is the default rotation in corona and by positive values we rotate to the right… this all comes together and leads me to the orientation choices I outline above.
Having < 1, 0 > as 0-degrees is just weird for me.
I tend to use the math as a tool to get the results I want or that fit the rules of the ‘world’ I’m operating in.
Whatever the case, I admit freely, you’re a much more math savvy individual than I am. I know the general ideas and can find the solutions, but I get the sense you remember the fundamental rules and operations better than I do.
PS - I’ll stop now … I feel I’ve started hijacking this thread.
print("Q: what is the unit vector of 0 degrees? A: ", math.cos(0), math.sin(0)) -- always, doesn't matter the orientation of the frame
BUT, it turns out that the better you know that, the less it actually matters. (fe, if someone told me to invert the x-axis and rotate the frame by arbitrary 30-degrees, i’d still “sense” where the trig was going to end up) So, who cares really, use whatever is convenient, no further justification needed – tho using <1,0> does tend to save some calcs of the form “theTrigAngle = 90 - theClockAngle” (or vice-versa).
(btw, this angle stuff is implied by the OP, so it’s not TOO far off-topic)