Half-Life Position, Velocity, and Angles (for n00bs)
Half-Life Position, Velocity, and Angles
This guide is intended for newbies with a desire to learn about position, velocity, and angles. The goal is for you to be comfortable manipulating these variables in Pawn in AMXx in Half-Life. Dimensions dimension - a property of space; extension in a given direction
There really is no dimension here--it's the 0th dimension, after all. You can think of it as an infinitely small point. It does not extend in any direction. We cannot specify a location in this dimension.
Now we have a line in one direction. You can only change the length of the line. Many problems in physics are idealized to one-dimensional problems for simplicity. For example, falling bodies are often idealized to fall straight down with no deviation. We can specify a location in this dimension with one number. For example, a straight line starting from your floor going up to your ceiling. Let's say the floor is 0, the ceiling might be 2.5 (in meters, let's say). Any point in between would be between 0 and 2.5. In code, you can hold the position with a variable like this: Code:
I'm only talking about the first dimension, so, strictly speaking, there is no explicit direction. You can only go back and forth. However, you can say the variable from my example exists in the Y-direction because our world is in three dimensions and saying "Y-direction" implies that we are talking about going up and down, in general. Depending on the context, you might say that a one-dimensional variable is in the X-direction or Z-direction. Typically, you only say that it is in the X or Y directions.
Finally! Now we can move up, down, left, and right (and any combination of these directions). More complicated physics problems are idealized in two dimensions. For example, a projectile's motion is idealized in two dimensions. We can specify a location in two dimensions with a pair of numbers. We call this pair of numbers coordinates. In writing, you usually see coordinates written like this (X, Y). The X-direction means left and right. The Y-direction means up and down. In code, you can hold the position with a variable like this: Code:
The variable is called an array and it holds two values. The first value, fPosition[0], should hold the position in the X-direction. The second value, fPosition[1], should hold the position in the Y-direction. (You can also use [1] for the X-direction and [0] for the Y-direction, but it's nice to do things in a standard way so others can understand your code quickly.) Code:
set_hudmessage prepares a hud message with position, color, effects, and time show_hudmessage shows text in the way specified by set_hudmessage
OH BOY! Now we can move up, down, left, right, forwards, backwards AND any combination of these directions. We can specify a location in three dimensions with a group of three numbers. In writing, you usually see coordinates written like this (X, Y, Z). The Z-direction means forwards and backwards. BUT WAIT! The Z-direction means forwards and backwards in the picture above, but it actually means up and down in Half-Life. Why? The programmers of Half-Life decided to make the Z-direction up/down. So, forward/back/left/right are handled with the X and Y directions.In code, you can hold the position in three dimensions with a variable like this: Code:
fPosition[0] holds the position in the X-direction, fPosition[1] holds the position in the Y-direction, and fPosition[2] holds the position in the Z-direction. Code:
Vectors So far, I've covered the 0th, 1st, 2nd, and 3rd dimensions. The code examples have dealt with coordinates representing positions, but I have hinted at a different use for three-dimensional arrays with my physics examples. You need to learn two new words: vector and scalar vector - a quantity possessing both magnitude and direction, represented by an arrow the direction of which indicates the direction of the quantity and the length of which is proportional to the magnitude scalar - representable by position on a scale or line; having only magnitude When we are dealing with positions, like the position of a player in a map or the position of text on the screen, we are dealing with scalar quantities. Scalar values have no direction, only magnitude (size). When you say a player is located at (X, Y, Z), you know nothing about whether or not he is moving. If he is moving, you don't know where. Velocity is a vector quantity. Let's jump into an example: Code:
pev is a FakeMeta function used to retrieve values from entities pev_velocity tells pev() which value we are retrieving fVelocity holds the value we are retrieving, it must be a 3-dimensional array After executing this code, let's say that our velocity is now (100, 200, 0). This means we have a velocity in the X-direction of 100 units per second, a Y-velocity of 200 units per second, and a Z-velocity of 0 units per second. The numbers directly indicate the magnitude of the velocity. How can we find the direction? (Vector values have both magnitude AND direction.) Here is the situation, in picture form: http://i.imgur.com/tAHOwfO.png Notice that the vectors both start at the origin (0, 0, 0) and extend away from it. To get the direction, you must combine all three component vectors (X, Y and Z) into one. (Components are pieces.) http://i.imgur.com/CTBc3P8.png So, now we can see that fVelocity holds a vector value. That means you can discover the magnitude AND direction of the velocity by interpreting the values it holds.
They are not the same. Speed is a scalar quantity and velocity is a vector quantity. You can store speed in a variable like this: Code:
To store velocity, you must have a variable like this: Code:
Speed means distance/time, like miles per hour (mph) or meters per second (m/s). It does not tell you where you are going. Velocity, when you're NOT dealing with code, is written in two parts. First, you say the speed like 40 mph (yes, speed is one part of velocity). Next, you must say the direction. For example, North. When we're dealing with code, you don't have two parts. Instead, you have to interpret the 3 values to figure out speed and direction.
Remember Pythagoras? He has this nifty theorem: Code:
c * c = a * a + b * b If you glance at the above image with the red, green, and pink arrows, you can see how this works. C is the length of the pink arrow, a is the length of the green arrow, and b is the length of the red arrow. In three dimensions, it's like this: Code:
hypotenuse * hypotenuse = x * x + y * y + z * z Here's what we were looking for: Code:
speed * speed = x vel * x vel + y vel * y vel + z vel * z vel Code:
We don't have North, South, East, and West in Half-Life. We just have angles, so that's how we'll represent the direction of our velocity. The math is more complicated, so let's leave it at this: Code:
So, let's do something Code:
set_pev is a FakeMeta function used to set various values for entities random_float generates a random number between two numbers random_num does the same thing, but returns an int rather than a float set_task is used to call a function after a few seconds Angles There is another use for three-dimensional arrays (variables like fVelocity[3]). They are used to store an entity's angles.
I'm going to use your imagination to make this easy to understand. Imagine a hotdog and place it on the desk in front of you with one end pointing at the monitor. pitch - lift one end of the hotdog while the other end stays on the desk PUT IT BACK DOWN! yaw - now, spin the hotdog on the desk POINT IT AT THE MONITOR AGAIN! roll - now, roll the hotdog along the desk Now do the same actions you did to the hotdog, but move your head instead of the hotdog. (Tilt your head up/down, rotate left/right, and then roll it.) You should begin to feel a sort of satisfaction now that you understand the meaning of pitch, yaw, and roll. If not, yell at me. Here's a nice image that explains pitch, yaw, and roll. Thanks arkshine! (And thanks to the creator, whoever it is.) http://i.imgur.com/ut8S82e.png In Half-Life, the angles are stored thusly: Code:
You can use pev() to retrieve the angles, just like we did with velocity or origin, but there are several different angles in Half-Life. You'll have to experiment with them to get a good idea of the purpose they serve. pev_angles - orientation of the entity pev_v_angle - orientation of the camera, (player's view), v_angle = view angle pev_punchangle - deviation from the view angle (POW! I punched you in the kisser and now your face rotated over there but you will slowly return back to your v_angle) You can get/set these just like the others, with FakeMeta or Engine: Code:
What's a normal? Is it the opposite of a weird? Wow, I'm tired. More vocabulary for your hungry, hungry mind: plane - a flat or level surface normal - the dictionary definition is confusing, so here's mine: a line that is perpendicular to the plane Why is this useful? Well, it get's complicated.
My Wall Text plugin makes use of positions, vectors, angles, and normals. The goal of the plugin is to aim at a wall and paint text on it. Here's the step-by-step process: 1. Get the position on the wall where the user is aiming. 2. Get the normal of the wall. (The wall is a plane and the normal points out of the wall into the world where the player exists.) 3. Make a vector pointing the to left (from the perspective of the wall's normal). 4. PAINT! 5. Move over to the left, using the vector we calculated. 6. Repeat steps 4-5 until finished. If you're observant, you'll notice that I've neglected to do this calculation for sloped surfaces like ramps. The plugin will work for perfectly vertical walls at any angle, but it will fail if you want to do a multi-line message on a sloped wall like a ramp, because I was too lazy to write the extra code. Here's the code: Code:
|
Re: Half-Life Position, Velocity, and Angles (for n00bs)
Would help others GJ :up:
|
Re: Half-Life Position, Velocity, and Angles (for n00bs)
First. I will read it now.
Edit: fuck |
Re: Half-Life Position, Velocity, and Angles (for n00bs)
Quote:
|
Re: Half-Life Position, Velocity, and Angles (for n00bs)
Quote:
|
Re: Half-Life Position, Velocity, and Angles (for n00bs)
Quote:
|
Re: Half-Life Position, Velocity, and Angles (for n00bs)
PutMeBackDammit!
|
Re: Half-Life Position, Velocity, and Angles (for n00bs)
Woo.~~
|
Re: Half-Life Position, Velocity, and Angles (for n00bs)
Code:
set_task( 2.0, "PutMeBackDammit", id, fOrigin, 2 ) Secondly, you are only passing the first 2 dimensions through the set_task. Also, in PutMeBackDammit fOrigin[3] should be iOrigin[3] since it isn't really declared a float. I would suggest adding mins/maxs in the angles section. And lastly, clean up your language, it's a little annoying with all the "dammit"s. Anyway, cheers for the tut. :crab: |
Re: Half-Life Position, Velocity, and Angles (for n00bs)
Thanks for catching the mistakes. I used "dammit" with the intention of adding some humor to an otherwise boring read. Maybe you're right that it's inappropriate, so I've removed all of the occurrences.
I'll cover mins/maxs shortly. Thanks! :crab: |
All times are GMT -4. The time now is 18:12. |
Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.