Velocity: Difference between revisions
Jump to navigation
Jump to search
Note:The next paragraph only appears to apply to players! Base velocity is "swallowed" by other entities.
Warning:Don't try to change another entity's local or absolute velocity, as it will likely overwrite both values when it next tries to move. Instead, add to the existing base value with
TomEdwards (talk | contribs) No edit summary |
m (→See also) |
||
(7 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
'''Velocity''' is a measure of speed and direction. In | '''Velocity''' is a measure of speed and direction. In {{source|4}}, it is a <code>[[Vector]]</code> that an entity will try to move the length of once per second: a velocity of <code>(0,0,-200)</code> means that you are falling at 200 [[unit]]s per second. | ||
== | == Absolute, local and base == | ||
There are actually three different velocity vectors: | There are actually three different velocity vectors: | ||
Line 8: | Line 8: | ||
# '''Base velocity:''' applied to the entity from other sources | # '''Base velocity:''' applied to the entity from other sources | ||
# '''Absolute velocity:''' the sum of the local and base velocities; the actual value used to move | # '''Absolute velocity:''' the sum of the local and base velocities; the actual value used to move | ||
{{note|The next paragraph only appears to apply to players! Base velocity is "swallowed" by other entities.}} | |||
Having separate local and base velocities allows for the proper handling of [[Wikipedia:Inertia|inertia]]. If an NPC is standing on a fast-moving train, the motion of the carriage is stored in its base velocity while the motion the NPC creates itself when walking up and down is stored in its local velocity. There is no danger of either value overwriting the other, or of the NPC thinking it's walking at 150MPH. | Having separate local and base velocities allows for the proper handling of [[Wikipedia:Inertia|inertia]]. If an NPC is standing on a fast-moving train, the motion of the carriage is stored in its base velocity while the motion the NPC creates itself when walking up and down is stored in its local velocity. There is no danger of either value overwriting the other, or of the NPC thinking it's walking at 150MPH. | ||
{{warning|Don't try to change another entity's local or absolute velocity, as it will likely overwrite both values when it next tries to move. Instead, add to the existing base value with <code>SetBaseVelocity()</code>. }} | {{warning|Don't try to change another entity's local or absolute velocity, as it will likely overwrite both values when it next tries to move. Instead, add to the existing base value with <code>SetBaseVelocity()</code>.}} | ||
== Application == | |||
When an entity moves, the code looks a little like this: | |||
<source lang=cpp>SetLocalVelocity( vecSomeValue ); | |||
SetAbsVelocity( GetLocalVelocity() + GetBaseVelocity() ); | |||
// frametime is in seconds, e.g. 0.033 | |||
SetAbsOrigin( GetAbsOrigin() + (GetAbsVelocity() * gpGlobals->frametime) );</source> | |||
Notice how: | |||
# Objects don't so much travel through space as teleport a very small distance each frame. {{note|Beware an object moving so fast that it doesn't pass through intervening space, which can happen unexpectedly if the server's framerate dips too far. {{todo|How can this be best avoided?}}}} | |||
# <code>[[gpGlobals]]->frametime</code> is used to regulate the entity's speed regardless of how long each frame takes to calculate. (See [[Vector#Scalar|Vector]] for more detail on this operation.) | |||
== Functions == | == Functions == | ||
Line 20: | Line 38: | ||
== See also == | == See also == | ||
* [[Wikipedia:Velocity]] | |||
[[Category:Source]] | |||
[[Category: | [[Category:C++]] |
Latest revision as of 19:39, 10 April 2023
Velocity is a measure of speed and direction. In Source, it is a
Vector
that an entity will try to move the length of once per second: a velocity of (0,0,-200)
means that you are falling at 200 units per second.
Absolute, local and base
There are actually three different velocity vectors:
- Local velocity: generated by the entity
- Base velocity: applied to the entity from other sources
- Absolute velocity: the sum of the local and base velocities; the actual value used to move

Having separate local and base velocities allows for the proper handling of inertia. If an NPC is standing on a fast-moving train, the motion of the carriage is stored in its base velocity while the motion the NPC creates itself when walking up and down is stored in its local velocity. There is no danger of either value overwriting the other, or of the NPC thinking it's walking at 150MPH.

SetBaseVelocity()
.Application
When an entity moves, the code looks a little like this:
SetLocalVelocity( vecSomeValue );
SetAbsVelocity( GetLocalVelocity() + GetBaseVelocity() );
// frametime is in seconds, e.g. 0.033
SetAbsOrigin( GetAbsOrigin() + (GetAbsVelocity() * gpGlobals->frametime) );
Notice how:
- Objects don't so much travel through space as teleport a very small distance each frame.
Note:Beware an object moving so fast that it doesn't pass through intervening space, which can happen unexpectedly if the server's framerate dips too far.
Todo: How can this be best avoided? gpGlobals->frametime
is used to regulate the entity's speed regardless of how long each frame takes to calculate. (See Vector for more detail on this operation.)
Functions
GetLocalVelocity()
,SetLocalVelocity()
GetBaseVelocity()
,SetBaseVelocity()
GetAbsVelocity()
,SetAbsVelocity()