• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • ✅ The POLL for Hive's Texturing Contest #33 is OPEN! Vote for the TOP 3 SKINS! 🔗Click here to cast your vote!

Math help - Find X,Y component

Status
Not open for further replies.
Level 7
Joined
Nov 19, 2015
Messages
283
Hi, I'm creating a knockback system. I have multiple knockback spells and want them to add up to each other. Please let me know if there is a better way of doing this but I decided to break it up into X and Y movement.

I haven't done any proper maths for a while now and I want to know how to break down a vector into its components.

Example: I have a knockback distance of n*SQRT(2) at an angle of 45 degrees would have an X distance of n and a Y distance of n.

So given the angle and a distance, I need its X and Y components. Unless someone has a better knockback system that allows units to be knocked around by multiple sources.
 
Level 24
Joined
Aug 1, 2013
Messages
4,658
It is always easy to create the function in GUI and then convert it to custom text to find out how it works:
JASS:
function PolarProjectionBJ takes location source, real dist, real angle returns location
    local real x = GetLocationX(source) + dist * Cos(angle * bj_DEGTORAD)
    local real y = GetLocationY(source) + dist * Sin(angle * bj_DEGTORAD)
    return Location(x, y)
endfunction

As you can see, this creates 2 coordinates (x, y) with a certain distance towards a certain angle.
The calculations are (StartingX + distance*CosBJ(angle)) and (StartingY + distance*SinBJ(angle)).

JASS:
    set x_v = speed * Cos(angle * bj_DEGTORAD)
    set y_v = speed * Sin(angle * bj_DEGTORAD)
(When you use radians instead of degrees, you can remove the "* bj_DEGTORAD" stuff.
Also, the "speed" used in this calculation is the speed per interval and not the speed per second.)

You can use those calculations to set the X-Velocity and Y-Velocity and use those to move the unit.
It is a nice way of doing it and it saves huge processing time when you just store X-V, Y-V (and Z-V) in their own variables.
However, it is a pain to use, so you might need some functions to make it easier:
JASS:
function SetMissileSpeed takes unit missile, real speedPerSecond returns nothing
function SetMissileAngle takes unit missile, real angleDegrees returns nothing
function GetMissileSpeed takes unit missile returns real
function GetMissileAngle takes unit missile returns real
(Or whatever you call these functions.)
Ofcourse, how you do it, you can think of yourself (I guess) ;)
 
Level 7
Joined
Nov 19, 2015
Messages
283
Thanks for that, my maths is really rusty lol.

I just found this knockback system. Do you know if it does what I want?
The test map is too congested and messy so I can't tell if a unit recieves a knockback from different sources will combine. The code is too complex for me to understand.

http://www.hiveworkshop.com/forums/spells-569/knockback-v1-8-v1-2-a-223634/?prev=status%3Da%26u%3DAlmia
or
http://www.hiveworkshop.com/forums/spells-569/gui-knockback-2-5d-203180/

I've personally used Bribe's knockback system and love it. The only downside I guess is that it requires his Unit Indexer, which isn't that much of a downside unless you are using Custom value of unit for something else in your project.

Both maps have documentation and example triggers for their systems. How is the code too complex? You need at least minimal GUI experience to properly use it. Considering that you tried to make your own knockback system, I'm sure that you can at the very least read the documentation and look at the example trigger.
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,239
Any form of instance based knockback system should work. The result of multiple knockback instances running on a single unit should produce the desired results. This will potentially be quite inefficient due to each knockback result being computed and applied separately.

A more efficient solution might map knockback instances onto a list on a per unit basis. This list is then iterated fully to compute the knockback which is then applied only once per unit. Depending on mechanics this could allow two knockback instances in opposite direction with on collision effects to mostly cancel each other out instead of applying the on collision effects from both in a rather random way.
 
Last edited:
Level 24
Joined
Aug 1, 2013
Messages
4,658
The collision will most probably indeed be done in a circle on the locations where they would be after they were moved, which means that one of the checks is done on his location + offset (depending on the speed of the knockback) and the second be applied on the location where the unit was before the movement... as long as both knockbacks are equally as strong.

In that perspective, merging the movements is better to avoid any unexpected results next to the efficiency, but I think that in most cases, this doesn't even make any difference at all.
 
Level 7
Joined
Nov 19, 2015
Messages
283
A more efficient solution might map knockback instances onto a list on a pre unit basis. This list is then iterated fully to compute the knockback which is then applied only once per unit.

I don't get the list on a pre unit basis part.

I love math but I do not understand thia at all lol
Same here, these guys here are too smart for me. I spend more time trying to decipher the replies than creating the triggers. I tried one of the knockback systems with a rifleman of either side but the knockbacks just go in the direction of who shoots second.

I was thinking of having the same speed for all knockbacks to make it simpler. I was wondering if I could just have move a unit by X amount and Y amount. If I just take the X and Y amount from each knock back, they will add up or cancel out each other. Since they are all at the same speed I only need the distance of the maximal knock back from each source. And find the X distance and Y distance.

I was planning on using Weitlol said in the first post.
X = X + distance * cos(angle)
Y = Y + distance * sin(angle)
 
Status
Not open for further replies.
Top