|
RayHitMapPixels | |
Collision = RayHitMapPixels(ThisMap, TheLevel, StartX, StartY, EndX, EndY) | |
Parameters: ThisMap = The Index of the map you query for a collision TheLevel = The level within the map to query StartX = The starting X coordinate of the ray StartY = The starting Y coordinate of the ray EndX = The ending X coordinate of the ray EndY = The ending Y coordinate of the ray |
|
Returns: Collision = The collision state, It'll return True (1) in an impact occurs and false (0) if it didn't |
|
The RayHitMapPixels() function checks if a ray impacts upon map level at pixel level. The function uses a method known as ray casting, which is fancy way of saying the algorithm steps along the line looking for impacts upon any none transparent pixels within the map. So the ray is traced from the starting coordinate through to the end coordinate if no impact is found. When an impact occurs, the functions returns true (1) and the coordinate of that impact will be stored in vertex zero in GetIntersectX#(0) and GetIntersectY#(0). Detecting impacts on the map can be useful is number of different gaming situations, two that come to mind would be detecting line of sight and of course projectile impacts with the environment. Line of sight calculations are common in video game character AI, where we might want our character to only react to the player (or other characters) when they're clearly in view. For example, imagine our computer controlled character is meant to only fire towards the player when the player is in clear sight. If it fired just every time in the direction of the target, then it'd firing into walls other obstructions and the player would soon see the character isn't that smart. . It's precisely these types of situations why commands such as RayHitMapPixels are in PlayBASIC. Now assuming our game environment is constructed using a map, or we at least are using a map for collisions, we can detect if our character can see the player by casting a ray between the two character positions. To do this we use RayHitMapPixels giving the command the map & level, with the starting coordinate of the character wanting to fire and the destination coordinate of the character it want to fire at. If the ray hits the environment, the way is obstructed, if not, the way is clear. So our character can happily fire towards the player. Another situation where RayHitMapPixels can be very helpful, is when we plot the path of projectiles across a map. When we have fast moving objects in a game, the temptation is that we need to check for impacts between our moving object (like a bullet) and the game environment every time the projectile moves. There would no doubt be situations where that might well be the only solution, like when the game environment is changing every refresh, but that's generally not the case. In most games the game environment is static, what this allows to do is work out the projectiles complete flight path creation to potential impact with the game environment, when it's fired.. Now assuming we have the angle the project is going to be moving in, we can use COS / SIN to calculate the expected end point of the moving object. Once we've to end point, we use RayHitMapPixels to check for any impacts along the path. Regardless if it hits the world or not, we've gathered some important information about this projectile, namely it's life span. To calculate it's life span in frames, we take distance the object will be travel (GetDistance2D), and divide that by it's speed (number of pixel it's moving per frame). This gives us how many times the object will move before impacting the world. Knowing when it's hit the world up front, negates the need to check for impacts between the projectile and world while it's moving. What we're doing here, is swapping a constant overhead for a one time overhead. Which is generally a more efficient solution, the more projectiles you have. FACTS: * RayHitMapPixels traces across the map pixel by pixel, so it's best used on maps with FX formatted blocks. * RayHitMapPixels is optimized for speed over accuracy, so the there's bound to be some inaccuracy when tracing long rays, or parts of map where the solid pixels are tiny. |
|
Example Source: Download This Example
|
|
|||||||||||||||||||||||||||||||||||||||
(c) Copyright 2002 - 2024 - Kevin Picone - PlayBASIC.com |