[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Not sure what to call this.



Hi

the easiest way tho position an rotated image a the same spot as before is using a rect (leaving the center of the rect at the same sport):
--------- pseudo code----------
rect.center = (posx, posy) # center is at the coordinates you want
...
rotated_image = pygame.transform.rotozoom(image, angle, 1.0)
rect = rotated_image.get_rect(center=rect.center)
...
blit(rotated_image, rect)
------------------------------
Using this code the center of the image will be at the same position as before. It is a bit more complicated if the any other position of the image has to stay at the same coordinates.

Here is an example I found on the pygame site: http://www.pygame.org/projects/7/412/

Basically you will have to rotate a point around the given axis (the point is preferable the center of the image ;-) ). Take a good look at line 21 and following in JumpingJack.py: def rot_point(point, axis, ang)



I hope that helps ;-)

~DR0ID




Brian Fisher schrieb:
What you describe sounds like the most basic and simple example of
"Inverse Kinematics" - which basically means determining how you would
arrange the parts of a system like a jointed body so that the end
points (like say hands and feet) satisfy some set of criteria. Inverse
Kinematics is a very google-searchable topic.

But speaking to pygame and your simple example specifically - the two
functions of interest are probably math.atan2 and
pygame.transform.rotozoom, and they would let you figure out the angle
the arm should be at, and draw the arm image rotated, respectively.

Here's like some non-working incomplete code that should give the idea:
  angle = math.atan2(mouse.x - shoulder.x, mouse.y - shoulder.y)
  rotated_arm = pygame.transform.rotozoom(arm_image, angle, 1.0)
  display.blit(rotated_arm, (shoulder.x, shoulder.y))

the ugly stuff I glossed over is how you get the arm image so that it
lines up with the shoulder right, cause rotozoom returns an image that
is bigger than the original, and I forget exactly how to deal with it.
The archives for this list have various cases of people asking about
how to use rotozoom, with helpful explanations.

On 9/15/07, Lamonte Harris <pyth0nc0d3r@xxxxxxxxx> wrote:
Basically lets give a real live example.  When your standing up and you move
your arm at an diagonal the socket of the bone doesn't move but your arm
does, lets put this back to pygame.  How would Say if K have a simple sprite
that has 2 parts body,1 arm.  How would say if that sprite is in the middle
of the screen don't move but when I move the mouse the arm points in the
direction of where the mouse is located, but doesn't exactly move to that
position, how would that exactly work?