Shake a 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
AERenoir
Veteran
Posts: 320
Joined: Fri May 27, 2011 8:23 pm
Contact:

Shake a sprite

#1 Post by AERenoir »

Is it possible to do a hpunch-like effect on a single sprite, not on the entire screen? What I'm trying to accomplish is two sprites on screen bumping into each other. So one sprite runs into the other and the "bumped" sprite shakes.

Right now I've used a custom-defined MoveTransition and shifting the sprite's xpos, but if there's a more efficient way I'd like to know it.

User avatar
PyTom
Ren'Py Creator
Posts: 16088
Joined: Mon Feb 02, 2004 10:58 am
Completed: Moonlight Walks
Projects: Ren'Py
IRC Nick: renpytom
Github: renpytom
itch: renpytom
Location: Kings Park, NY
Contact:

Re: Shake a sprite

#2 Post by PyTom »

ATL would be a good way to do it.
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
Software > Drama • https://www.patreon.com/renpytom

User avatar
AERenoir
Veteran
Posts: 320
Joined: Fri May 27, 2011 8:23 pm
Contact:

Re: Shake a sprite

#3 Post by AERenoir »

Oh, which ATL command would you say is best? I'm still no good with ATL, unfortunately >.<

Elmiwisa
Veteran
Posts: 476
Joined: Sun Jul 21, 2013 8:08 am
Contact:

Re: Shake a sprite

#4 Post by Elmiwisa »

Code: Select all

transform hpunch_approximant:
    linear 0.2 xoffset -20 #move left 20 pixel in 0.2 seconds
    linear 0.2 xoffset +20 #move right 20 pixel in 0.2 seconds
    repeat 5 #repeat the above 5 times
This is just my approximation of hpunch, it's too fast for me to see clearly.

User avatar
ShippoK
Veteran
Posts: 348
Joined: Wed Mar 28, 2012 8:59 pm
Projects: Eyes of Gold (In-Progress)
Organization: (Red Moon)
Location: Glorious Nippon (A.K.A the USA)
Contact:

Re: Shake a sprite

#5 Post by ShippoK »

Elmiwisa wrote:

Code: Select all

transform hpunch_approximant:
    linear 0.2 xoffset -20 #move left 20 pixel in 0.2 seconds
    linear 0.2 xoffset +20 #move right 20 pixel in 0.2 seconds
    repeat 5 #repeat the above 5 times
This is just my approximation of hpunch, it's too fast for me to see clearly.
Fast, more like too slow? For a faster shake try something around 0.090 or less.
Also, the +offset moves the character to the side while -offset keeps the character where they are.
Unless you want to move your character keep the +offset at 0/None.

Also, You don't need the transform hpunch_approximant: you can just use your image itself.

Code: Select all

ExampleCharacter_image:
        linear 0.090 xoffset -10 #-offset to keep Character in place
        linear 0.090 xoffset +0 #+offset to move Character
        repeat 4 #Repeats

#linear = Pause  (0.1 is one Second) (0.01 is one millisecond)
Hope this helps makes it more understanding.
(Note; I am not an expert)
Much Appreciated for those who took the Survey!

Image Image
Eyes of Gold [BL (Suspense) (+18)] IN-PROGRESS
Alternate Online [(Friendship) (+10)] ON-HOLD
DeviantART• •[Honest critique] - 'Honesty is its own reward.'

Elmiwisa
Veteran
Posts: 476
Joined: Sun Jul 21, 2013 8:08 am
Contact:

Re: Shake a sprite

#6 Post by Elmiwisa »

ShippoK wrote: Fast, more like too slow? For a faster shake try something around 0.090 or less.
Also, the +offset moves the character to the side while -offset keeps the character where they are.
Unless you want to move your character keep the +offset at 0/None.

Also, You don't need the transform hpunch_approximant: you can just use your image itself.

Code: Select all

ExampleCharacter_image:
        linear 0.090 xoffset -10 #-offset to keep Character in place
        linear 0.090 xoffset +0 #+offset to move Character
        repeat 4 #Repeats

#linear = Pause  (0.1 is one Second) (0.01 is one millisecond)
Hope this helps makes it more understanding.
(Note; I am not an expert)
I agree with you that 0.2 is slow, but as I said, hpunch move too fast for me to actually time how much time the shake take, so I can't get an accurate value for timing. It can be adjusted easily if it looks slow.
As for the rest of your post:

