# SolvedWorld 2 Screen Tutorial

#### VDV777

##### Jr.Coder
Full Member
Nobleman
Help create a class of "right."

The source code of the tutorial below.
C++:
``````bool WorldToScreen(Vector3D World, float *ScreenX, float *ScreenY)
{
//Get the enemy position
Vector3D Position = VectorSubtract(World, RefDef->Origin);
Vector3D Transform;

//Get the Dot Products from the View Angles of the player
Transform.x = DotProduct(Position, RefDef->viewAxis[1]);
Transform.y = DotProduct(Position, RefDef->viewAxis[2]);
Transform.z = DotProduct(Position, RefDef->viewAxis[0]);

//Make sure the enemy is in front of the player. If not, return.
if (Transform.z < 0.1f)
return false;

//Calculate the center of the screen
Vector2D Center = Vector2D((float)RefDef->Width * 0.5f, (float)RefDef->Height * 0.5f);

//Calculates the screen coordinates
*ScreenX = Center.x * (1 - (Transform.x / RefDef->fov.x / Transform.z));
*ScreenY = Center.y * (1 - (Transform.y / RefDef->fov.y / Transform.z));

return true;

}``````
RefDef - whatever address you have that stores your origin, view angles and view axis - how to do this ????
DotProduct - What do you mean?

Maybe someone will come in handy tutorial, but I can not figure out how to do this, please help with the source code.

Before I start, I would like to put forth that I would count Math amoungst my worst subjects - and Geometry amoungst my worst fields in Math. This partially has to do with the fact I took Geometry back when I was in eight-grade, and that, like every thirteen year-old, I was an idiot who did not care about learning. As such, there might be mistakes in the method I am about to put forth; all I know is that it works, and that to my mind, it seems correct. Feel free to mention any mistakes you find.

Consider the following scene; you are the entity marked "P," there is another entity marked "E," and there is the origin named "O:"

Strip away the buildings and all the fancy graphical effects, and this boils down to:

Now knowing this, how are we to translate the entity E's position into a 2D coordinate? Well the first measure is to understand what we actually mean. There is no way to directly translate a 3D coordinate into a 2D coordinate, just as there is no way to directly translate a cube into a square; however, notice the word "directly." Let us continue with the example of a cube - there obviously has to be a way to translate a cube into a square, or at least into a 2D space, otherwise something like:

would simply not be possible. So how do we go about this? Whenever we choose to draw a 3D object on a 2D plane (such as paper), we must first pick a view-port, which is exactly what it sounds like - a static view of the scene with a fixed source. To turn a cube into a square, we simply pick the view-port to be directly facing one of the sides of the cube, as so:

To model the earlier model (the common drawing that is used to represent a cube), we simply place the view-port so that it looks directly at one of the angles:

View attachment 1809

This same idea applies to any F.P.S.'s - in fact, it is central to the notion of them. Most modern F.P.S.'s conceive the illusion that as you move, you move your character around the map; this is simply not true. When "moving," all one is doing is adjusting their view-port in relation to the world. To track entity location then, most games make use of a series of 3D vectors that give the location of an entity relative to the origin of the map.

Knowing this, let us now go back to original representation of our scene and make it more accurate:

If we eliminate the Y-axis in our problem (we will assume the map is constantly flat), we can take a top down view of the above image, and greatly simplify our task:

View attachment 1811

Given that these two entities could be at any location on the map (even though we assume they are always in Quadrant I), it makes sense to stop trying to focus the scene around the map's origin, and instead makes sense to allow our entity P to be the origin - we can do this by subtracting our origin vector and the enemy's origin vector. The result will be the absolute distance of the entity E from the entity P.

Assume vAbsDistance is a three dimensional vector. Let vAbsDistance be equal to:

In relation to operations on vectors, let:

The vector vAsbDistance will now be a vector from the origin to an object we will call E' (E prime):

Now let us incorporate our view-port. Most games make use of either trio of angles that represent the yaw, pitch, and roll of a player's view-port relative to some origin (usually facing directly North or South, East or West, directly above or directly below) or a trio of 3D vectors that represent the same thing, but in vector format. For us, working with vectors is the easiest way for us to complete our task - luckily, these view-angles can be translated to vectors with some math.

