123
-=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- (c) WidthPadding Industries 1987 0|384|0 -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=-
Socoder -> Blitz Max -> Rotation with Parent and Children

Mon, 03 Jan 2022, 23:59
therevillsgames

Rotation with Parent and Children


I'm working on a Jigsaw game and want to rotate the connected pieces in a nice smooth animation by 90 degrees each time around the selected piece...

My current idea is to change the image handles, is there a better way than what I am trying... as it's driving me crazy! O_O

test.png is


Tue, 04 Jan 2022, 04:15
Jayenkai
Hmmm.. I would do the whole thing in a completely different manner.
(*sorry!!)

In Space Invaders, you don't move each alien, and try to position it in the right place. You clump the aliens into an array, then move the array's offset.
I *think* that's roughly what I'd be doing here..

Start with two arrays (or lists, if you prefer!!)
Piece : x,y,rotation, clusterID, clusterAngle, clusterDistance
Cluster : x,y,rotation

Assign each piece its own cluster, and an Angle/Distance of 0.
If you connect two, Change Piece A's cluster ID to be the same as Piece B's cluster, and find it's Angle/Distance from the center of the middle of the cluster. (Which should be Piece B's position.)

When drawing, find the cluster position for that piece, then offset (rotation of the cluster + angle of the piece)*distance of the piece

When lifting a cluster, do some squiggly maths to find a handle for that cluster, and move/rotate the cluster based on where it is in relation to the handle. Then all your pieces should very easily fall into place, based on the cluster's new location/orientation.

(I'd GotoJSE an example, but I still have my morning blog+pixelart to do!! Might do later!!)

-=-=-
''Load, Next List!''
Tue, 04 Jan 2022, 06:29
Jayenkai
  --v

This example started well enough, but got heavily convoluted along the way.

"Why don't you open source any of your code, Jay?" they ask, repeatedly. This is why!!

Hold a piece with the mouse, and hit Z or X to spin it right round baby, right round..

-=-=-
''Load, Next List!''
Tue, 04 Jan 2022, 15:07
therevillsgames
Thanks Jay, always good to see how another dev goes about solving a problem

With your rotation it looks like it always rotates around the center one, I'm trying to rotate it around the one that is selected... I'll keep plugging away
Tue, 04 Jan 2022, 15:15
Jayenkai
You have the angle and distance between mouse and "the middle of the cluster."
All you have to do is rotate the middle of the cluster around the mouse co-ords. My brain was fried by that point, but it should be easily do-able

-=-=-
''Load, Next List!''
Tue, 04 Jan 2022, 15:34
therevillsgames
Yeah I'm pretty poor with the whole trig stuff And it doesnt help that Blitz works in degrees and has Y going down O_o
Tue, 04 Jan 2022, 15:38
therevillsgames
Actually what I think I need is the formula to calculate the final x and y after the rotation.

So I know the target rotation and I know the difference between each piece... now where did I leave my school maths books
Tue, 04 Jan 2022, 15:46
Jayenkai
Degrees are easy. 1 isn't enough. 90 is a quarter of the cake. 180 is enough of the cake. 270 is a good amount of cake. 360 is my slice of cake.


This .. "Works"..
But the maths and sudden jumping, etc, leaves a lot to be desired.

> Reveal 🔎

-=-=-
''Load, Next List!''
Tue, 04 Jan 2022, 17:19
shroom_monk
+1 for Jay's approach of treating each connected cluster of pieces as a single object and just operating on those clusters - that's how I'd approach this too.

Personally however, rather than storing the angle and distance from the middle of the cluster for each piece (i.e. polar coordinates), I would instead store the (x,y) vector relative to some arbitrary handle point in the cluster (i.e. cartesian coordinates). The advantage of this is that it's then easier to reason about (IMO anyway), and easier to rotate the cluster around any arbitrary point that isn't the middle of it.

I probably shouldn't be attempting to write formulae at quarter past midnight without expecting them to be full of errors, though I'd probably just be reformulating what Jay's already written anyway. Happy to give it a shot another day though, if desired.

(FWIW, I went and wrote some maths tutorials about vectors, trig, etc many years ago on SoCoder. I have no idea if they actually make sense to anyone who's not me, but who knows, they might come in handy...)

-=-=-
A mushroom a day keeps the doctor away...

Keep It Simple, Shroom!
Tue, 04 Jan 2022, 18:56
therevillsgames
Thanks Guys... Shroom I'll have a look.

Currently gone back to basics with trig and converted it over to JSE. So I have just two pieces, connected, and I want to rotate them relative to each over.

So


Seems nearly there... but if I update the connected piece to those coords the next time the trig doesnt work.




https://GotoJSE.com/hQfdSWJ5.BAS
Tue, 04 Jan 2022, 20:36
therevillsgames
Ah.... watch this video: https://www.youtube.com/watch?v=AmaC23gQCTw

And I nearly had it... but I was using the final angle not the delta of the angle, so updated the code to:





https://GotoJSE.com/5kDsQlXH.BAS
Tue, 04 Jan 2022, 20:39
therevillsgames
Now to get it moving in a nice arc...
Wed, 05 Jan 2022, 12:56
Jayenkai
.. That could totally be a game. Rotational walker of some sort, dragging its feet along a maze-like path..

-=-=-
''Load, Next List!''
Wed, 05 Jan 2022, 20:26
therevillsgames
Yeah the movement is pretty cool

Here is the final BlitzMax code working:


And the updated JSE:

https://GotoJSE.com/5kDsQlXH.BAS
Thu, 06 Jan 2022, 01:44
Jayenkai
Congrats! Smooth like butter
Thu, 06 Jan 2022, 15:44
therevillsgames
Thanks! And barring the constant issue I enjoyed porting it to JSE
Fri, 21 Jan 2022, 09:35
Jayenkai


Guilt trip, much?

I feel I have to post this, before everyone lynches me for pinching the idea of making a jigsaw puzzle.
I was actually asked if I could do a jigsaw, using my Platdude pixelarts, mid December when I was knee deep in Advent stuff.
I set it aside until new year, then this thread appeared and I felt that doing a jigsaw, RIGHT AFTER helping TheRevillsGames with his, would've looked like I'm just nabbing it.
But.. maaan, I really wanna do it!

So, I hope TheRevillsGames doesn't object to me doing a jigsaw puzzle.
I didn't pinch it, honest!!

-=-=-
''Load, Next List!''
Fri, 21 Jan 2022, 13:49
therevillsgames
LoL, of course not Jay! It just a jigsaw game, nothing mind blowing new etc and you didn't sign the NDA I sent