Rotation around point other than origin.

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
laserpanda
Prole
Posts: 2
Joined: Sat Jun 29, 2013 6:51 am

Rotation around point other than origin.

Post by laserpanda »

I'm making a little hockey game, and I'm having trouble making the stick rotate as illustrated in the picture.
owZvrdb.png
owZvrdb.png (3.29 KiB) Viewed 453 times
where the stick rotates from the same point in the skaters hand regardless of the skater's rotation, but I'm not really sure where to start. If possible, I'd also like to keep the stick from rotating through the player's body no matter which direction they're facing.
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Rotation around point other than origin.

Post by raidho36 »

Thers's geometry solution to that, but this is acheived easily via transformations, since they stack on top of one another. Transform your player to desired rotation and position, render it, then add up another transform for the stick (relative to the player, this what's the thing of transform stacking) and render it too.
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Rotation around point other than origin.

Post by micha »

First there is a simple, but not beautiful solution: Draw (in you graphics program, not in LÖVE) the player with different angles for the stick and then compose the animation from these images. That way you only have one image, which is the player together with the stick and this is rotated around the same point all the time.

Now the proper, but more difficult solution:
First find the coordinates of the joint (between stick and player), relative to the players origin (I use the center of his head). Let's say in this case this is 20 pixels to the right and 30 pixels up (just as an example). So

Code: Select all

deltaX = 20
deltaY = -30
Now if you rotate the player around the origin with a certain angle, then the relative coordinates of the joint are

Code: Select all

deltaXrotated = deltaX * cos(angle) - deltaY * sin(angle)
deltaYrotated = deltaX * sin(angle) + deltaY * cos(angle)
Which leads to the absolute position of the joint:

Code: Select all

jointX = playerX + deltaXrotated
jointY = playerY + deltaYrotated
And the modified angle of the stick is

Code: Select all

angleModified = angle + angleStick
User avatar
Lafolie
Inner party member
Posts: 809
Joined: Tue Apr 05, 2011 2:59 pm
Location: SR388
Contact:

Re: Rotation around point other than origin.

Post by Lafolie »

Couldn't this be easily solved with use of Pythagoras Theorem?
Do you recognise when the world won't stop for you? Or when the days don't care what you've got to do? When the weight's too tough to lift up, what do you? Don't let them choose for you, that's on you.
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Rotation around point other than origin.

Post by micha »

Pythagoras Theorem gives you the lengths of a diagonal line, given the coordinates of the end points. In this case the lengths and the angles is sort of given and the coordinates of the end point (the joint) has to be found.
laserpanda
Prole
Posts: 2
Joined: Sat Jun 29, 2013 6:51 am

Re: Rotation around point other than origin.

Post by laserpanda »

micha wrote:First there is a simple, but not beautiful solution: Draw (in you graphics program, not in LÖVE) the player with different angles for the stick and then compose the animation from these images. That way you only have one image, which is the player together with the stick and this is rotated around the same point all the time.

Now the proper, but more difficult solution:
First find the coordinates of the joint (between stick and player), relative to the players origin (I use the center of his head). Let's say in this case this is 20 pixels to the right and 30 pixels up (just as an example). So

Code: Select all

deltaX = 20
deltaY = -30
Now if you rotate the player around the origin with a certain angle, then the relative coordinates of the joint are

Code: Select all

deltaXrotated = deltaX * cos(angle) - deltaY * sin(angle)
deltaYrotated = deltaX * sin(angle) + deltaY * cos(angle)
Which leads to the absolute position of the joint:

Code: Select all

jointX = playerX + deltaXrotated
jointY = playerY + deltaYrotated
And the modified angle of the stick is

Code: Select all

angleModified = angle + angleStick
The simple solution wouldn't work, because I want the stick to actually physically interact with the puck, rather than the traditional 2d hockey game approach of having the puck object disappear when the player touches it, and the player sprite switched to a player-with-puck sprite. The more complex solution seems like the right way to accomplish what I was trying to do. I'll see how it works.
Post Reply

Who is online

Users browsing this forum: No registered users and 8 guests