* Call of Duty 4 uses both of these methods; they are held in refdef->ViewAngles (a vec3_t) and refdef->ViewAxis (an array of vec3_t's) respectively. *

* A vec3_t is nothing more than float[ 3 ]. *

Imposing the vectors that represent the view-port, and assuming we are looking at the origins, we receive:

View attachment 1813

By eliminating the fluff, and separating the vectors, we are left with two vector compliments:

View attachment 1814

Enter the Dot Product. If we let A and B be two vectors, than the Dot Product is nothing more than ( A.x * B.x ) + ( A.y * B.y ) + ( A.z * B.z ). However, this value has some interesting attributes:

Let A and B be two vectors, and let |A| and |B| be the length of A and B respectively, and let |A| and |B| not equal 0. Then:

Where theta is the angle represented by the image below:

In our current example, this will give us the value that will represent the distance E' is away from our view-vector. As such, we will take the Dot Product of both sets of vectors, and store them appropriately.

Assume vRightTransform and vForwardTransform are two floats. Let these equal:

C++:
``````vRightTransform = vAbsDistance . vRightView
vForwardTransform = vAbsDistance . vForwardView``````
Now we quickly need to take a diversion to account for something - the Dot Product is signed operation, meaning it will work regardless of whether or not the vector is negative. Consider the case:

In our following equation to transform our values to a 2D coordinate, we assume vForwardTransform is positive; as such, if the enemy is behind us, then our equation will give us the same value as if he was in front of us. To account for this fact, we will need to ensure that vForwardTransform is positive before continuing! Keep this in mind for later, when we code this!

We are on to the final steps - let us now consider our problem focusing on our view-port:

In a picture I have now lost the link to, it was explained that X is equal to the result of:

This of course can be reduced to:
C++:
``X = C + [ (C * X') / (Z'* FOV) ]``
We, of course, have X' and Z' as result of our operations with the Dot Product - in addition, C and FOV are held in refdef->Width / 2 and refdef->FOVx respectively. Letting centerX equal refdef->Width / 2, this becomes:
C++:
``X = centerX +(( centerX * vRightTransform )/( vForwardTransform * refdef->FOVx))``
As such, we have concluded our series of steps to get our 2D coordinate (in the X-Axis, at least):

C++:
``````vAbsDistance = vEnemy - vPlayer

vRightTransform   = vAbsDistance . vRightView
vForwardTransform = vAbsDistance . vForwardView

if( vForwardTransform < 0)
return

Let centerX = refdef->Width/2

X = centerX +(( centerX * vRightTransform )/( vForwardTransform * refdef->FOVx))``````
tutorial is copy \ past

p.s. You tell Improve knowledge c + +, but this is not taught anywhere else. Hacks into love. want to make hacks. Please help with the source code.

Last edited:

#### VDV777

##### Jr.Coder
Full Member
Nobleman
Vector 3d - not understand it, hike x, y, z enemy - x, y, z my
VectorSubtract - do not understand the difference between a campaign 2x vectors: the enemy of my
DotProduct - x * y * z or something like that, too, it is not clear
RefDef - it is a separate class that contains the coordinates, angle and so forth, but also fully understand that as a yes
viewAxis - view along the axis - how to properly calculate not understand, can anyone help
Vector2D - I do not know how to calculate

#### c5

##### Kim Kong Trasher
Dank Tier VIP
Dank Tier Donator
How about you go and get your linear algebra straight, this ain't no math class here mate. Should've paid more attention in school

PS: Most of the functions purposes used here are very well explained on the web, just a little effort of google and you've got them all. Don't expect to get your W2S working straight off the bat, start with radars or something more simple if you are not that used to math.

#### VDV777

##### Jr.Coder
Full Member
Nobleman
How about you go and get your linear algebra straight, this ain't no math class here mate. Should've paid more attention in school

PS: Most of the functions purposes used here are very well explained on the web, just a little effort of google and you've got them all. Don't expect to get your W2S working straight off the bat, start with radars or something more simple if you are not that used to math.
Mathematics nothing to do with, you know what to expect ...

#### c5

##### Kim Kong Trasher
Dank Tier VIP
Dank Tier Donator
Mathematics nothing to do with, you know what to expect ...
It's all about mathematics.. And as said, there's no 100% universal method for doing this, it's up to you to adapt these different methods onto your games.

Last edited:

#### VDV777

##### Jr.Coder
Full Member
Nobleman
It's all about mathematics.. And as said, there's no 100% universal method for doing this, it's up to you to adapt these different methods onto your games.
what is the Vector? it is a distance from of point A to Point B (A*B) - is right?
what is the Vector 3d?? It is a distance of (from point X to point X1(X*X1)) + (from point Y to point Y1(Y*Y1)) + (from point Z to point Z1(Z*Z1)) - is right?

#### Syperus

##### RTFM
Meme Tier VIP
Dank Tier Donator
Mathematics nothing to do with, you know what to expect ...
c5 is right. It's all mathematics. Even PocketMonster told you it was mathematics here. Read up on some Algebra. Not to beat a dead horse here, but pretty much everything in this world can be narrowed down to mathematics. Math is very important, especially if you're going to be programming. Good Luck!

#### xploiitz

##### Coder
Fleep Tier Donator
Trump Tier Donator
Nobleman
This part of an aimbot or ESP is ALL about math... this is actually very neat stuff if youre interested in mathematics, I just finished my Calculus 3 course this past semester and its really fascinating how it all ties into this! Ofcourse the stuff in this tutorial is very very very basic compared to what we went over in class (infact its not very applicable , very theoretical and kinda useless, but nonetheless I feel knowledgeable enough to give a small little primer below)

Anyway: the dot product of two vectors gives you a scalar number (always) its just sum of the components multiplied together
example Vector 1 <1, 2, 3> Vector 2 <3, 2, 1>
so V1 • V2 is X1*X2 + Y1 * Y2 + Z1 * Z2 = 1 * 3 + 2 * 2 + 3 * 1 = 3 + 4 + 3 = 10!

But what does this 10 mean?

The scalar result you get from the dot product of two vectors basically tells you if they are perpendicular or not, so if V1 • V2 = 0 , then you know they are perpendicular to each other (at right angles)
If the Angle between V1 and V2 are less than 90 degress the dot product will be positive (greater than zero) like in the example I gave.
if the Angle between V1 and V2 are greater than 90 degrees the dot product will be negative( less than zero).

another way to rewrite this V1 • V2 = |A| * |B| * Cos(Θ)
in other words, the dot product of two vectors will be equal to the cosine of the angle between the vectors, times the length of each of the vectors
the |A| means the magnitude of the Vector A. its the square root of the sum of the components squared... |A| = sqrt( X^2 + Y^2 + Z^2)

Incase anyone forgot their trigonometry...

Sin = Opposite / Hypotenuse ........ Y/R
Cos = Adjacent / Hypotenuse........ X/R
Tan = Opposite / Adjacent ............ Y/X

R is radius (from the famous unit circle)

The 3D distance formula is the same exact formula as the 2D distance formula, with a Z component since we are dealing with games in 3D space.

if you have 2 3D vectors A and B <1,2,3> and <4,5,6> then the distance between these two vectors will be
sqrt( (X2-X1)^2 + (Y2-Y1)^2 + (Z2- Z1)^2)) = sqrt(3^2 + 3^2 + 3^2) = sqrt (9 + 9 + 9) = sqrt(27) = about 5.196

