Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

problem, help me
Started by mariasmile509 May 03 2018 05:45 PM

10 replies to this topic
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

mariasmile509

[GLOBAL: userInfoPane.html]
mariasmile509
  • Observer

  • 2 posts
  • Corona SDK

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.



[TOPIC: post.html]
#2

remiduchalard

[GLOBAL: userInfoPane.html]
remiduchalard
  • Contributor

  • 301 posts
  • Corona SDK

Hello,

 

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



[TOPIC: post.html]
#3

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 6,920 posts
  • Corona SDK

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:


  • sporkfin likes this

[TOPIC: post.html]
#4

davebollinger

[GLOBAL: userInfoPane.html]
davebollinger
  • Corona Geek

  • 1,167 posts
  • Enterprise

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

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

 

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)



[TOPIC: post.html]
#5

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 6,920 posts
  • Corona SDK

@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!


Edited by roaminggamer, 04 May 2018 - 09:44 AM.


[TOPIC: post.html]
#6

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 6,920 posts
  • Corona SDK

updated last post with additional info



[TOPIC: post.html]
#7

davebollinger

[GLOBAL: userInfoPane.html]
davebollinger
  • Corona Geek

  • 1,167 posts
  • Enterprise

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!


  • roaminggamer likes this

[TOPIC: post.html]
#8

davebollinger

[GLOBAL: userInfoPane.html]
davebollinger
  • Corona Geek

  • 1,167 posts
  • Enterprise

@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)



[TOPIC: post.html]
#9

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 6,920 posts
  • Corona SDK

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.



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.


Edited by roaminggamer, 04 May 2018 - 10:44 AM.


[TOPIC: post.html]
#10

davebollinger

[GLOBAL: userInfoPane.html]
davebollinger
  • Corona Geek

  • 1,167 posts
  • Enterprise

Having < 1, 0 > as 0-degrees is just weird for me.

 

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)


  • roaminggamer likes this

[TOPIC: post.html]
#11

sporkfin

[GLOBAL: userInfoPane.html]
sporkfin
  • Contributor

  • 393 posts
  • Corona SDK

@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  :)

 

So in my world:

 

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

angle     0 is (1, 0) and moves anticlockwise

Y axis    is reversed




[topic_controls]
[/topic_controls]