![]() While the grid is placed in this manner, the edge of the screen would be half the screen's width, negative for left and positive for right. If we wanted to find where it crosses the top or bottom of the screen, we divide both sides by m so that the equation becomes: x = y/m - then we just set y to the edge of the screen. ![]() For example, if we want to find what the y-value is at the point where the line crosses the edge of the screen, then we use the original form y = mx, where x is set to the edge of the screen. Once we have the slope, we can use substitution to calculate where the line would cross the borders of the screen. (In the image above, our target is the mouse cursor.) And if the grid is placed like this, we can very easily calculate the slope, m: it's simply the target's y/ x. Since the line will go through the center, we know that our intercept, b, must be zero. ![]() If we imagine that our screen is on a grid, and that the origin point (0, 0) is right at the center of the screen, then it's easy to calculate the values that describe the line. If we can find a line on screen describing which direction the object we are targeting is in, we can determine the point where it crosses any given edge, and then use a little trial and error to find out which side of the screen it will be attached to. Most mainstream engines have built in functions for doing this consult your engine's documentation for more. Tip: If you are working in 3D, you'll need to transform the world location to the screen location of your 3D object. Since we are finding the position relative to the screen - a flat surface - we do all calculations in 2D, even if the game is in 3D. Thanks to this relationship, if we have one value we can use the general equation to easily calculate the other value, both conceptually and mathematically. It uses a slope, which normally uses the symbol m, that defines the steepness of the line, and an offset or intercept, which uses the symbol b, that defines where the line crosses the y-axis. The slope intercept form is a way to describe a straight line in 2D with linear algebra. ![]() In this tutorial, I'll explain a method that uses simple algebra to find where to place such an indicator arrow. Many games use an arrow that floats close to the edge of the screen to indicate which direction the target lies in. In 2D scrolling games (and some 3D games), you'll often need to show the player the location of a target that is off-screen, whether it's an enemy, an ally, or a game objective. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |