Camera Vectors in R5 - how to use, READ THIS!
#15

After about an hour thinking about this idea last night, and how it would be possible, then about an hour making it today, I present to you...
GetPlayerFacingPoint (couldn't think of a better name)
Finally, a function which will return the X, Y and Z coordinate which your player is currently facing/aiming at.

NOTE: This needs the MapAndreas plugin to work!

The code will be at the end of this post, but first..

What does it do?
- It returns the X, Y and Z coordinate in the world which your player is currently aiming at.

What could it be used for?
- Anything you want. While I was testing, I made a random "gun" which created an explosion at the point the player was looking at. The possibilities are endless*.
* The possibilities aren't endless due to technical limitations, but it still sounds cool to say that.

How does it work?
- It works by using the players camera vector and position, and map data from the MapAndreas plugin to detect where objects are.

Wait, so it actually gets the position where the player is aiming? The thing that noobs have been requesting for the past 4 years?
- Yes, although with slight limitations listed below.

Are there any problems or limitations?
- Actually, yes. Because of the way the MapAndreas plugin works, as well as the way it's used in this function, it's not perfect. Potential problems are listed below;
  • It, sadly, doesn't detect players - this is possible to implement but would require a player loop and IsPlayerInRangeOfPoint on every increment.
  • Because of the way walls are detected, this will not work in covered areas such as car parks, and attempting to shoot from outside into a carpark will cause the point to be returned at the entrance.
  • Also because of the way walls are detected, if you aim something with a cover (of any sort) before it (such as a shop with a small covered area on the outside of it, or even an extended bit of the wall), the point will be at that object, the one closest to you, even if there is no wall at that position. (sort of same problem as above)
  • It doesn't work with custom objects, either (although this functionality may be added to MapAndreas)
  • AFAIK, the MapAndreas data is accurate to 1 GTA unit. Some objects are less than this size, and are not in this data (such as fences or walls), which means these will be skipped and the position will "go through" it.
  • Some objects, like metal fences, are not in the MapAndreas data because they are dynamic and can be destroyed. This is the same problem as above.
  • Over long distances (very long, or on shitty computers) the function will take a while to execute (nup, read below)
  • Camera data seems to only update every second or two. This means if you're using the function on a moving player, it may detect the point at a previous camera position. (nup, read below)
BUT, the function still works exceptionally well considering how it detects the point.

Who made this function?
- I did, of course. With attempted help from Kye when the default values on the function wouldn't work while it was public, but nobody else helped on the actual function itself. Credit to Kye for creating the MapAndreas plugin (and SA-MP), and JernejL for helping explain the camera position/vector functions.

Any demo for this?
- I was going to upload a video to ******* of a small script I made which creates an explosion where you're aiming, but I couldn't be bothered. I'll leave it up to you guys.

Holy shiznit, I found a bug!
- I have tested this thoroughly and haven't found anything wrong with the actual code, besides the limitations listed above. If you believe there is something else wrong with it, PM me or reply to this thread.

Could you explain the parameters?
GetPlayerFacingPoint(playerid,&Float,&Float:y,&F loat:z,Float:limit=30.0,Float:increment=1.0)
  • playerid - If you can't figure out what "playerid" might be, this function is definitely not for you.
  • &Float - Variable to store the result of the X axis.
  • &Float:y - Variable to store the result of the Y axis.
  • &Float:z - Variable to store the result of the Z axis.
  • Float:limit=30.0 - The limit (in game units) before the loop will stop. If no objects are detected before the limit, the result will be the position at this point.
  • Float:increment=1.0 - The amount to increment each time the position is checked for objects. MapAndreas data is accurate to 1 GTA unit, so it shouldn't be less than 1.0. Lower values = higher accuracy (except <1), slower processing. Higher values = lower accuracy, faster processing.
GIVE US THE F***ING CODE ALREADY
Oh, right, sorry...
pawn Код:
// This is where the fun starts
// Note: this needs the MapAndreas plugin to work!
GetPlayerFacingPoint(playerid,&Float:x,&Float:y,&Float:z,Float:limit=30.0,Float:increment=1.0) {
  // Create the variables used
  new Float:camvector[3],Float:campos[3],Float:last[4],Float:cur[4],a=0,started=0;
  // Get the players camera front vector and position
  GetPlayerCameraFrontVector(playerid,camvector[0],camvector[1],camvector[2]);
  GetPlayerCameraPos(playerid,campos[0],campos[1],campos[2]);
  // Start the loop
  // This loop will go through, and at each (increment) from the players camera vector/position it will check that
  //  position and whether there are obstacles there. It hopes to be reasonably accurate at detecting objects so that
  //  it's able to return the exact(-ish) position a player is aiming at.
  // See the "if" check in the loop for how it detects objects.
  // If no position is found before (limit), the position at (limit) will be returned
  for(new Float:i=0.1;i<=limit;i+=increment) {
    for(a=0;a<3;a++) cur[a] = (camvector[a] * i) + campos[a];
    MapAndreas_FindZ_For2DCoord(cur[0],cur[1],cur[3]);
    // Make sure this isn't the first run. Probably better just to check last[0-3] results. eh.
    if(started &&
    // Player is aiming up, current highest point higher than test Z, last highest point lower than test Z
    (((cur[3] > cur[2]) && (last[3] < cur[2])) ||
    // Player is aiming down, current highest point higher than test Z, last test Z higher than highest point
    ((cur[3] > cur[2]) && (last[2] > cur[3])))) {
      // We have a rough point where the player is aiming; between the current and last point
      // Average these points to get a (generally) more accurate/acceptable result
      for(a=0;a<4;a++) last[a] = ((cur[a] + last[a]) / 2); // Save all last values as average of results
      // Note (above) although all 4 values (X, Y, Z, highest Z) are saved, only 3 are used/returned
      break; // Break out of the loop with the last values saved
    }
    else {
      for(a=0;a<4;a++) last[a] = cur[a]; // Save all last values
    }
    started = 1; // If it wasn't set already, (first) indicate this is now past the first run
  }
  // Save the final values into the referenced variables
  x = last[0];
  y = last[1];
  z = last[2];
  return 1;
}
The function is well commented so that scripters can understand what the hell I'm doing, and so that I could as well, while I was making it. (I usually confuse myself with things like this)


Some more boring notes for you to read;
- The GTA map is, AFAIK, 6000 units wide and high. This means it's the maximum limit before it's just useless. If you had this as the limit, though, you're an idiot and should be shot. I don't recommend it to be any higher than 100.
- I also don't recommend that the increment be set any lower than 1.0, it will be useless because of the MapAndreas map data.
- I ALSO, finally, don't recommend that you use this function constantly. The function (default) averaged around ~20ms on a reasonable computer. This is 0.2% of a second, so it's really not much, but people get crazy ideas (like saving files OnPlayerUpdate) so be careful. Anyway, most servers will be more powerful than my computer, and will execute the function much faster.

And regarding the stuff above which is crossed out...
- About using the function constantly, or it running slowly, well I re-tested and found it took <1ms to execute each time on default settings.
- About the camera positions not updating, well it seemed to be the case before, but for some reason it now seems to update instantly, and testing while running with 20 checks per second seems to prove this.

Enjoy.
Reply


Messages In This Thread
Camera Vectors in R5 - how to use, READ THIS! - by JernejL - 24.03.2010, 18:20
Re: Camera Vectors in R5 - how to use, READ THIS! - by Norn - 24.03.2010, 18:42
Re: Camera Vectors in R5 - how to use, READ THIS! - by JernejL - 24.03.2010, 18:44
Re: Camera Vectors in R5 - how to use, READ THIS! - by wafffllesss - 24.03.2010, 18:47
Re: Camera Vectors in R5 - how to use, READ THIS! - by Norn - 24.03.2010, 19:23
Re: Camera Vectors in R5 - how to use, READ THIS! - by JernejL - 24.03.2010, 21:13
Re: Camera Vectors in R5 - how to use, READ THIS! - by CracK - 25.03.2010, 05:15
Re: Camera Vectors in R5 - how to use, READ THIS! - by Burridge - 25.03.2010, 07:08
Re: Camera Vectors in R5 - how to use, READ THIS! - by boelie - 25.03.2010, 19:18
Re: Camera Vectors in R5 - how to use, READ THIS! - by JernejL - 25.03.2010, 19:30
Re: Camera Vectors in R5 - how to use, READ THIS! - by boelie - 25.03.2010, 19:37
Re: Camera Vectors in R5 - how to use, READ THIS! - by MaykoX - 25.03.2010, 19:59
Re: Camera Vectors in R5 - how to use, READ THIS! - by Rac3r - 26.03.2010, 07:22
Re: Camera Vectors in R5 - how to use, READ THIS! - by CracK - 26.03.2010, 11:22
Re: Camera Vectors in R5 - how to use, READ THIS! - by ev0lution - 27.03.2010, 01:36
Re: Camera Vectors in R5 - how to use, READ THIS! - by Norn - 27.03.2010, 08:11
Re: Camera Vectors in R5 - how to use, READ THIS! - by ev0lution - 28.03.2010, 08:35
Re: Camera Vectors in R5 - how to use, READ THIS! - by ev0lution - 02.04.2010, 00:15
Re: Camera Vectors in R5 - how to use, READ THIS! - by SourceCode - 20.09.2011, 01:34
Re: Camera Vectors in R5 - how to use, READ THIS! - by JernejL - 28.09.2011, 21:05
Re: Camera Vectors in R5 - how to use, READ THIS! - by SourceCode - 15.11.2011, 13:45
Re: Camera Vectors in R5 - how to use, READ THIS! - by Banditukas - 02.11.2014, 12:00

Forum Jump:


Users browsing this thread: 6 Guest(s)