Snippet Deflection Angle

emjlr3

Change can be a good thing
Reaction score
397
Was asked about stuff bouncing off other stuff a while back, and posted a script of which I couldn't recall the source, and which I wasn't sure how functioned.

To that end, I decided I wanted my own script for doing this, one that I understood, and could manipulate in the future to my needs.

There, for what is worth, here is my own:

Description:

Code:
A missile (or some other assumingly circular object) is traveling along minding its own business.
From out of no where comes a building, or perhaps some other projectile, or maybe even a wall out ahead.
Preferably in a somewhat realistic fashion.
If such a collision were to take place, and the missile interacts in an [U]elastic[/U] fashion,
what should be its new and improved, direction of travel?

JASS:
// Returns angle in radians
function GetDeflectionAngle takes unit missile, unit target returns real
    return 2.*Atan2(GetUnitY(target)-GetUnitY(missile),GetUnitX(target)-GetUnitX(missile))+bj_PI-GetUnitFacing(missile)*bj_DEGTORAD
endfunction

// Wrapper, returns angle in degrees
function GetDeflectionAngleDeg takes unit missile, unit target returns real
    return bj_RADTODEG*GetDeflectionAngle(missile,target)
endfunction


We are presumably colliding a missile with some static object. You can always replace the target with a set of x and y coords, or some other projectile.

It's important to note, however, that this is entirely dependant upon the direction the missile is traveling, which I assume is its facing. If this is not the case, update the script to account for this irregularity.

On a second and equally important note, the first function returns the angle in radians, whereas the wrapper returns the angle in degrees. Remember that, its important!!!

Lastly, a word from the wise, for aesthetics sake, dont update your projectiles facing, its slow and ugly. Simply create a new one at the correct angle in its place.

Simple enough.
Feel free to stop reading here, and just start your collisions. :D

========================================

Then again, if you are feeling bold, try the rest on for size:

Given what we know about deflections, we can see that so long as our entity collides with some flat surface, given we know the angular relationship between the two (incident angle), we can determine the exiting (accidental) angle.

Well, we don't have a flat surface. We have a building, or something like it. Additionally however, we have a center position, or an x and y coordinate system, for our target, the direction our missile is traveling (its facing), and the angle between it and the target its interacting with (which we can calculate using simple trigonometry).

We are therefore going to assume that at some distance out from our center point (collision radius), the missile is interacting with a flat surface. A silly assumption, I know, but it's the only one that works.

Ok, now the goal is to eventually determine our accidental angle (B in the picture), which is directly related to the incidental angle (a in the picture):

Deflection.png


Ergo, if we can calculate our incidental angle, we will be able to calculate our accidental angle. Easy enough, pretty much pie infact, with some simple arithmetic and geometry.

First we need a flat surface. Well, infact, we dont really need the surface, simply it's surface normal, which for our purposes is the angle between the missile and the building:
JASS:
bj_RADTODEG*Atan2(GetUnitY(target)-GetUnitY(missile),GetUnitX(target)-GetUnitX(missile))


This provides us with the WC3 world angle between these two entities, which is the angle perpendicular to our interaction "surface".

Now the difference between the surface normal and the missiles traveling angle is our incident angle:
JASS:
angle-face


This can be negative, infact it needs to be, considering we're working on the positive direction (in other words, clockwise about our WC3 world), so don't fret.

Now that we have this difference, doubling it will give us the difference between the incident and accident angles, since they are both equally spaced apart from the surface normal:
JASS:
2.*(angle-face)


Not so fast, yes this is the difference between the two, but we can't simply add this to our current direction angle to give us the new direction, that won't work at all.

We are bouncing off of and moving in the opposite direction from the target. Therefore our new angle is going to be in the opposite direction of the current:
JASS:
face+180.


There are 360 degrees in a circle, +180 will give us the opposite direction.

Now that we have our opposite direction, adding the multiple of our surface/incident difference will give us the new angle of deflection:
JASS:
face+180.+(2.*(angle-face))


Using simple algebra, this can be further optimized through expansion, into the following form:

JASS:
face+180.+(2.*(angle-face))=face+180.+2.*angle-2.*face
=-face+180.+2.*angle //or,
=2.*angle+180.-face



***
Lets do a simple example on a sheet of paper, go ahead, take one out, I'll wait......

Our missile is traveling at 45 degrees, and when it interacts with the building they are at a 90 degree angle. Draw a compass, label the axis. Draw a dot somewhere, facing 45 degrees, and draw another at a 90 degree angle from the first.

JASS:
ang=90.
face=45.

=face+180.+(2.*(angle-face))
=45.+180.+(2.*(90.-45.))
=225.+(2.*45.)
=225.+90.
=315. degrees


