Get units/s

Please post any questions about developing your plugin here. Please use the search function before posting!
decompile
Senior Member
Posts: 416
Joined: Sat Oct 10, 2015 10:37 am
Location: Germany
Contact:

Get units/s

Postby decompile » Fri Jan 15, 2016 1:47 pm

HeyGuys,

Just want to ask how you convert the velocity to units per second?

Like in ES you could use:

Syntax: Select all

vecmath.vector(525, 3500, 10).length()


Syntax: Select all

def playerVelocity(self):
return PlayerEntity(self.index).velocity


and it seems like it has no .length() attribute and it only returns the vector.
User avatar
iPlayer
Developer
Posts: 590
Joined: Sat Nov 14, 2015 8:37 am
Location: Moscow
Contact:

Postby iPlayer » Fri Jan 15, 2016 1:57 pm

Given the vector v(x, y, z), you can calculate its length this way:

Image

I believe SP provides .get_length method for Vectors
Image /id/its_iPlayer
My plugins: Map Cycle • Killstreaker • DeadChat • Infinite Jumping • TripMines • AdPurge • Bot Damage • PLRBots • Entity AntiSpam

Hail, Companion. [...] Hands to yourself, sneak thief. Image
decompile
Senior Member
Posts: 416
Joined: Sat Oct 10, 2015 10:37 am
Location: Germany
Contact:

Postby decompile » Fri Jan 15, 2016 2:18 pm

Thank you, worked.

Syntax: Select all

return PlayerEntity(self.index).velocity.get_length()
User avatar
satoon101
Project Leader
Posts: 2697
Joined: Sat Jul 07, 2012 1:59 am

Postby satoon101 » Fri Jan 15, 2016 2:20 pm

iPlayer wrote:I believe SP provides .get_length method for Vectors

Indeed:
https://github.com/Source-Python-Dev-Team/Source.Python/blob/master/src/core/modules/mathlib/mathlib_wrap.cpp#L139

Though we could (should?) change that to be a property named length.
Image
User avatar
iPlayer
Developer
Posts: 590
Joined: Sat Nov 14, 2015 8:37 am
Location: Moscow
Contact:

Postby iPlayer » Fri Jan 15, 2016 3:30 pm

What about __len__ method? Or is it too implicit?
Image /id/its_iPlayer
My plugins: Map Cycle • Killstreaker • DeadChat • Infinite Jumping • TripMines • AdPurge • Bot Damage • PLRBots • Entity AntiSpam

Hail, Companion. [...] Hands to yourself, sneak thief. Image
necavi
Developer
Posts: 129
Joined: Wed Jan 30, 2013 9:51 pm

Postby necavi » Fri Jan 15, 2016 4:11 pm

__len__ is pretty explicitly for containers only. I don't see much benefit to doing len(vector) over vector.length, really.
decompile
Senior Member
Posts: 416
Joined: Sat Oct 10, 2015 10:37 am
Location: Germany
Contact:

Re: Get units/s

Postby decompile » Mon Apr 25, 2016 9:53 pm

How can I get the the units without z? So the actuall forward speed?
User avatar
iPlayer
Developer
Posts: 590
Joined: Sat Nov 14, 2015 8:37 am
Location: Moscow
Contact:

Re: Get units/s

Postby iPlayer » Mon Apr 25, 2016 10:08 pm

Use the first formula from the picture I posted
Image /id/its_iPlayer
My plugins: Map Cycle • Killstreaker • DeadChat • Infinite Jumping • TripMines • AdPurge • Bot Damage • PLRBots • Entity AntiSpam

Hail, Companion. [...] Hands to yourself, sneak thief. Image
User avatar
satoon101
Project Leader
Posts: 2697
Joined: Sat Jul 07, 2012 1:59 am

Re: Get units/s

Postby satoon101 » Mon Apr 25, 2016 10:19 pm

To easily do that, you can always zero out the z value of the Vector:

Syntax: Select all

velocity = Player(index).velocity
velocity.z = 0
return velocity.length

Return to “Plugin Development Support”

Who is online

Users browsing this forum: No registered users and 136 guests