Author Topic: Calculating angles?  (Read 7177 times)

0 Members and 1 Guest are viewing this topic.

Offline chickendude

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 817
  • Rating: +90/-1
  • Pro-Riot Squad
    • View Profile
Calculating angles?
« on: October 23, 2012, 08:45:26 am »
I'm trying to put together a routine that will calculate the appropriate X/Y velocity to go towards an object. What i've got now is really buggy, i think the general algorithm/idea is ok though. My biggest hurdle so far has been handling negative numbers, as none of my routines for multiplication/division seem to like negative numbers. What i'm currently doing is really just adjusting the Y velocity and not bothering with the X velocity. The general equation is something like this:
(object Y - bullet Y)/(object X - bullet X)

The X/Y velocities have 5 bits which act as a decimal place, but this doesn't let me use it. I hacked in the ability to handle negative numbers in the division routine (essentially turning all negative numbers positive, then negating the result afterward), but my multiplication routine doesn't like negatives numbers either.

Offline Xeda112358

  • they/them
  • Moderator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4704
  • Rating: +719/-6
  • Calc-u-lator, do doo doo do do do.
    • View Profile
Re: Calculating angles?
« Reply #1 on: October 23, 2012, 10:09:58 am »
Hmm, have you ever created a line drawing routine? Basically, take a point (x1,y1) and (x2,y2), and pretend you are drawing a line between them, but instead of drawing the pixels, use it as a path to draw your bullets. I am sure that finding the velocities would be a lot easier, though. Hopefully somebody comes along that knows more than I do!

Offline Keoni29

  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2466
  • Rating: +291/-16
    • View Profile
    • My electronics projects at 8times8
Re: Calculating angles?
« Reply #2 on: October 23, 2012, 11:07:13 am »
If the input is negative just multiply it by -1. Then write a script which handles positive numbers only and then multiply the outcome by -1 if the input was smaller than zero.
« Last Edit: October 23, 2012, 11:07:44 am by Keoni29 »
If you like my work: why not give me an internet?








Offline thepenguin77

  • z80 Assembly Master
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1594
  • Rating: +823/-5
  • The game in my avatar is bit.ly/p0zPWu
    • View Profile
Re: Calculating angles?
« Reply #3 on: October 23, 2012, 01:15:22 pm »
I'm trying to put together a routine that will calculate the appropriate X/Y velocity to go towards an object. What i've got now is really buggy, i think the general algorithm/idea is ok though. My biggest hurdle so far has been handling negative numbers, as none of my routines for multiplication/division seem to like negative numbers. What i'm currently doing is really just adjusting the Y velocity and not bothering with the X velocity. The general equation is something like this:
(object Y - bullet Y)/(object X - bullet X)

The X/Y velocities have 5 bits which act as a decimal place, but this doesn't let me use it. I hacked in the ability to handle negative numbers in the division routine (essentially turning all negative numbers positive, then negating the result afterward), but my multiplication routine doesn't like negatives numbers either.

Ok, so basically, I'm assuming this is what you want to happen:

Angle = atan2(x, y)
xVel = cos(angle)
yVel = sin(angle)

Now, yes, this will work, but it's not going to be very fast. Atan2 is just arcTangent that will give you the angle even if it's in the [90,270] region, but that thing is a little annoying to implement. Cos and Sin aren't that hard, but require lookup tables.


Before I explain this, to make things easier, dX = object X - bullet X and dY = object Y - bullet Y.

