AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Velocity - Speed and Direction (https://forums.alliedmods.net/showthread.php?t=343172)

MrPickles 06-24-2023 00:15

Velocity - Speed and Direction
 
Hi, I was wondering, if I have a certain Velocity stored in a array, we know that velocity is a vector, it has direction and speed, but how can I change its speed, keeping its same direction?

fysiks 06-24-2023 16:17

Re: Velocity - Speed and Direction
 
Multiply it by a number. If you're not familiar with how vectors work, this just mean multiply each of the three values by the same number. If you want to double the speed, multiply each of the three values by 2.

MrPickles 06-24-2023 16:24

Re: Velocity - Speed and Direction
 
Quote:

Originally Posted by fysiks (Post 2806378)
Multiply it by a number. If you're not familiar with how vectors work, this just mean multiply each of the three values by the same number. If you want to double the speed, multiply each of the three values by 2.

What if I want to multiply it by 5 but I don't want its speed to exceed "X" amount?

fysiks 06-24-2023 16:30

Re: Velocity - Speed and Direction
 
If you want to set it to a specific value, you need to calculate the "unit vector" (a vector with magnitude of 1). Then, you can multiply that by the exact speed value that you want. There is a bunch of info on the internet to calculate this, I'd recommend checking some of them out. I think there might be some functions in AMX Mod X to help with this, I just haven't found them yet (it's been a while since I've needed to use vector stuff).

EDIT: Here is a function to normalize the vector (to get the unit vector): http://www.amxmodx.org/api/xs/xs_vec_normalize

MrPickles 06-24-2023 21:55

Re: Velocity - Speed and Direction
 
some example in a real code?

with:

PHP Code:


new Float:__fl_Velocity[3];
entity_get_vectoridEV_VEC_velocity__fl_Velocity );

// Multiply speed

entity_set_vectoridEV_VEC_velocity__fl_Velocity ); 


Natsheh 06-26-2023 08:31

Re: Velocity - Speed and Direction
 
PHP Code:

#include <xs>
#include <engine>

client_prethink(id)
{
static 
Float:__fl_Velocity[3];
entity_get_vectoridEV_VEC_velocity__fl_Velocity );

if(
xs_vec_len(__fl_Velocity) > 100.0)
{
   
xs_vec_normalize(__fl_Velocity__fl_Velocity);
   
xs_vec_multiple(__fl_Velocity100.0__fl_Velocity);
}

entity_set_vectoridEV_VEC_velocity__fl_Velocity ); 


Here in the previous code it will limit the maximum speed for players to 100 units/sec


Keep in mind vector angles wont change if you divide the vector by its length and it will be called a unit vector, if you've a unit vector and want to give it a magnitude you've to multiplied it with a number that number will be called a vector scalar.

fysiks 06-26-2023 23:37

Re: Velocity - Speed and Direction
 
Quote:

Originally Posted by Natsheh (Post 2806432)
PHP Code:

#include <xs>
#include <engine>

client_prethink(id)
{
static 
Float:__fl_Velocity[3];
entity_get_vectoridEV_VEC_velocity__fl_Velocity );

if(
xs_vec_len(__fl_Velocity) > 100.0)
{
   
xs_vec_normalize(__fl_Velocity__fl_Velocity);
   
xs_vec_multiple(__fl_Velocity100.0__fl_Velocity);
}

entity_set_vectoridEV_VEC_velocity__fl_Velocity ); 


Here in the previous code it will limit the maximum speed for players to 100 units/sec


Keep in mind vector angles wont change if you divide the vector by its length and it will be called a unit vector, if you've a unit vector and want to give it a magnitude you've to multiplied it with a number that number will be called a vector scalar.

"scalar" and "vector" are two mutually exclusive terms in this context. I guess you probably mean that it's a "scaler" which probably just makes it super confusing. Just call it a number. If you really need a name for it you'd probably just call it "the magnitude".


All times are GMT -4. The time now is 14:31.

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