Another interesting thing that isnt mentioned in this tutorial is the cross product (only applicable to 3D coordinate systems)
essentially what happens when you cross two vectors is you get a 3rd vector that is normal to both of the vectors you just crossed
by normal I mean perpendicular ( at 90 degrees)

A Ã— B = [(a2b3 – a3b2) (a3b1 – a1b3) (a1b2 – a2b1)]

You can use the dot product to calculate how much to rotate, and the cross product to determine which way to rotate.
Additionally, the cross product is often used for finding an objects forward, up or right facing sides (vector)

I love Mathematics, its been my favorite subject in college so far....

I could draw up some triangles for yall sometime to help make more sense of it if needed. But I hoped this cleared some things up!
or maybe even a youtube recording or something? Dunno.... would have to show enough interest!

Either way I hope this helped you guys understand it a little better

Last edited:

#### Fleep

##### Founder
Meme Tier VIP
Excellent post xploiitz I think if you went over that information and how it could be applied many people(including me) would greatly appreciate it.
Some graphics or a Youtube video would be awesome.

Fleep

#### xploiitz

##### Coder
Fleep Tier Donator
Trump Tier Donator
Nobleman
awesome. I'll see if I can borrow my friends bamboo board this week, ill make a video of he lets me borrow it for a day or two.
Should be fun, I love teaching Math to people haha.

