[ANY] Make entities bounce (or hop)

Post Python examples to help other users.
User avatar
VinciT
Senior Member
Posts: 331
Joined: Thu Dec 18, 2014 2:41 am

[ANY] Make entities bounce (or hop)

Postby VinciT » Thu Apr 02, 2020 9:17 pm

To make entities bounce (or hop), something needs to act upon the entities with a certain amount of force in order to change their velocity.
With Source.Python, the easiest way to accomplish this is to change their velocity directly with the teleport virtual function.

Syntax: Select all

entity.teleport(origin, angle, velocity)
Or with the apply_force_center function by accessing the PhysicsObject of the entity (thank you L'In20Cible).

Syntax: Select all

entity.physics_object.apply_force_center(velocity)

Image Image

Here's a very simple class that you can use to make any entity bounce randomly with ease.



And here's how'd you use this to make entities bounce:

Syntax: Select all

from events import Event


@Event('bomb_dropped')
def bomb_dropped(event):
bouncy_bomb = bouncy_instances[event['entindex']]
# The bomb will only bounce when it's on the ground, until it is picked up.
bouncy_bomb.start_bouncing(tickrate=0.3, multiplier=1.3, ground_check=True)

Syntax: Select all

from entities.entity import BaseEntity
from entities.hooks import EntityPreHook, EntityCondition


@EntityPreHook(EntityCondition.is_player, 'drop_weapon')
def drop_weapon_pre(stack_data):
base_entity = BaseEntity._obj(stack_data[1])

bouncy_weapon = bouncy_instances[base_entity.index]
# The weapon will bounce once a second no matter where it is, either in the
# air or on the ground, for the next 10 seconds or until it is picked up.
bouncy_weapon.start_bouncing(
tickrate=1,
multiplier=0.5,
ground_check=False,
duration=10
)

Image
By adjusting the bounce_multiplier, you can make entities wiggle, hit the skybox, and everything in between.

Image Image
Image Image
Last edited by VinciT on Tue Apr 07, 2020 10:33 pm, edited 1 time in total.
ImageImageImageImageImage
User avatar
L'In20Cible
Project Leader
Posts: 1533
Joined: Sat Jul 14, 2012 9:29 pm
Location: Québec

Re: [ANY] Make entities bounce (or hop)

Postby L'In20Cible » Mon Apr 06, 2020 9:01 am

That's funny! :smile:

Here are some suggestions/improvements:

  • Instead of using Repeat combined with an EntityDictionary, you could just use Entity.repeat.
  • Instead of retrieving the m_hMoveParent property, you could just do:

    Syntax: Select all

    if self.parent is not None:
  • Instead of tracking the z axis to know if the entity is on the ground, you could just use:

    Syntax: Select all

    if int(self.physics_object.velocity[0].length):
  • Instead of delaying the stop yourself, you could just pass it to the repeat and let it handle that for you:

    Syntax: Select all

    self.think.start(interval=tickrate, limit=(duration / tickrate))
  • Instead of using Entity.teleport, you could use (you may need to add more force than 300 units, though):

    Syntax: Select all

    self.physics_object.apply_force_center(velocity)
  • Should forward the caching keyword to Entity.__init__.
User avatar
VinciT
Senior Member
Posts: 331
Joined: Thu Dec 18, 2014 2:41 am

Re: [ANY] Make entities bounce (or hop)

Postby VinciT » Tue Apr 07, 2020 10:28 pm

Oh wow, thank you for the amazing suggestions, I would've never thought to use PhysicsObject. I went ahead and changed everything you mentioned.
I solved the force issue by multiplying the new velocity with the mass of the object.
L'In20Cible wrote:That's funny! :smile:
Glad you liked it. :tongue:
ImageImageImageImageImage
User avatar
L'In20Cible
Project Leader
Posts: 1533
Joined: Sat Jul 14, 2012 9:29 pm
Location: Québec

Re: [ANY] Make entities bounce (or hop)

Postby L'In20Cible » Wed Apr 08, 2020 1:36 pm

VinciT wrote:I solved the force issue by multiplying the new velocity with the mass of the object.

That's actually a great idea! That means every bounce will be consistent regardless if the object is heavier than another or not.
decompile
Senior Member
Posts: 416
Joined: Sat Oct 10, 2015 10:37 am
Location: Germany
Contact:

Re: [ANY] Make entities bounce (or hop)

Postby decompile » Thu Apr 09, 2020 3:44 am

Funny idea. :D Good job

Return to “Code examples / Cookbook”

Who is online

Users browsing this forum: No registered users and 13 guests