Animating a hammer to move back and forth..?
Lokitoes
Member, PRO Posts: 187
Hi all,
I'd like to make a simple actor of a hammer that swings back and forth, as if someone was using it to hammer something. I'm hoping to do this without making a frame-by-frame animation. Is there a way to, say, control the angular velocity so that it swings back and forth rather than completing a full circle? Or some other attribute? I'd like it to rotate forwards by about 90 degrees, then back to its starting point, and for this to loop.
I'm going to eventually have it on a move rule so that the player can move it around, so I'd like it to continue this angular movement both when it isn't being used, and when other rules are being applied to it.
Cheers!
Comments
maybe interpolate. Not sure if you will need timers but I'm sure you can do it.
This thread might be helpful:
http://forums.gamesalad.com/discussion/66887/pendulum-movement-formula
http://jamie-cross.net/posts/ ✮ Udemy: Introduction to Mobile Games Development ✮ Learn Mobile Game Development in One Day Using Gamesalad ✮ My Patreon Page
Empathy points off for the use of the mathematically arbitary phrase 'rotate forwards' !?
You just need to constrain the hammer to an orbit that itself is constrained to just the upper values of an incrementing angle's sin function.
Here a 'hammer' is assumed to be a single image that is oriented so that the head of the hammer is at the 0° position (3 o'clock / 'pointing' right) - so an actor that is wider than it is taller as the hammer is effectively lying on its side.
The rules should look something like this:
Constrain X position to:
half the hammer's width multiplied by the cos of (range of movement abs(sin(anglespeed multiplier )))+offset value X
Constrain Y position to:
half the hammer's width multiplied by the sin of (range of movement abs(sin(anglespeed multiplier )))+offset value Y
Constrain Rotation to:
range of movement abs(sin(anglespeed multiplier ))
. . . . .
If you want the hammer to continuoulsy loop then take the lazy option and just make the 'angle' = self.time (or game.time) so the angle just keeps increasing and your hammer keeps swinging.
. . . . .
Simply use the offset values to control the offset movement of the hammer.
Hold on, let me make a quick demo, back in a minute . . . .
Here's the basic movement (file attached)
Actual constrain values used (for a 200 x 100 pixel actor)
X = 100 *cos(90 *abs(sin( self.Time *300)))+ X offset
Y = 100 *sin(90 *abs(sin( self.Time *300)))+ Y offset
R = (90 *abs(sin( self.Time *300)))
. . . . . .
The 100 is half the width of the hammer (effectively half the height as the hammer is on its side).
The 90 is a 90° range of movement - turn this down (for example to something like 20) and you will get a gentle hammering action, turn it up above 90 and it will move more towards a manic hammering action.
The *300 controls the speed of the hammering.
The offset values are the absolute offsets, use these values to move the whole system around the screen - for example if you use mouse position X for the X offset and mouse position Y for the Y offset the swinging hammer will follow the mouse.
Here is the same idea, but I've thrown in a 'grab box' with which you can move the hammer around, obviously you'd make this grab box invisible, I left it visible just so you could see it. File attached. You could of course move the hammer by clicking on the hammer itself (rather than a controller like this grab box), but with the hammer swinging it can be tricky to grab.
Cool thanks guys I'll give this and the pendulum movement a go. haha sorry about the wording - never learnt maths or science at high school - I'm from rural Australia, which probably speaks for itself.
Thanks for that example @Socks I'll definitely be pillaging from that
Not sure if you can view this, but this 3D projection mapped artwork was the outcome of this thread's mission I don't have the proper footage yet, this is just a bit of footage taken by a stranger on instagram, but basically the artwork 'falls down' and the hammers come along and fix it back into place. Not interactive though, as it's an impossible struggle to get .app files and my mapping software to talk to each other.