#### c5

##### Kim Kong Trasher
Dank Tier VIP
Dank Tier Donator
I love Mathematics, its been my favorite subject in college so far....
I'm in the same boat with you in that case

Math is inevitable when dealing with computers

#### VDV777

##### Jr.Coder
Full Member
Nobleman
Excellent post xploiitz I think if you went over that information and how it could be applied many people(including me) would greatly appreciate it.
Some graphics or a Youtube video would be awesome.

Fleep
Can you make a video "how to calculate the world to the screen," I can mount a video, make all the special effects in cinema 4d, AE. I have seen many similar issues - I think it will be helpful to all beginners. Unless of course you have time to eat .....

#### xploiitz

##### Coder
Fleep Tier Donator
Trump Tier Donator
Nobleman
I'm gonna see if I can get ahold of that bamboo tablet tonight and then in a day or two I'll make a video so long as I have the tablet!

Its actually really fun stuff!

The 3D to 2D is quite a complex formula

Here's a quick little article that explains it pretty nicely if you understand some basic trig.

https://freespace.virgin.net/hugo.elias/routines/3d_to_2d.htm

Last edited:

#### xploiitz

##### Coder
Fleep Tier Donator
Trump Tier Donator
Nobleman
Excellent post xploiitz I think if you went over that information and how it could be applied many people(including me) would greatly appreciate it.
Some graphics or a Youtube video would be awesome.

Fleep
So I have the tablet board on hand.... Fleep Do you have any recommendations for recording my self in such a way that you did for your tutorials?? I tried some free "Camstudio" program and I stopped recording after 20minutes, it ended up being a massive file that wouldve taken hours to compress, so I said fuck that.

but yeah, any recommendations?

#### Fleep

##### Founder
Meme Tier VIP
The way I do it is through FRAPS, I then use WinAvi to convert the files into MP4 which makes them much smaller.

Its a fair amount of work, with a tablet Im sure you will have limitations because of the specs but could be worth a try.

Fleep

#### VDV777

##### Jr.Coder
Full Member
Nobleman
So I have the tablet board on hand.... Fleep Do you have any recommendations for recording my self in such a way that you did for your tutorials?? I tried some free "Camstudio" program and I stopped recording after 20minutes, it ended up being a massive file that wouldve taken hours to compress, so I said fuck that.

but yeah, any recommendations?
Could not be better:
Bandicam - for video
https://rghost.ru/45989130
Video converter - for convert video
https://rghost.ru/45989215
and download from torrents - sony vegas 12 or Corel VideoStudio Pro X5 - for special effects

or Give me the video - I'll do nicely, with special effects

#### hacke21

##### Newbie
Full Member
You can use the dot product to calculate how much to rotate, and the cross product to determine which way to rotate.
Additionally, the cross product is often used for finding an objects forward, up or right facing sides (vector)

I love Mathematics, its been my favorite subject in college so far....

I could draw up some triangles for yall sometime to help make more sense of it if needed. But I hoped this cleared some things up!
or maybe even a youtube recording or something? Dunno.... would have to show enough interest!

Either way I hope this helped you guys understand it a little better
Hi,

i have some problems doing the facing in 3D space. In 2D space i did it like this:
C++:
``````        protected void FaceTarget()
{
Vector2 direction = center - target.Center;
direction.Normalize();

rotation = (float)Math.Atan2(-direction.X, direction.Y);
}``````
(center is a Vector2)

In 3D space i have two Vector3 objects and i want the first one to facing the 2nd vector.
Can you explain the facing in 3D a bit more in deepth ? Would be awesome !

Last edited:
Attention! Before you post:

99% of questions are answered in the Beginner's Guide, do it before asking a question.

No Hack Requests. Post in the correct section.  Search the forum first. Read the rules.

How to make a good post:

• Fill out the form correctly
• Tell us the game name & coding language
• Ask specific questions, be descriptive
• Post errors, line numbers & screenshots
• Post code snippets using code tags
• If it's a large project, zip it up and attach it

If you do not comply, your post may be deleted.  We want to help, please make a good post and we will do our best to help you.