........................................................................................ :roll:

1. Are you trying to claim that setting a value with a + to xoffset do something completely different from setting a value with a -? These are just stylistic differences you know.
2. The transform is because AERenoir want a character to shake went bumped into, not shaking right went the character first appeared.
3. linear is not the same as Pause. Pause (or more correctly, pause without the capital) do nothing, linear actually move the character. If the shaking is really fast it probably won't make a difference because nobody can see fast enough. But to get as similar to hpunch as possible, then it's a linear here.
4. 1 is 1 second. 0.1 is 0.1 second. 0.01 is 0.01 second. It's very direct. And "millisecond" means 0.001 second, in case you get that word wrong.

User avatar
AERenoir
Veteran
Posts: 320
Joined: Fri May 27, 2011 8:23 pm
Contact:

Re: Shake a sprite

#7 Post by AERenoir »

Ooh, I'll try that. So where do I put this ATL block? *I fail* >.<

Elmiwisa
Veteran
Posts: 476
Joined: Sun Jul 21, 2013 8:08 am
Contact:

Re: Shake a sprite

#8 Post by Elmiwisa »

Uh, it's a transform statement, so put it around where you declare your image, just to make it easier to organize. It doesn't matter where you put it really, as long as it's not inside some block.

User avatar
AERenoir
Veteran
Posts: 320
Joined: Fri May 27, 2011 8:23 pm
Contact:

Re: Shake a sprite

#9 Post by AERenoir »

So the transform has to be declared at the init, right? And then when I need the sprite to shake I say "show charactersprite with pseudo-hpunch"?

Elmiwisa
Veteran
Posts: 476
Joined: Sun Jul 21, 2013 8:08 am
Contact:

Re: Shake a sprite

#10 Post by Elmiwisa »

Yes.
If you don't put the transform statement in a block, it automatically run as if it is in an init block.

User avatar
rusicaria
Regular
Posts: 84
Joined: Sat Mar 28, 2020 12:26 am
Projects: VivaCity (VN)
Tumblr: rusicariaGAMES
itch: rusicariaGAMES
Contact:

Re: Shake a sprite

#11 Post by rusicaria »

Hey, so I know this is an old thread, but seeing as I spent ages trying to find an answer I figured I'd reply with what I found here:

viewtopic.php?t=19429

Donmai's reply basically explains it step by step, so for anyone who is extremely new to coding, if you're going to use the tranform command, make sure to define the command first:

Code: Select all

init python:
    define.move_transitions("shake/whatever you want to name it", 1.0)
and then script in the transform command:

Code: Select all

transform shake:
    ease .06 xoffset 24
    ease .06 xoffset -24
    ease .05 xoffset 20
    ease .05 xoffset -20
    ease .04 xoffset 16
    ease .04 xoffset -16
    ease .03 xoffset 12
    ease .03 xoffset -12
    ease .02 xoffset 8
    ease .02 xoffset -8
    ease .01 xoffset 4
    ease .01 xoffset -4
    ease .01 xoffset 0
btw if you want the sprite to shake vertically rather than horizontally, just change all the xoffsets's to yoffsets's, and credit to Donmai who's coding I used for this.

then to use on a sprite:

Code: Select all

label example:
    show character at shake

User avatar
Donmai
Eileen-Class Veteran
Posts: 1958
Joined: Sun Jun 10, 2012 1:45 am
Completed: Toire No Hanako, Li'l Red [NaNoRenO 2013], The One in LOVE [NaNoRenO 2014], Running Blade [NaNoRenO 2016], The Other Question, To The Girl With Sunflowers
Projects: Slumberland
Location: Brazil
Contact:

Re: Shake a sprite

#12 Post by Donmai »

rusicaria wrote: Sun Apr 18, 2021 7:02 pmand credit to Donmai
Thanks, but I only wrote the detailed instructions. The transform itself was written by nyaatrap
viewtopic.php?p=251409#p251409
Image
No, sorry! You must be mistaking me for someone else.
TOIRE NO HANAKO (A Story About Fear)

Post Reply

Who is online

Users browsing this forum: Google [Bot]