Draw a line directed outward from your first dot at a 315 degree angle. Looks pretty good, no? Well, thats the idea, I knew you'd see things my way.

Enjoy,
Hulk3
 
Well, that's all very easy to understand....but what is this used for?
 
If such a collision were to take place, and the missile interacts in an elastic manor,
what should be its new and improved, direction of travel?

im guessing if you want to code something that will bounce off another thing, like a wall, and you want the angle of which its supposed to head towards after impact.
 
Lastly, a word from the wise, for aesthetics sake, dont update your projectiles facing, its slow and ugly. Simply create a new one at the correct angle in its place.

well, i was constantly changing the facing of my missile here..
and it looks not that bad.

[Snippet]Advanced Projectile Motion Experiment
 
This is a commonly asked for snippet, so it'll be very useful.

Short, but well explained. Exactly how it should be. The only thing missing are some titles to separate the different chapters.
 
you were also changing it by just a wee bit

trying turning it 180 degrees
 
It's important to note, however, that this is entirely dependant upon the direction the missile is traveling, which I assume is its facing. If this is not the case, update the script to accommodate this.
:rolleyes:

Introduce wrappers to remove that limitation:

JASS:
function GetDeflectionAngleXY takes real angle, real x1, real y1, real x2, real y2 returns real
    return angle+180.+(2.*((bj_RADTODEG*Atan2(y2-y1,x2-x1))-angle))
endfunction

//GetDeflectionAngleLoc(...)?

function GetDeflectionAngle takes unit u, unit obstacle returns real
    return GetDeflectionAngleXY(GetUnitFacing(u),GetUnitX(u),GetUnitY(u),GetUnitX(obstacle),GetUnitY(obstacle))
endfunction
Also, I never use degrees. So maybe it would be more user friendly to take and return the angle in radians? Users who insist on using degrees can convert the argument and returned value with bj_DEGTORAD and bj_RADTODEG.
 
thats not a bad idea

I usually work in radians myself - but for me this was easier to accomplish in degrees, from a spacial perspective

its not impossible for the user to correct for this, or simply add it to the snippet themselves
 
Does missle leak?

What missile? There are no missiles created, unless I'm misunderstanding something (or if your begging for help on a map your making). Reals don't leak, if that's what your asking.


Anyway, nice job. I find this VERY useful for a map that I'm making. +Rep.
 
This is indeed very useful.

I was thinking about making you make this a 1-liner, so that it'd be inlined, but that'd really lower the readability and the editability of it.
Anyway, it's not like this is going to be called very often. How often does a missile bounce? :rolleyes:

Anyway, I'll be sure to use this sometime. Thanks for submitting.

Approved.
 
optimized script and added radian return function - should be inline friendly now
 
optimized script and added radian return function - should be inline friendly now

No it wouldn't, check the jasshelper documentation about inline, arguments must be use only one time and in the order of the function header.
 
It would inline if he kept the original "2 * angle + bj_PI - facing * bj_DEGTORAD"
 
JASS:
function Double takes integer i returns integer
    return i * i
endfunction


^This wouldn't inline by JassHelper because the parameter is referenced more than once.
 
o well

this shouldn't really be called periodically, and the math aint all that bad anyhow
 