What I recommend you do is treat this as a vector problem (sorry, I'm currently in calc 3). Basically, you have your direction vector <dX, dY>. So there is no need to recalculate the direction. All you need to do is scale it to the right speed.

So, the way to do this is to first find the length of the direction vector : length = sqrt(dX^2 + dY^2). And then to scale it to the right speed, divide by the length and multiply by the new speed: <dX, dY>/length*speed.

In the end, here's what you get:
length = sqrt(dX^2 + dY^2)
xVel = (objectX - bulletX)/length*speed
yVel = (objectY - bulletY)/length*speed


The only tricky part of this routine is the square root, but I know I've seen a few around here, so that shouldn't be too difficult.
zStart v1.3.013 9-20-2013 
All of my utilities
TI-Connect Help
You can build a statue out of either 1'x1' blocks or 12'x12' blocks. The 1'x1' blocks will take a lot longer, but the final product is worth it.
       -Runer112

Offline Keoni29

  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2466
  • Rating: +291/-16
    • View Profile
    • My electronics projects at 8times8
Re: Calculating angles?
« Reply #4 on: October 23, 2012, 01:46:58 pm »
Or the quick and dirty way:
if x < objectX {x++}
if x > objectX {x--}
if y < objectX {y++}
if y > objectX {y--}

It is not pretty, but Δy/Δx will be the same and it will move somewhat diagonally.
If you like my work: why not give me an internet?








Offline calc84maniac

  • eZ80 Guru
  • Coder Of Tomorrow
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2912
  • Rating: +471/-17
    • View Profile
    • TI-Boy CE
Re: Calculating angles?
« Reply #5 on: October 23, 2012, 01:53:02 pm »
Or the quick and dirty way:
if x < objectX {x++}
if x > objectX {x--}
if y < objectX {y++}
if y > objectX {y--}

It is not pretty, but Δy/Δx will be the same and it will move somewhat diagonally.
Aside from looking awful, I don't think that would work for this purpose because typically bullets don't change direction after being fired :P
"Most people ask, 'What does a thing do?' Hackers ask, 'What can I make it do?'" - Pablos Holman

Offline chickendude

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 817
  • Rating: +90/-1
  • Pro-Riot Squad
    • View Profile
Re: Calculating angles?
« Reply #6 on: October 23, 2012, 02:36:30 pm »
Awesome, that's exactly what i needed. I remembered A2 + B2 = C2 but was afraid i'd have to use sine/cosine (and an LUT) which seemed like a lot of work/memory for something that's not that important. I looked at a couple line-drawing routines but they were just way above my head. It's for a boomerang-effect, but only on the way back (the angle should be constant on the way out).

The routine i used is pretty buggy.

Actually, i'm gonna try to implement that right now :)

EDIT: ;)
« Last Edit: November 21, 2012, 12:56:06 pm by chickendude »

Offline Keoni29

  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2466
  • Rating: +291/-16
    • View Profile
    • My electronics projects at 8times8
Re: Calculating angles?
« Reply #7 on: October 23, 2012, 02:50:55 pm »
Or the quick and dirty way:
if x < objectX {x++}
if x > objectX {x--}
if y < objectX {y++}
if y > objectX {y--}

It is not pretty, but Δy/Δx will be the same and it will move somewhat diagonally.
Aside from looking awful, I don't think that would work for this purpose because typically bullets don't change direction after being fired :P
True :P
If you like my work: why not give me an internet?








Offline chickendude

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 817
  • Rating: +90/-1
  • Pro-Riot Squad
    • View Profile
Re: Calculating angles?
« Reply #8 on: October 23, 2012, 03:23:19 pm »
Ok, i just tried to write this out but i'm a little confused as to what "speed" is. It seems to me that speed and length are the same? But that can't be right. Right now, bullet x velocity is 96 (%01100000: moving 3 (%011) pixels every frame) when y velocity = 0, so that should mean that length is also 96, right? It seems to me like:
velX = length/(objX-bullX) * speed
velY = length/(objY-bullY) * speed
..would be more accurate, as shouldn't a larger distance between the object and the bullet give a lower velocity?

Offline calc84maniac

  • eZ80 Guru
  • Coder Of Tomorrow
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2912
  • Rating: +471/-17
    • View Profile
    • TI-Boy CE
Re: Calculating angles?
« Reply #9 on: October 23, 2012, 03:27:08 pm »
You divide by length because you want the speed to be the same no matter what the distance is.
"Most people ask, 'What does a thing do?' Hackers ask, 'What can I make it do?'" - Pablos Holman

