Page 1 of 1

[ANY] Make entities bounce (or hop)

Posted: Thu Apr 02, 2020 9:17 pm
by VinciT
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

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

Posted: Mon Apr 06, 2020 9:01 am
by L'In20Cible
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__.

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

Posted: Tue Apr 07, 2020 10:28 pm
by VinciT
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:

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

Posted: Wed Apr 08, 2020 1:36 pm
by L'In20Cible
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.

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

Posted: Thu Apr 09, 2020 3:44 am
by decompile
Funny idea. :D Good job