Inlining wouldn't make the math any faster anyway. It's just a little bonus really (if it's possible to do), and has no noticeable benefits in the world of JASS.
The function is fine as it is.
 
no but there would be a net effect to reduce processing time, ergo, removing a bj wrapper
 
I am not getting the example : (. Can you upload another picture that explains it please?

Edit:
emjlr3: Our missile is traveling at 45 degrees, and when it interacts with the building they are at a 90 degree angle. Draw a compass, label the axis. Draw a dot somewhere, facing 45 degrees, and draw another at a 90 degree angle from the first.

Code:
        this line is 
        "southern wall of 
        building"
        ----------------------
                  /\
                 /  \
                /    \
               /      \
              /        \
             /          \
            /            \
          45deg        315deg
          *                *
          ^                ^ 
       the missile         missile after
       traveling at        colliding with 
       45deg               the wall

Looking at the numbers it seams that this is happening but I still can't understand your example (drawing the dots).


Edit2: "...and when it interacts with the building they are at a 90 degree angle"

Is it possible "entity1" to "interact" with "entity2" at an angle other than 90 degree?


Edit3:
emjlr3: Well, infact, we dont really need the surface, simply it's surface normal, which for our purposes is the angle between the missile and the building

Why do we (emjlr3) say that the angle between the missile and the target (building) is equivalent to the surface normal??
 
General chit-chat
Help Users
  • No one is chatting at the moment.
  • Varine Varine:
    I should have had all of this like a month ago but whatever, it's fine. He's trying a thing, idk it's his menu I don
  • Varine Varine:
    I don't care.
  • Varine Varine:
    Fuck I need more saturday nights off. Turns out I'm much cooler to drunk girls. If I hang out with enough people like that, eventually I'll meet one that likes me sober too
  • Varine Varine:
    idk if anyone has ever done IT. I am not IT but I just got off a 20 minute phone call with the general manager asking me how to make her new computer print. I do adore her, like I know her personally, but I asked three times if the printer was on. She apologized for bothering me, and Chef Ben walked in a few minutes later, turned the printer on apparently, and then called to ask if I was trying to print shit from my house again
  • Varine Varine:
    So I have a newfound appreciation
  • Varine Varine:
    And also I'm going to remove that one from my secondary career options
  • Varine Varine:
    As I get older I'm not thinking I can do this forever anymore, and I'm kind of dead end right now. I get a fine salary and fair raises, like I can easily live my lifestyle on this normally, but I don't know if I can do this in ten years, and I don't think I want to. I don't really have any desire to take over another restaurant, and I don't want to try and own one, and I doubt I'll luck out like I did with that taco place in Texas
  • Varine Varine:
    I have a math degree but that was a while ago and I'm not positive I actually deserved that. It was kind of just given to me I feel like cuz I was in school so long. Like for a bit as a teenager I got real into the mormon thing, and my dad grew up kind of similarly. Much like he fell out of it
  • Varine Varine:
    Anyway he started coming to church with me for a bit, and they gave home the I think Melchizedek? idk how to spell it, but it's the 'upper' priesthood, like he had a whole blessing thing after I got baptized, and it WAS a very neat experience. And the only reason I think they did they did that was cuz he was like late 30s, and that is kind of the age that they have the last chance at that
  • Varine Varine:
    And I mean its not, but like... I'm pretty well set in my lack of religious beliefs, I guess. And I'm a bit younger than my dad would have been then.
  • Varine Varine:
    And then I got real into, he got bored and was like you do you dude, and that's how I got a huge amount of freedom when I started doing drugs as a teenager
  • Varine Varine:
    In hindsight my parents were fantastic. but they were too fucking tired to raise kids like me. And their jobs were easier with better benefits.
  • Varine Varine:
    Anyway idk where I'm going here, but I asked ChatGPT some of the same things and holy shit was it way more strict and just gave me (importantly the wrong) phone number to a hotline
  • Varine Varine:
    TH, I gotta say, though, I'm amazed you keep this up. I feel like there are you and Ghan doing whatever you're doing, Tom posting news, and then me using the chatbox as a journal every once in a while.
    +1
  • Varine Varine:
    Thanks for letting me do that, also. I know I'm not using it correctly, but somewhere along the line I went from arguing with Cheshire to using this as a kind of void for my frustrations and hopes. I like to think of the whole thing as an enigma of sorts
  • Varine Varine:
    It's not, you guys could probably blackmail me if you went back far enough. I doubt anyone would, you've always been very kind, and I'm glad to have been a part of all this! I hope you enjoy me too, cuz I'm going to keep doing it.
    +1
  • Varine Varine:
    In my defense I was high for a lot of that though
  • V-SNES V-SNES:
    Thanks for sharing @Varine
  • The Helper The Helper:
    Brother how long we been here together? You guys are some of my best friends and I have never even met any of you. I dont know what I would if I did not have my Varine rant to look at when I get on. How could I possibly ever shut this site down? We have been here in this little chat box through a lot of shit for a long time. I love you guys!
    +2
  • tom_mai78101 tom_mai78101:
    I missed out on a lot of chat messages. But yeah, we're planning for my mom's funeral back in Taiwan. I'm really glad I work remotely, so I can try to leverage that and go to places outside of US while still working in US.
    +1
  • tom_mai78101 tom_mai78101:
    If it wasn't for my mom, I wouldn't be able to work in the US. Let alone being able to work until our office closed, and I became a full-time remote employee.
  • tom_mai78101 tom_mai78101:
    Really important though, get your wills and last testaments ready. We avoided like 2 to 3 months of waiting for probate court to get back to us.
    +2
  • The Helper The Helper:
    V-SNES I got good news I got your stuff packed for shipping and just need to get to the post office to get shipping cost!
    +1
  • jonas jonas:
    Oh man Cheshire, I often wonder what happened to her and most of the other people who used to be here
  • V-SNES V-SNES:
    @The Helper that's great! Looking forward to getting them!

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials
      Top