Offline chickendude

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 817
  • Rating: +90/-1
  • Pro-Riot Squad
    • View Profile
Re: Calculating angles?
« Reply #10 on: October 23, 2012, 03:38:49 pm »
So what is the speed, then? Would speed be "96"? I'm trying to figure out how to calculate the initial velocities.
Let's simplify 96 to 3.
object = 10,10 (x,y)
bullet = 5,2
length = sqrt(32+02)
length = 3
xVel = (10-5)/3*...3? -> 5/1 xVel = 5
yVel = (10-2)/3*3 -> 8/1 yVel = 8

If i change speed to one:
xVel = (10-5)/3*1 -> 5/3 xVel = 1 2/3
yVel = (10-2)/3*1 -> 8/3 yVel = 2 2/3
I don't quite get what values i'm supposed to be using. :/

EDIT: Maybe i've got it now, i'll test it out tomorrow and see.
« Last Edit: October 23, 2012, 04:44:37 pm by chickendude »

Offline thepenguin77

  • z80 Assembly Master
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1594
  • Rating: +823/-5
  • The game in my avatar is bit.ly/p0zPWu
    • View Profile
Re: Calculating angles?
« Reply #11 on: October 23, 2012, 05:28:19 pm »
Speed is simply how fast you want your bullets to move. 8 would be 8 pixels per frame, 1 would be 1 pixel per frame, and 96 would be 96 pixels per frame. (Assuming the units on xVel are pixels/frame of course)

You can set it to whatever you want.
zStart v1.3.013 9-20-2013 
All of my utilities
TI-Connect Help
You can build a statue out of either 1'x1' blocks or 12'x12' blocks. The 1'x1' blocks will take a lot longer, but the final product is worth it.
       -Runer112

Offline calc84maniac

  • eZ80 Guru
  • Coder Of Tomorrow
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2912
  • Rating: +471/-17
    • View Profile
    • TI-Boy CE
Re: Calculating angles?
« Reply #12 on: October 23, 2012, 10:31:48 pm »
So what is the speed, then? Would speed be "96"? I'm trying to figure out how to calculate the initial velocities.
Let's simplify 96 to 3.
object = 10,10 (x,y)
bullet = 5,2
length = sqrt(32+02)
length = 3
xVel = (10-5)/3*...3? -> 5/1 xVel = 5
yVel = (10-2)/3*3 -> 8/1 yVel = 8

If i change speed to one:
xVel = (10-5)/3*1 -> 5/3 xVel = 1 2/3
yVel = (10-2)/3*1 -> 8/3 yVel = 2 2/3
I don't quite get what values i'm supposed to be using. :/

EDIT: Maybe i've got it now, i'll test it out tomorrow and see.
No, you calculate length as sqrt((10-5)2+(10-2)2). However, you'll probably want to multiply by speed before you divide by length to prevent integer division roundoff errors (simply dividing by length will give you something between 0 and 1, after all, which would round down to 0).
"Most people ask, 'What does a thing do?' Hackers ask, 'What can I make it do?'" - Pablos Holman

Offline chickendude

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 817
  • Rating: +90/-1
  • Pro-Riot Squad
    • View Profile
Re: Calculating angles?
« Reply #13 on: October 24, 2012, 02:05:04 am »
Thanks, i figured that out later. I was confusing dX/Y with velX/Y. :)

Offline chickendude

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 817
  • Rating: +90/-1
  • Pro-Riot Squad
    • View Profile
Re: Calculating angles?
« Reply #14 on: October 24, 2012, 11:47:54 am »
So i wrote out a little routine to calculate all of that and it worked nicely, i realized later that the problems with my first routine weren't (completely) due to that part of the code but rather an error in the main draw/update coordinates loop where i had reused 'd' to set some other registers to 0 when 'd' wasn't 0. After that i rewrote another routine which ignores X velocity (it's always +/- BULLET_SPEED) but is much faster (no square root, only one multiplication/division call) and i think works fine enough :)

EDIT: ;)
« Last Edit: November 21, 2012, 12:56:42 pm by chickendude »