Frozenwind
System maker
- Reaction score
- 99
Mirror system by frozenwind @ Clan CBS @ Northend
1. Introduction.
2. What does this system do?
3. How to implant it.
5. The mathematical theory behind it [warning, pure algebra] --> You don't need to read this part. It's only for the interested people.
1. Introduction
I was working on some map containing a mirror. Units near the mirror got a mirror image at the other side of the mirror. At first I made 4 types of mirrors (horizontal, vertical, diagonal 1 and diagonal 2), all with their own kind of formulas. I made the mirror image move, watching to it’s current position. Trust me, that way sucks. Once your mirror image walks wrong due some bug, it keeps bugging.
2. What does this system do?
In the system I’ve made your mirror image will walk, depending on the position of yourself. Nothing special, true, but my system works for ANY kind of mirror you can think of. In mathematical terms, for every kind of mirror with any slope. For those who don’t know what a slope is, this image might explain: It’s the increase of the y for each x (dy / dx).
You enter a point of anything which you want to reflect to the other side of the mirror. When making an escape like mirror escape you’d enter the coordinates of your escaper. Also you have to enter 2 points on your mirror / line. My system will do the rest. It gives you point U reflected over line L in the point i. (So you enter point U and it gives you point i).
3. How to implant it.
This part of my tutorial about my system might look HUGE, but that is due the images. Most of you won’t need all of the images, but it’s made like that, that everyone should be able to create the trigger without any help by watching to the images. If you need help, just ask!
Please note: the trigger is in the map which I attached.
Firstly, you’ll need to copy my script in the header of your map.
Good job, my system is now implanted in your map, but how do you actually use it?
3.1 Requirements
Before I can tell you how to use my system you’ll firstly need to make some variables.
Now you’ve created the variables, we should create our mirror.
This will be our example mirror with our example units, create regions like I did above.
Ok, we’ve got our mirror and our 2 regions (A and B), now we can create your trigger!
3.2 How to call the system?
Let’s make your trigger now. The example images below are aimed for “mirror escape”. If you don’t make “mirror escape” you’d need another event + condition. Besides your Xu and Yu might need to be set differently. Leave a reply if you don’t understand how to do it in your situation, or PM me.
Now you’ll need to insert a custom script. This is a piece of JASS code within a GUI function. Copy this exactly (when your point variable has the same name as I made):
When your point variable has a different name, you should replace "LEAK_locationvar1" for your point variable name. Think of capitalism!
3.3 Remove leaks.
This isn’t a tutorial about what leaks are, so I won’t explain here why we do this. Just do it. Create a “Custom Script” function and insert the following script code:
When your point variable has a different name, you should replace "LEAK_locationvar1" for your point variable name. Think of capitalism!
Please note: the custom script should be BELOW where you use the variable (to order a mirror image to move for example). Also, it must be ABOVE a “wait” function, else your game will leak.
This trigger isn't completely leak-less yet. In this image below you see the trigger 100% leakless (this leak-less trigger you'll find in the example map).
4. Examples where it can be used for.
Like I’ve mentioned before, you can use my system for mirror escape, which is most common to do for most of you.
You order your escaper to go somewhere and your mirror image is some kind of AI. It automatically goes the point symmetrical to where you're going.
My snippet/system calculates where your mirror image should go, the snippet/system is "the brain" behind your mirror image.
Alternatively you might use it for a cinematic, where some creature watches to itself in a mirror.
I don’t know for what more you can use it, but I’m sure you’ll get a brilliant idea for a new map idea!
This is a part of a GUI trigger code of a map of mine and Sipo (MTC). The function of my system here is similar to mirror escape, but a little bit different.
1)set the 6 global variables Xa, Ya, Xb, Yb, Xu and Yu..
2)call the function which you’ve placed in the header of your map.
3)do something with the function (in my case, order my mirror image to walk).
4)remove eventual leaks which point variables might cause.
download example map
here or
here
or see attachment
5. The mathematical theory behind it [warning, pure algebra] - Reading isn't neccesarily. It's only for the intersted people.
Most of you won’t care, I’m sure about that. But I’m also sure about the fact that some of you might be interested in the theory behind it, to learn from it. Some people believe you shouldn’t accept anything which isn’t proven. In fact, I believe that! So for those who think the same as I do, here is the mathematical theory!
5.1 Horizontal mirror
Let’s start of simple, imagine a horizontal mirror:
Xu = Xm = Xi
(1) Yu = Yi + 2a
(2) a = Ym – Yi
Substitution of (2) in (1) gives
Yu = Yi + 2 ( Ym-Yi )
Yu = Yi + 2 * Ym – 2 * Yi
Yu = -Yi + 2 * Ym
Yi = -Yu + 2 * Ym
So the coordinates of i are the following:
i ( Xu, -Yu + 2* Ym)
This means the coordinates of your mirror image would become the same as coordinate i, when your escaper stands at location U.
5.2 Vertical mirror
As simple as a horizontal mirror, but then the X coordinates varies. Imagine:
Yu = Ym = Yi
(1) Xi = Xu + 2a
(2) a = Xm – Xu
Substitution of (2) in (1) gives
Xi = Xu + 2 ( Xm – Xu)
Xi = Xu + 2 * Xm – 2 * Xu
Xi = - Xu + 2 * Xm
So the coordinates of i are the following:
i (- Xu + 2 * Xm, Yu)
Point i is the reflection of point U over the line (mirror) L.
When your unit stands at point U and watches in the mirror, he sees his mirror image at point i.
5.3 Diagonal mirror with formula y = x
Lets go a little step further, to diagonal mirrors. What is so special on diagonal mirrors? Both the Xi as the Yi are different from the Xu and the Yu. After some calculations you’ll see it’s very easy though!
In the image you can see that the distance between point M and point U is the same as the distance between point M and point i. This fact I’ll use to calculate the coordinates of point i. For those which don’t know the formula, just accept that it is like that:
d(U,L) = d(i,L)
|-Xu + Yu| _ _ _ _ _ _ _ |-Xi + Yi|
------------------- = ----------------
1² + 1²) _ _ _ _
1² + 1²)
Line L and line Ui are perpendicular to each other, so the slope (short: m) of Ui times the slope of L should give “-1” according to the laws of the coordinate system.
m(Ui) × m(L) = -1
m(Ui) = -1 / m(L)
m(Ui) = -1 / 1
(1) m(Ui) = -1
(2) U (Xu, Yu)
(3) Ui: y = ax + b
(1) + (2) + (3) gives
Yu = -Xu + b
(4) Yu + Xu = b
(4) + (3) +(1) gives
y = -x + Yu + Xu
(7)
(5) + (7)V(6) + (7)
-Xi + Yu + Xu = Xi + c V -Xi + Yu + Xu = Xi – c
-2 * Xi = c – Yu – Xu V -2 * Xi = - c – Yu – Xu
(8) Xi = -½ * c + ½ * Yu + ½ * Xu V (8) Xi = ½ * c + ½ * Yu + ½ * Xu
(9) c = -Xu + Yu V (9) c = -Xu + Yu
(8) + (9) V (8) + (9)
Xi = -½ * (-Xu + Yu) + ½ * Yu + ½ * Xu V Xi = ½ * (-Xu + Yu) + ½ * Yu + ½ * Xu
Xi = ½ * Xu – ½ * Yu + ½ * Yu + ½ * Xu V Xi = -½ * Xu + ½ * Yu + ½ * Yu + ½ * Xu
Xi = Xu V Xi = Yu
Wrong coordinate, this is U instead of i V (7) + Xi
V Yi = - (Yu) + Yu + Xu
V Yi = Xu
V i ( Yu, Xu )
Huh? The opposite coordinates of U???
How can that be???
Well, its actually quite logical!
5.4 Diagonal mirror with formula y = x + b
When you though 5.3 was hard, you better quit reading now, because now it even gets harder. I’m going to imagine myself a coordinate system which actually doesn’t exist. I translate the coordinate system with the origin (0,0) to another point. I make the origin be (Xa, Ya) as shown below:
How can you know b? You can’t! You simply lack information! Or can we…?
What if we make the formula of L: y = x? Simply remove the b?
But then we should do minus b, but we don’t know b!
Well, you can solve this by imagining an imaginary coordinate system.
How much did we translate O? It went from (0,0) to (Xa, Ya). So we did a translation of (Xa, Ya). So if you want to go from a value on the imaginary coordinate system to a value on the original coordinate system you’d need to do the opposite translation, so (-Xa, -Ya).
Well, lets take a look at our imaginary coordinate system, with his unique coordinates.
We’ve now got a y = x line, a coordinate U’, a coordinate i’. Now we can use the tactic we’ve learned in §5.3. Once we know i’, we should go back to the original i on the original coordinate system though. Let i’(Xi2, Yi2). When calculating the coordinates of i’, we’ll get both Xi2 as Yi2. Afterwards we translate i’ by (Xa, Ya) so we got the original i fitting the coordinates i (Xi2 + Xa, Yi2 + Ya). Let’s calculate i’!
d(U’, L) = d(i’, L)
|-(Xu-Xa) + (Yu – Ya)| _ _ _ _ _ _ |-Xi2 + Yi2|
------------------------------ = -------------------
1² + 1²) _ _ _ _ _ _ _ _ _ _ _ _
1² + 1²)
|-Xu + Xa + Yu – Ya| = |-Xi2 + Yi2| Let -Xu + Xa + Yu – Ya = c -Xi2 + Yi2 = -c V -Xi2 + Yi2 = c (4) Yi2 = Xi2 - c V This is coordinate U V Like we’ve learned before (positive c)
(1) U2i2: y = - x + b (same slope as at §5.3 (perpendicular))
(2) U2 (Xu – Xa, Yu – Ya)
(1) + (2)
Yu - Ya = - (Xu – Xa) + b
b = Yu – Ya + Xu – Xa
(3) U2i2 : y = -x + (Yu – Ya + Xu – Xa)
(3) + (4)
-Xi2 + (Yu-Ya+Xu-Xa) = Xi2 - c
-2 * Xi2 = -(Yu – Ya + Xu – Xa) – c
Xi2 = ½ (Yu – Ya + Xu – Xa) + ½ * c
c = -Xu + Xa + Yu – Ya, so
Xi2 = ½ Yu – ½ Ya + ½ Xu – ½ Xa + ½ (-Xu + Xa + Yu – Ya)
Xi2 = ½ Yu – ½ Ya + ½ Xu – ½ Xa - ½ -Xu + ½ Xa + ½ Yu – ½ Ya
(5) Xi2 = Yu – Ya
(3) + (5)
Yi2 = - (Yu – Ya) + (Yu – Ya + Xu – Xa)
Yi2 = -Yu + Ya + Yu – Ya + Xu – Xa
Yi2 = Xu – Xa
i’ (Yu – Ya, Xu – Xa)
i (Xi2 + Xa , Yi2 + Ya)
i (Yu – Ya + Xa, Xu – Xa + Ya)
So, here is our i coordinate for every mirror matching the formula y = x + b.
This is quite advanced already, can imagine with a variable slope?
5.5 Diagonal mirror with formula y = mx + b
Alright, here we are. You’ve learned enough to go to the advanced algebraic rules of the coordinate system. We’ll now take a variable slope which will cause some problems. Well, first imagine yourself a coordinate system with a line y = mx + b.
Well, before we watch to our slope we’ll eliminate the b first, like we did at §5.4.
As some of you might see, it is impossible to determine the slope, we’ll need another point on our mirror to detect it.
All right, now we got our slope! m = (Yb – Ya) / (Xb – Xa)! It seems like we know all that we need, so let’s try to calculate i’.
d(U’, L’) = d(I’, L’)
|-m(Xu-Xa) + (Yu – Ya)| _ _ _ _ _ _ |-m(Xi2) + (Yi2)|
------------------------------ = -----------------------
1² + (-m)²) _ _ _ _ _ _ _ _ _ _ _
1² + (-m)²)
|-m(Xu-Xa) + (Yu – Ya)| = |-m(Xi2) + (Yi2)| Let: c = -m(Xu-Xa) + (Yu – Ya) -m(Xi2) + (Yi2) = -c V -m(Xi2) + (Yi2) = c (4) Yi2 = m * Xi2 – c V This is point U’ again, like I’ve explained before.
Line L and line Ui are perpendicular to each other, so the slope (short: m) of Ui times the slope of L should give “-1” according to the laws of the coordinate system.
m(U2i2) × m(L) = -1
m(U2i2) = -1 / m(L)
m(U2i2) = -1 / m
(1) U2i2: y = - (1/m)*x + b
(2) U2 (Xu – Xa, Yu – Ya)
(1) + (2)
Yu – Ya = - (1/m) * (Xu – Xa) + b
Yu – Ya = - ((Xu-Xa)/m) + b
Yu – Ya + ((Xu – Xa)/m) = b
(3) U2i2: y = - (1/m)*x + (Yu – Ya + ((Xu – Xa)/m))
(3) + (4)
- (1/m) * Xi2 + Yu – Ya + ((Xu – Xa)/m) = m * Xi2 – c
- (1/m + m) * Xi2 = - (Yu - Ya + ((Xu – Xa)/m) + c)
Xi2 = (Yu - Ya + ((Xu – Xa)/m) + c) / (1/m + m)
c = -m(Xu-Xa) + (Yu – Ya), so
Xi2 = (Yu - Ya + ((Xu – Xa)/m) + (-m(Xu-Xa) + (Yu – Ya))) / (1/m + m)
Xi2 = (Yu - Ya + ((Xu – Xa)/m) - m(Xu-Xa) + (Yu – Ya)) / (1/m + m)
Xi2 = (2*Yu – 2*Ya + ((Xu – Xa)/m) - m(Xu-Xa)) / (1/m + m)
Xi2 = (2*Yu – 2*Ya + ((Xu – Xa)* (1/m)) - m(Xu-Xa)) / (1/m + m)
Let Xu – Xa = p
Xi2 = (2*Yu – 2*Ya + (1/m) * p – m * p) / (1/m + m)
Xi2 = (2*Yu – 2*Ya + ((1/m) – m) * p) / (1/m + m)
Xi2 = (2*(Yu – Ya) + ((1/m) – m) * p) / (1/m + m)
(5) Xi2 = (2*(Yu – Ya) + ((1/m) – m) * (Xu-Xa)) / (1/m + m)
(4) + (5)
Yi2 = m * (2*(Yu – Ya) + ((1/m) – m) * (Xu-Xa)) / (1/m + m)) – (-m(Xu-Xa) + (Yu – Ya))
Yi2 = m * (2*(Yu – Ya) + ((1/m) – m) * (Xu-Xa)) / (1/m + m)) + m(Xu-Xa) - (Yu – Ya)
Yi2 = (m*2*(Yu – Ya) + m*((1/m) – m) * (Xu-Xa)) / (1/m + m)) + m(Xu-Xa) - Yu + Ya
i ( Xi2 + Xa , Yi2 + Ya)
Xi = (2*(Yu – Ya) + ((1/m) – m) * (Xu-Xa)) / (1/m + m) + Xa
Yi = (m*2*(Yu – Ya) + m*((1/m) – m) * (Xu-Xa)) / (1/m + m)) + m(Xu-Xa) - Yu + Ya + Ya
Yi = (m*2*(Yu – Ya) + m*((1/m) – m) * (Xu-Xa)) / (1/m + m)) + m(Xu-Xa) - Yu + 2* Ya
And here are our final formulas, like you’ll find them in my system! Although… there is a small problem:
5.6 Exceptional mirror
Life wouldn’t be life if there weren’t any exceptions.
Let’s take a look at this formula:
m = (Yb – Ya) / (Xb – Xa)
I’m sure all if you know that you can’t divide by zero.
So what if Xb – Xa is zero? Then your game would crash!
What kind of mirror do we have when the X coordinate of B is equal to the X coordinate of A. Same X… so they’re on a vertical line.
So let’s use the formula we’ve created at §5.2 in case (Xa – Xb = 0).
Now, let’s take a look at this part of a formula (at Xi & Yi):
1/m + m
We divide 1 by m, but what if m = 0? Then our game would be crashing! Let’s take a look at the formula of m:
m = (Yb – Ya) / (Xb – Xa)
so if m = 0 you’d get:
(Yb – Ya) / (Xb – Xa) = 0
Yb – Ya = 0
Yb = Ya
So if the Y coordinate of B is the same as the Y coordinate of A you’re game would be crashing with the formula. Same Y coordinate means we’ve got a horizontal mirror. Same story as with the vertical mirror, we add an exception to our function.
There is another exception, but we don’t need to watch to it. That exception lies in the Xi and Yi formula’s. That exception is same story as a horizontal mirror, so it’s already listed in our exceptions.
6. Credits
Please give +rep / credits when you use this in your map.
This system was created by frozenwind / FrozenCure @ Clan CBS @ Northend.
1. Introduction.
2. What does this system do?
3. How to implant it.
3.1 Requirements
3.2 How to call the system?
3.3 Remove leaks
4. Examples where it can be used for + download5. The mathematical theory behind it [warning, pure algebra] --> You don't need to read this part. It's only for the interested people.
5.1 Horizontal mirror
5.2 Vertical mirror
5.3 Diagonal mirror with formula y = x
5.4 Diagonal mirror with formula y = x + b
5.5 Diagonal mirror with formula y = mx + b
5.6 Exceptional mirror
6. Credits1. Introduction
I was working on some map containing a mirror. Units near the mirror got a mirror image at the other side of the mirror. At first I made 4 types of mirrors (horizontal, vertical, diagonal 1 and diagonal 2), all with their own kind of formulas. I made the mirror image move, watching to it’s current position. Trust me, that way sucks. Once your mirror image walks wrong due some bug, it keeps bugging.
2. What does this system do?
In the system I’ve made your mirror image will walk, depending on the position of yourself. Nothing special, true, but my system works for ANY kind of mirror you can think of. In mathematical terms, for every kind of mirror with any slope. For those who don’t know what a slope is, this image might explain: It’s the increase of the y for each x (dy / dx).
You enter a point of anything which you want to reflect to the other side of the mirror. When making an escape like mirror escape you’d enter the coordinates of your escaper. Also you have to enter 2 points on your mirror / line. My system will do the rest. It gives you point U reflected over line L in the point i. (So you enter point U and it gives you point i).
3. How to implant it.
This part of my tutorial about my system might look HUGE, but that is due the images. Most of you won’t need all of the images, but it’s made like that, that everyone should be able to create the trigger without any help by watching to the images. If you need help, just ask!
Please note: the trigger is in the map which I attached.
Firstly, you’ll need to copy my script in the header of your map.
JASS:
//******************************************************************************************************************
//******************************************************************************************************************
//*********** *************
//*********** Mirror determining system *************
//*********** Reflect point U over de line L in the point I *************
//*********** *************
//*********** For ANY kind of slope! *************
//*********** *************
//*********** *************
//*********** *************
//*********** Requirements: *************
//*********** *************
//*********** - Global real variable Yu *************
//*********** - Global real variable Ya *************
//*********** - Global real variable Yb *************
//*********** - Global real variable Xu *************
//*********** - Global real variable Xa *************
//*********** - Global real variable Xb *************
//*********** - My function in the header of your map *************
//*********** *************
//*********** *************
//*********** *************
//*********** Vocabulary: *************
//*********** *************
//*********** Point U = The point that will be reflected over line L *************
//*********** In terms of mirror escape this would be your escaper *************
//*********** Point A = A random point on your mirror *************
//*********** Point B = A random point on your mirror, being not equal to point A *************
//*********** *************
//*********** *************
//*********** *************
//*********** How the implant it? *************
//*********** *************
//*********** Place this system in the header of your map *************
//*********** *************
//*********** Set global variable Yu as the Y coordinate of point U *************
//*********** Set global variable Xu as the X coordinate of point U *************
//*********** Set global variable Ya as the Y coordinate of point A *************
//*********** Set global variable Xa as the X coordinate of point A *************
//*********** Set global variable Yb as the Y coordinate of point B *************
//*********** Set global variable Xb as the X coordinate of point B *************
//*********** *************
//*********** Copy the following triggerfunction in a custom script somewhere at your triggers. *************
//*********** set udg_>locationvarname< = mirrordeterminer (udg_Yu,udg_Ya,udg_Yb,udg_Xu,udg_Xa,udg_Xb) *************
//*********** Point I now is stored in your global variable >locationvarname< *************
//*********** Now you can let your mirror image (for example) walk to >locationvarname< *************
//*********** *************
//*********** *************
//*********** GIVE CREDITS WHEN YOU USE THIS IN YOUR MAP! *************
//*********** *************
//*********** *************
//*********** *************
//*********** This system was created by ©frozenwind / ©FrozenCure *************
//*********** *************
//*********** Clan CBS @ Northend *************
//*********** *************
//******************************************************************************************************************
//******************************************************************************************************************
//*********** *************
//*********** START OF TRIGGER CODE *************
//*********** *************
//******************************************************************************************************************
//******************************************************************************************************************
function mirrordeterminer takes real Yu, real Ya, real Yb, real Xu, real Xa, real Xb returns location
local real a
local real Xi
local real Yi
if Xb-Xa == 0 then //VERTICAL MIRROR
set Xi = -1 * Xu + 2 * Xa
set Yi = Yu
else
set a = ((Yb-Ya)/(Xb-Xa))
if a == 0 then //HORIZONTAL MIRROR
set Xi = Xu
set Yi = -1 * Yu + 2 * Ya
else //DIAGONAL MIRROR
set Xi = (( 2* (Yu-Ya) + ((1/a) - a) * (Xu-Xa) ) / ((1/a) + a) ) + Xa
set Yi = ((2*a*(Yu-Ya) + (1- (a*a)) * (Xu-Xa)) / ((1/a) +a)) + a * (Xu-Xa) - Yu + 2 * Ya
endif
endif
set a = 0
return Location(Xi, Yi)
endfunction
//******************************************************************************************************************
//******************************************************************************************************************
//*********** *************
//*********** END OF TRIGGER CODE *************
//*********** *************
//*********** Mirror determining system by ©frozenwind / ©FrozenCure *************
//*********** *************
//******************************************************************************************************************
//******************************************************************************************************************
Good job, my system is now implanted in your map, but how do you actually use it?
3.1 Requirements
Before I can tell you how to use my system you’ll firstly need to make some variables.
Now you’ve created the variables, we should create our mirror.
This will be our example mirror with our example units, create regions like I did above.
Ok, we’ve got our mirror and our 2 regions (A and B), now we can create your trigger!
3.2 How to call the system?
Let’s make your trigger now. The example images below are aimed for “mirror escape”. If you don’t make “mirror escape” you’d need another event + condition. Besides your Xu and Yu might need to be set differently. Leave a reply if you don’t understand how to do it in your situation, or PM me.
Now you’ll need to insert a custom script. This is a piece of JASS code within a GUI function. Copy this exactly (when your point variable has the same name as I made):
JASS:
set udg_LEAK_locationvar1 = mirrordeterminer (udg_Yu,udg_Ya,udg_Yb,udg_Xu,udg_Xa,udg_Xb)
When your point variable has a different name, you should replace "LEAK_locationvar1" for your point variable name. Think of capitalism!
3.3 Remove leaks.
This isn’t a tutorial about what leaks are, so I won’t explain here why we do this. Just do it. Create a “Custom Script” function and insert the following script code:
JASS:
call RemoveLocation (udg_LEAK_locationvar1)
When your point variable has a different name, you should replace "LEAK_locationvar1" for your point variable name. Think of capitalism!
Please note: the custom script should be BELOW where you use the variable (to order a mirror image to move for example). Also, it must be ABOVE a “wait” function, else your game will leak.
This trigger isn't completely leak-less yet. In this image below you see the trigger 100% leakless (this leak-less trigger you'll find in the example map).
4. Examples where it can be used for.
Like I’ve mentioned before, you can use my system for mirror escape, which is most common to do for most of you.
You order your escaper to go somewhere and your mirror image is some kind of AI. It automatically goes the point symmetrical to where you're going.
My snippet/system calculates where your mirror image should go, the snippet/system is "the brain" behind your mirror image.
Alternatively you might use it for a cinematic, where some creature watches to itself in a mirror.
I don’t know for what more you can use it, but I’m sure you’ll get a brilliant idea for a new map idea!
This is a part of a GUI trigger code of a map of mine and Sipo (MTC). The function of my system here is similar to mirror escape, but a little bit different.
1)set the 6 global variables Xa, Ya, Xb, Yb, Xu and Yu..
2)call the function which you’ve placed in the header of your map.
3)do something with the function (in my case, order my mirror image to walk).
4)remove eventual leaks which point variables might cause.
download example map
here or
here
or see attachment
5. The mathematical theory behind it [warning, pure algebra] - Reading isn't neccesarily. It's only for the intersted people.
Most of you won’t care, I’m sure about that. But I’m also sure about the fact that some of you might be interested in the theory behind it, to learn from it. Some people believe you shouldn’t accept anything which isn’t proven. In fact, I believe that! So for those who think the same as I do, here is the mathematical theory!
5.1 Horizontal mirror
Let’s start of simple, imagine a horizontal mirror:
Xu = Xm = Xi
(1) Yu = Yi + 2a
(2) a = Ym – Yi
Substitution of (2) in (1) gives
Yu = Yi + 2 ( Ym-Yi )
Yu = Yi + 2 * Ym – 2 * Yi
Yu = -Yi + 2 * Ym
Yi = -Yu + 2 * Ym
So the coordinates of i are the following:
i ( Xu, -Yu + 2* Ym)
This means the coordinates of your mirror image would become the same as coordinate i, when your escaper stands at location U.
5.2 Vertical mirror
As simple as a horizontal mirror, but then the X coordinates varies. Imagine:
Yu = Ym = Yi
(1) Xi = Xu + 2a
(2) a = Xm – Xu
Substitution of (2) in (1) gives
Xi = Xu + 2 ( Xm – Xu)
Xi = Xu + 2 * Xm – 2 * Xu
Xi = - Xu + 2 * Xm
So the coordinates of i are the following:
i (- Xu + 2 * Xm, Yu)
Point i is the reflection of point U over the line (mirror) L.
When your unit stands at point U and watches in the mirror, he sees his mirror image at point i.
5.3 Diagonal mirror with formula y = x
Lets go a little step further, to diagonal mirrors. What is so special on diagonal mirrors? Both the Xi as the Yi are different from the Xu and the Yu. After some calculations you’ll see it’s very easy though!
In the image you can see that the distance between point M and point U is the same as the distance between point M and point i. This fact I’ll use to calculate the coordinates of point i. For those which don’t know the formula, just accept that it is like that:
d(U,L) = d(i,L)
|-Xu + Yu| _ _ _ _ _ _ _ |-Xi + Yi|
------------------- = ----------------
Line L and line Ui are perpendicular to each other, so the slope (short: m) of Ui times the slope of L should give “-1” according to the laws of the coordinate system.
m(Ui) × m(L) = -1
m(Ui) = -1 / m(L)
m(Ui) = -1 / 1
(1) m(Ui) = -1
(2) U (Xu, Yu)
(3) Ui: y = ax + b
(1) + (2) + (3) gives
Yu = -Xu + b
(4) Yu + Xu = b
(4) + (3) +(1) gives
y = -x + Yu + Xu
(7)
(5) + (7)V(6) + (7)
-Xi + Yu + Xu = Xi + c V -Xi + Yu + Xu = Xi – c
-2 * Xi = c – Yu – Xu V -2 * Xi = - c – Yu – Xu
(8) Xi = -½ * c + ½ * Yu + ½ * Xu V (8) Xi = ½ * c + ½ * Yu + ½ * Xu
(9) c = -Xu + Yu V (9) c = -Xu + Yu
(8) + (9) V (8) + (9)
Xi = -½ * (-Xu + Yu) + ½ * Yu + ½ * Xu V Xi = ½ * (-Xu + Yu) + ½ * Yu + ½ * Xu
Xi = ½ * Xu – ½ * Yu + ½ * Yu + ½ * Xu V Xi = -½ * Xu + ½ * Yu + ½ * Yu + ½ * Xu
Xi = Xu V Xi = Yu
Wrong coordinate, this is U instead of i V (7) + Xi
V Yi = - (Yu) + Yu + Xu
V Yi = Xu
V i ( Yu, Xu )
Huh? The opposite coordinates of U???
How can that be???
Well, its actually quite logical!
5.4 Diagonal mirror with formula y = x + b
When you though 5.3 was hard, you better quit reading now, because now it even gets harder. I’m going to imagine myself a coordinate system which actually doesn’t exist. I translate the coordinate system with the origin (0,0) to another point. I make the origin be (Xa, Ya) as shown below:
How can you know b? You can’t! You simply lack information! Or can we…?
What if we make the formula of L: y = x? Simply remove the b?
But then we should do minus b, but we don’t know b!
Well, you can solve this by imagining an imaginary coordinate system.
How much did we translate O? It went from (0,0) to (Xa, Ya). So we did a translation of (Xa, Ya). So if you want to go from a value on the imaginary coordinate system to a value on the original coordinate system you’d need to do the opposite translation, so (-Xa, -Ya).
Well, lets take a look at our imaginary coordinate system, with his unique coordinates.
We’ve now got a y = x line, a coordinate U’, a coordinate i’. Now we can use the tactic we’ve learned in §5.3. Once we know i’, we should go back to the original i on the original coordinate system though. Let i’(Xi2, Yi2). When calculating the coordinates of i’, we’ll get both Xi2 as Yi2. Afterwards we translate i’ by (Xa, Ya) so we got the original i fitting the coordinates i (Xi2 + Xa, Yi2 + Ya). Let’s calculate i’!
d(U’, L) = d(i’, L)
|-(Xu-Xa) + (Yu – Ya)| _ _ _ _ _ _ |-Xi2 + Yi2|
------------------------------ = -------------------
|-Xu + Xa + Yu – Ya| = |-Xi2 + Yi2| Let -Xu + Xa + Yu – Ya = c -Xi2 + Yi2 = -c V -Xi2 + Yi2 = c (4) Yi2 = Xi2 - c V This is coordinate U V Like we’ve learned before (positive c)
(1) U2i2: y = - x + b (same slope as at §5.3 (perpendicular))
(2) U2 (Xu – Xa, Yu – Ya)
(1) + (2)
Yu - Ya = - (Xu – Xa) + b
b = Yu – Ya + Xu – Xa
(3) U2i2 : y = -x + (Yu – Ya + Xu – Xa)
(3) + (4)
-Xi2 + (Yu-Ya+Xu-Xa) = Xi2 - c
-2 * Xi2 = -(Yu – Ya + Xu – Xa) – c
Xi2 = ½ (Yu – Ya + Xu – Xa) + ½ * c
c = -Xu + Xa + Yu – Ya, so
Xi2 = ½ Yu – ½ Ya + ½ Xu – ½ Xa + ½ (-Xu + Xa + Yu – Ya)
Xi2 = ½ Yu – ½ Ya + ½ Xu – ½ Xa - ½ -Xu + ½ Xa + ½ Yu – ½ Ya
(5) Xi2 = Yu – Ya
(3) + (5)
Yi2 = - (Yu – Ya) + (Yu – Ya + Xu – Xa)
Yi2 = -Yu + Ya + Yu – Ya + Xu – Xa
Yi2 = Xu – Xa
i’ (Yu – Ya, Xu – Xa)
i (Xi2 + Xa , Yi2 + Ya)
i (Yu – Ya + Xa, Xu – Xa + Ya)
So, here is our i coordinate for every mirror matching the formula y = x + b.
This is quite advanced already, can imagine with a variable slope?
5.5 Diagonal mirror with formula y = mx + b
Alright, here we are. You’ve learned enough to go to the advanced algebraic rules of the coordinate system. We’ll now take a variable slope which will cause some problems. Well, first imagine yourself a coordinate system with a line y = mx + b.
Well, before we watch to our slope we’ll eliminate the b first, like we did at §5.4.
As some of you might see, it is impossible to determine the slope, we’ll need another point on our mirror to detect it.
All right, now we got our slope! m = (Yb – Ya) / (Xb – Xa)! It seems like we know all that we need, so let’s try to calculate i’.
d(U’, L’) = d(I’, L’)
|-m(Xu-Xa) + (Yu – Ya)| _ _ _ _ _ _ |-m(Xi2) + (Yi2)|
------------------------------ = -----------------------
|-m(Xu-Xa) + (Yu – Ya)| = |-m(Xi2) + (Yi2)| Let: c = -m(Xu-Xa) + (Yu – Ya) -m(Xi2) + (Yi2) = -c V -m(Xi2) + (Yi2) = c (4) Yi2 = m * Xi2 – c V This is point U’ again, like I’ve explained before.
Line L and line Ui are perpendicular to each other, so the slope (short: m) of Ui times the slope of L should give “-1” according to the laws of the coordinate system.
m(U2i2) × m(L) = -1
m(U2i2) = -1 / m(L)
m(U2i2) = -1 / m
(1) U2i2: y = - (1/m)*x + b
(2) U2 (Xu – Xa, Yu – Ya)
(1) + (2)
Yu – Ya = - (1/m) * (Xu – Xa) + b
Yu – Ya = - ((Xu-Xa)/m) + b
Yu – Ya + ((Xu – Xa)/m) = b
(3) U2i2: y = - (1/m)*x + (Yu – Ya + ((Xu – Xa)/m))
(3) + (4)
- (1/m) * Xi2 + Yu – Ya + ((Xu – Xa)/m) = m * Xi2 – c
- (1/m + m) * Xi2 = - (Yu - Ya + ((Xu – Xa)/m) + c)
Xi2 = (Yu - Ya + ((Xu – Xa)/m) + c) / (1/m + m)
c = -m(Xu-Xa) + (Yu – Ya), so
Xi2 = (Yu - Ya + ((Xu – Xa)/m) + (-m(Xu-Xa) + (Yu – Ya))) / (1/m + m)
Xi2 = (Yu - Ya + ((Xu – Xa)/m) - m(Xu-Xa) + (Yu – Ya)) / (1/m + m)
Xi2 = (2*Yu – 2*Ya + ((Xu – Xa)/m) - m(Xu-Xa)) / (1/m + m)
Xi2 = (2*Yu – 2*Ya + ((Xu – Xa)* (1/m)) - m(Xu-Xa)) / (1/m + m)
Let Xu – Xa = p
Xi2 = (2*Yu – 2*Ya + (1/m) * p – m * p) / (1/m + m)
Xi2 = (2*Yu – 2*Ya + ((1/m) – m) * p) / (1/m + m)
Xi2 = (2*(Yu – Ya) + ((1/m) – m) * p) / (1/m + m)
(5) Xi2 = (2*(Yu – Ya) + ((1/m) – m) * (Xu-Xa)) / (1/m + m)
(4) + (5)
Yi2 = m * (2*(Yu – Ya) + ((1/m) – m) * (Xu-Xa)) / (1/m + m)) – (-m(Xu-Xa) + (Yu – Ya))
Yi2 = m * (2*(Yu – Ya) + ((1/m) – m) * (Xu-Xa)) / (1/m + m)) + m(Xu-Xa) - (Yu – Ya)
Yi2 = (m*2*(Yu – Ya) + m*((1/m) – m) * (Xu-Xa)) / (1/m + m)) + m(Xu-Xa) - Yu + Ya
i ( Xi2 + Xa , Yi2 + Ya)
Xi = (2*(Yu – Ya) + ((1/m) – m) * (Xu-Xa)) / (1/m + m) + Xa
Yi = (m*2*(Yu – Ya) + m*((1/m) – m) * (Xu-Xa)) / (1/m + m)) + m(Xu-Xa) - Yu + Ya + Ya
Yi = (m*2*(Yu – Ya) + m*((1/m) – m) * (Xu-Xa)) / (1/m + m)) + m(Xu-Xa) - Yu + 2* Ya
And here are our final formulas, like you’ll find them in my system! Although… there is a small problem:
5.6 Exceptional mirror
Life wouldn’t be life if there weren’t any exceptions.
Let’s take a look at this formula:
m = (Yb – Ya) / (Xb – Xa)
I’m sure all if you know that you can’t divide by zero.
So what if Xb – Xa is zero? Then your game would crash!
What kind of mirror do we have when the X coordinate of B is equal to the X coordinate of A. Same X… so they’re on a vertical line.
So let’s use the formula we’ve created at §5.2 in case (Xa – Xb = 0).
Now, let’s take a look at this part of a formula (at Xi & Yi):
1/m + m
We divide 1 by m, but what if m = 0? Then our game would be crashing! Let’s take a look at the formula of m:
m = (Yb – Ya) / (Xb – Xa)
so if m = 0 you’d get:
(Yb – Ya) / (Xb – Xa) = 0
Yb – Ya = 0
Yb = Ya
So if the Y coordinate of B is the same as the Y coordinate of A you’re game would be crashing with the formula. Same Y coordinate means we’ve got a horizontal mirror. Same story as with the vertical mirror, we add an exception to our function.
There is another exception, but we don’t need to watch to it. That exception lies in the Xi and Yi formula’s. That exception is same story as a horizontal mirror, so it’s already listed in our exceptions.
6. Credits
Please give +rep / credits when you use this in your map.
This system was created by frozenwind / FrozenCure @ Clan CBS @ Northend.