problem, help me

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.

Hello,

without the image is quite hard to understand. Can you give us a link to see the image (example : google drive)?

You can use math2d (or other freely available helpers).

https://roaminggamer.github.io/RGDocs/pages/SSK2/libraries/math2D/

You must also realize that the coordinates in Corona are not Cartesian. 

+X is right, - X is left

+Y is down, -Y is up

In Corona, 0-degrees is traditionally the vector <0,-1>, 90 is traditionally <1,0>, and so on.

I have many examples of bullets and firing in my various gits: https://github.com/roaminggamer

The first one I found was in the ‘AskEd’ collection: https://github.com/roaminggamer/RG_FreeStuff/tree/master/AskEd

Specifically from April 2015: https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2015/04/fireBullet.zip

Here too are a couple videos to other old solutions:

https://www.youtube.com/watch?v=wklDDanKEeQ

https://www.youtube.com/watch?v=yWOIAdV4GAE

this is just me being a pesky math-nerd nitpicker, so grain of salt, but…

it most certainly IS Cartesian, tho quadrant iv, equivalent to quadrant i with y inverted (scaled by -1).

(added, to clarify:  cartesian only requires two oriented perpendicular axes, does not require a specific orientation)

the unit vector for it’s zero-angle is <1,0>, always.  (if trig seems wonky it’s likely because treating as if quadrant i)

@Davebollinger

Thanks for posting!

Cartesian Goof

 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:

plane.png

(free art from sprite lib: https://www.widgetworx.com/spritelib/))

or this,

ship.png

(free art from Kenney; Purchased license to support him: http://kenney.nl/))

In the second case, I simply rotate the art (in an art program) by 180 degrees, then my math and art are consistent.

Thanks again!

updated last post with additional info

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:&nbsp; what is the unit vector of 0 degrees?&nbsp; A:&nbsp; ", 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.

@davebollinger and @roaminggamer

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  :slight_smile:

So in my world:

rotation  0 is (0, -1) and moves clockwise

angle      0 is (1, 0) and moves anticlockwise

Y axis     is reversed

Hello,

without the image is quite hard to understand. Can you give us a link to see the image (example : google drive)?

You can use math2d (or other freely available helpers).

https://roaminggamer.github.io/RGDocs/pages/SSK2/libraries/math2D/

You must also realize that the coordinates in Corona are not Cartesian. 

+X is right, - X is left

+Y is down, -Y is up

In Corona, 0-degrees is traditionally the vector <0,-1>, 90 is traditionally <1,0>, and so on.

I have many examples of bullets and firing in my various gits: https://github.com/roaminggamer

The first one I found was in the ‘AskEd’ collection: https://github.com/roaminggamer/RG_FreeStuff/tree/master/AskEd

Specifically from April 2015: https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2015/04/fireBullet.zip

Here too are a couple videos to other old solutions:

https://www.youtube.com/watch?v=wklDDanKEeQ

https://www.youtube.com/watch?v=yWOIAdV4GAE

this is just me being a pesky math-nerd nitpicker, so grain of salt, but…

it most certainly IS Cartesian, tho quadrant iv, equivalent to quadrant i with y inverted (scaled by -1).

(added, to clarify:  cartesian only requires two oriented perpendicular axes, does not require a specific orientation)

the unit vector for it’s zero-angle is <1,0>, always.  (if trig seems wonky it’s likely because treating as if quadrant i)

@Davebollinger

Thanks for posting!

Cartesian Goof

 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:

plane.png

(free art from sprite lib: https://www.widgetworx.com/spritelib/))

or this,

ship.png

(free art from Kenney; Purchased license to support him: http://kenney.nl/))

In the second case, I simply rotate the art (in an art program) by 180 degrees, then my math and art are consistent.

Thanks again!

updated last post with additional info

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:&nbsp; what is the unit vector of 0 degrees?&nbsp; A:&nbsp; ", 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)