Raised This Month: $ Target: $400
 0% 

Calculating fall distance


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Xellath
Veteran Member
Join Date: Dec 2007
Location: Sweden
Old 02-04-2009 , 14:53   Calculating fall distance
Reply With Quote #1

If a player jumps from a building and land in a random place, how can you calculate the fall distance?

Can you do this with pev_origin? If so, could anyone just give me a little code snippet?
__________________
Achievements API - a simple way for you to create your OWN custom achievements!
Xellath is offline
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 02-04-2009 , 15:00   Re: Calculating fall distance
Reply With Quote #2

Code:
#include <amxmodx> #include <amxmisc> #include <fakemeta> #include <hamsandwich> new bool:g_alive[33]; new bool:g_was_on_ground[33]; new bool:g_falling[33]; public plugin_init() {     RegisterHam(Ham_Spawn, "player", "FwdPlayerSpawn", 1);     RegisterHam(Ham_Killed, "player", "FwdPlayerDeath", 1);     RegisterHam(Ham_Player_PreThink, "player", "FwdPlayerPreThink"); } public client_disconnect(client) {     g_alive[client] = false; } public FwdPlayerSpawn(client) {     if( is_user_alive(client) )     {         g_alive[client] = true;         g_falling[client] = false;     } } public FwdPlayerDeath(client) {     g_alive[client] = false; } public FwdPlayerPreThink(client) {     if( !g_alive[client] ) return;         if( g_falling[client] && pev(client, pev_movetype) != MOVETYPE_WALK )     {         g_falling[client] = false;     }         static Float:fall_origin[33][3];         new bool:on_ground = bool:(pev(client, pev_flags) & FL_ONGROUND);         if( on_ground && !g_was_on_ground[client] && g_falling[client] )     {         static Float:origin[3];         pev(client, pev_origin, origin);                 new Float:distance = fall_origin[client][2] - origin[2];         if( distance > 0.0 )         {             client_print(client, print_chat, "You fell %f units!", distance);         }     }     else if( !on_ground && g_was_on_ground[client] )     {         g_falling[client] = true;         pev(client, pev_origin, fall_origin[client]);     }         g_was_on_ground[client] = on_ground; }
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!

Last edited by Exolent[jNr]; 02-05-2009 at 12:36.
Exolent[jNr] is offline
Xellath
Veteran Member
Join Date: Dec 2007
Location: Sweden
Old 02-05-2009 , 01:43   Re: Calculating fall distance
Reply With Quote #3

If the fall distance should be displayed in units, how can I make that? Lets say you jumped from a building and landed on the ground, then a HUD msg pops up and shows the fall distance.

Code:
set_hudmessage(255, 0, 0, -1.0, 0.8, 0, 6.0, 5.0, 0.1, 0.4, -1);
show_hudmessage(id, "Fall Distance: %s", distance);
Something like that would pop up. How do I calculate the distance? The snippet you gave me exo, didn't really help me, yes, I suck at scripting, but if you could explain more, I would be capable of understanding. Thanks anyway.
__________________
Achievements API - a simple way for you to create your OWN custom achievements!
Xellath is offline
ConnorMcLeod
Veteran Member
Join Date: Jul 2006
Location: France (95)
Old 02-05-2009 , 02:12   Re: Calculating fall distance
Reply With Quote #4

All is done in client prethink :

When jump/fall : store player origin
when land : check origin and calculate : stored Z origin - actual Z origin, result in units
Just show your hudmessage where x-olent put "// you have the fall distance"

distance is a float; so, in your message, replace %s with %.1f
__________________
- tired and retired -

- my plugins -
ConnorMcLeod is offline
Xellath
Veteran Member
Join Date: Dec 2007
Location: Sweden
Old 02-05-2009 , 02:42   Re: Calculating fall distance
Reply With Quote #5

I did like you said and I getting errors on show_hudmessage, id is undentified.

Code:
set_hudmessage(255, 0, 0, -1.0, 0.8, 0, 6.0, 4.0, 0.1, 0.4, -1)
show_hudmessage(id,"Fall Distance: %.1f",distance);
Is that correct?

EDIT: Nevermind, I got it right by setting client instead. Thanks for all the help. +karma.
__________________
Achievements API - a simple way for you to create your OWN custom achievements!

Last edited by Xellath; 02-05-2009 at 02:50. Reason: Fixed :)
Xellath is offline
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 02-05-2009 , 11:33   Re: Calculating fall distance
Reply With Quote #6

In about 20 minutes Ill be in my programming class so I will be able to rewrite it more efficiently instead of posting from my iPod.
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
Xellath
Veteran Member
Join Date: Dec 2007
Location: Sweden
Old 02-05-2009 , 11:42   Re: Calculating fall distance
Reply With Quote #7

That sounds great, Exo. Also, did you check my PM?
Xellath is offline
Xellath
Veteran Member
Join Date: Dec 2007
Location: Sweden
Old 02-05-2009 , 12:16   Re: Calculating fall distance
Reply With Quote #8

Thanks a bunch, Exolent. Have fun at programming class.
Xellath is offline
Dores
Veteran Member
Join Date: Jun 2008
Location: You really don't wanna k
Old 02-05-2009 , 12:26   Re: Calculating fall distance
Reply With Quote #9

@Exolent:

Code:
new boo:g_falling[33]; // Typo ---> new bool:g_falling[33];

Also:
Code:
pev(client, pev_origin, fall_origin[client]);
Are you positive that static variables' value is stored just like global vars(except of course only for the specific function where they were created)?
I think that static vars are only to prevent creating vars multiple times like in Think forwards to decrease CPU or something.

If you use new on every client_connect forward, it will recreate it every time the player connects.
But if you use static, the variable already exists but resets.
__________________
O o
/Ż________________________
| IMMA FIRIN' MAH LAZOR!!!
\_ŻŻŻ

Last edited by Dores; 02-05-2009 at 12:31.
Dores is offline
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 02-05-2009 , 12:36   Re: Calculating fall distance
Reply With Quote #10

Thanks for the typo.

And static acts like a global variable inside a local method.
It does not get reset.
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 01:53.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode