[Solved] Shake/Vibrate Sprite

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Post Reply
Message
Author
User avatar
Semicolonkid
Regular
Posts: 26
Joined: Tue Feb 14, 2017 6:13 pm
Projects: Summoned
itch: semicolonkid
Contact:

[Solved] Shake/Vibrate Sprite

#1 Post by Semicolonkid » Wed May 16, 2018 12:49 pm

Hello!
I'm trying to figure out a way to make a character sprite shake/vibrate around in place to represent frustration or quivering in fear.
I dug around for an answer and found this, and added vertical movement to get this:

Code: Select all

transform shake:
    linear 0.090 xoffset -10
    linear 0.090 xoffset +0
    linear 0.090 yoffset -10
    linear 0.090 yoffset +0
    repeat
It's close, but it's not quite what I'm after. The sprite will move left, right, up, down, repeat, drawing a "corner" shape over and over. I want a more erratic shake in any direction.
I tried using Choice statements to break them up in various ways, but that usually just made the sprite move in a more robotic fashion (which admittedly was very cool and made me wish my story had sci-fi elements).

Any thoughts on how to achieve a more erratic look? Doesn't have to be random, as long as it looks like shaking on both the X and Y axis.
Last edited by Semicolonkid on Wed May 16, 2018 2:50 pm, edited 1 time in total.
Image

kivik
Miko-Class Veteran
Posts: 784
Joined: Fri Jun 24, 2016 5:58 pm
Contact:

Re: Shake/Vibrate Sprite

#2 Post by kivik » Wed May 16, 2018 1:55 pm

To start with, you can add multiple movements in the same linear statement:

Code: Select all

transform shake:
    linear 0.090 xoffset -10 yoffset -10
    linear 0.090 xoffset +10 yoffset +10
    repeat
I'm not very familiar with shaking effects, but I think randomising which direction / corner your image will move towards will always create weird effects - because a shake should always orbit a centre point or it's not a shake anymore. So if it starts top left it needs to go bottom right and back to top left. Of course you don't have to pass the centre point and but move in the general opposite direction at a slight angle shift - but I suspect this is too much work for what it's worth.

One thing you can do to randomise it is just how far the shakes go. This thread shows how to create a function to be used within a transform that achieves that: but you'll want a condition to toggle between top left and bottom right (if that's the direction you want to go for) in scale, so if you've just gone for bottom right (renpy.random.randint(5, 10) then the next set should be renpy.random.randint(-10, -5).

Hope that helps?

User avatar
Semicolonkid
Regular
Posts: 26
Joined: Tue Feb 14, 2017 6:13 pm
Projects: Summoned
itch: semicolonkid
Contact:

Re: Shake/Vibrate Sprite

#3 Post by Semicolonkid » Wed May 16, 2018 2:36 pm

kivik wrote:
Wed May 16, 2018 1:55 pm
Hope that helps?
Yeah! You make a good point about the randomness, and you're right that moving on two axes in one statement is necessary for this effect.
After some experimenting, I've found something I'm quite pleased with:

Code: Select all

transform shake(rate=0.090):
    linear rate xoffset 2 yoffset -6
    linear rate xoffset -2.8 yoffset -2
    linear rate xoffset 2.8 yoffset -2
    linear rate xoffset -2 yoffset -6
    linear rate xoffset +0 yoffset +0
    repeat
Basically, I worked out the rough coordinates that a 5-pointed star has, and tried to draw the shape over and over.
I further tried randomizing the distance, but with values as low as these, it wasn't very noticeable. The one augmentation I would consider further would be adding a "strength" argument that's multiplied with all the offsets, defaulting to 1.

So, problem solved! Thanks!
Image

Post Reply

Who is online

Users browsing this forum: No registered users