How to know what player the camera is facing?
#1

(not much time to explain)

OK, I am making an artificial gun, one that is mounted in a window like in, "brother at arms," the iPad game.

Here is a topic with a lot of info, (click me).

I tried a lot of it, modified a lot of it, failed most of it, got real close, failed again...

Here is what I am trying to get...
- Only the closest player is shot, only player in exact center of camera vision, so a radius of about 1.5.
- Don't want to be able to shoot through walls, but I understand that this is hard to do since you can't detect line of sight.
- All of this in a function that returns only the target players ID.

Maybe this should be in the requests section, not sure, I am asking for help, I told you what I did wrong. I've been typing this for like 20 minutes now, on phone!

Thanks!
Reply
#2

Here i've made a Throwing knife thing, which detects the Player in front of you and removes some health.
http://pastebin.com/qeSBDWDd
This will help you, If you want me to explain the script, just reply i'll do it
Reply
#3

Quote:
Originally Posted by Rudy_
Посмотреть сообщение
Here i've made a Throwing knife thing, which detects the Player in front of you and removes some health.
http://pastebin.com/qeSBDWDd
This will help you, If you want me to explain the script, just reply i'll do it
I think the reason I failed is that I'm horrible in trigonometry, that being said, could you Rudy explain just exactly what each of these lines do (not the obvious parts).
pawn Код:
tmpang = (90-atan2(tmpy-y, tmpx-x));
if(tmpang < 0) tmpang = 360.0+tmpang;
if(floatabs(tmpang-r) < 5.0)
This is so I know for future reference...
Reply
#4

Thanks to southclaw,


trig is kinda simple, to understand how this code properly works you have to understand how trigonometry works. Which is fairly simple yet hard to grasp at first.

The reason trigonometric functions are to return the lengths of sides on a triangle by using the Angle and other side lengths.
The application in SA:MP is to effectively get the two straight sides of a theoretical right angle triangle.

This image shows how it works, the diagonal line represents the direction (fAng = Angle of vehicle or player or whatever) You can see the radius of this (based on the distance, which is 0.5, I realise now that is a bad choice for an example)

The angle you take from players/vehicles is the offset angle from north (0 degrees) this is the angle you use. Now can you see two triangles in that diagram? Two right angled triangles on either side of the direction line if you join up the projected point to the X and Y axis (Draw lines in your head or something! (No! Don't get a pencil and start drawing on your head, that's not what I meant!))

You can use the trig functions on those theoretical triangles to get the lengths of the sides, now there's a way of working out what you need to use but that's used more in Geometry (You'll lean that if you take a math class or something!) Just remember it like this for SA:MP:

(Keep in mind SA:MP coordinates are in order: XY(Z) (ignore Z for now)

seeing as it's (x + sin, y + cos) I remember it by "sinning first 'cos I want to!" (It sounds stupid, but it works!)


Anyway, the next diagram shows what the Sine function does to the angle:



As you can see, sin-ing the angle will give you the "height" of the triangle, this is just a distance on the X axis, which is really handy because now you can add that distance to the Origin X coordinate!


This next image is similar, this is the Cosine function at work:


When you input the angle it returns that small distance from the origin to the projected point on the Y axis, now you can simply add that value to the Origin Y coordinate and there you go!



With the X modified with floatsin and Y modified with floatcos you now have a projected position based on an angle!

Wondering how to project it a certain distance? Well, I made a huge mistake in the images, the distance should be 1.0 NOT 0.5 (Just pretend it's 1.0!)
When you do this sin/cos stuff to coordinates, the end result will be always 1 unit from the origin. Now this is useful because now you can simply multiply those sine and cosine values by any unit value to project it a specific distance:

pawn Код:
X + (5.0 * floatsin(fAng)), Y + (5.0 * floatcos(fAng))
// This will make the projected position 5.0 meters away.
This also works with values below 1.0:

pawn Код:
X + (0.5 * floatsin(fAng)), Y + (0.5 * floatcos(fAng))
// This will make the projected position 0.5 meters away.
And negative values will make the projected position behind the origin point:

pawn Код:
X + (-5.0 * floatsin(fAng)), Y + (-5.0 * floatcos(fAng))
// This will make the projected position 5.0 meters behind the origin.


You can alter the angle too to produce different results, for instance adding 90.0 to the angle will make the projected position on the right hand side* of the origin:


pawn Код:
X + (5.0 * floatsin(fAng + [U]90.0[/U])), Y + (5.0 * floatcos(fAng + [U]90.0[/U]))
*Player/vehicle positions are inverted for some reason, adding 90 will make it on the left I think... (Experiment to find out for yourself! I always forget)
Reply
#5

Very well explained, I already knew most of what you explained, I used a similar method with sin and cos in my script in which players can throw players ...

pawn Код:
new Float:X, Float:Y, Float:Z, Float:VX, Float:VY, Float:A;
                GetPlayerCameraFrontVector(playerid, VX, VY, A);
                A = atan2(VY, VX) + 270.0;
                GetPlayerVelocity(targetid, X, Y, Z);
                SetPlayerVelocity(targetid, floatsin(-A, degrees) * 0.5, floatcos(A, degrees) * 0.5 , floatabs((Z*2.5)+1.035));
To the player/vehicle positions being inverted, I also use the method for spawning cars in front of my admins facing the left side of them, and yes, you do add 90 :P...

pawn Код:
new Float:x,Float:y,Float:z,Float:r;
                GetPlayerPos(playerid, x, y, z);
                GetPlayerFacingAngle(playerid,r);
                x += (5 * floatsin(-r, degrees));
                y += (5 * floatcos(-r, degrees));

                CreateVehicle(vehicleid, x, y, z, [COLOR="Red"]r+90[/COLOR], color1, color2, 5000);
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)