2
\$\begingroup\$

Using SDL_gfx's rotozoomSurface function to rotate a 38x58 sprite....naturally when it rotates it creates a new surface with a new size, and the rotation isn't going around the center point. I am actually having issues trying to figure out how to make it seem like its spinning around an axis...

Here is the image that I am using:

enter image description here

And here is the code that I have thus far:

  #define ANGLE_STEPS 32
  #define ANGLE_STEP  (360 / ANGLE_STEPS)
  ...
  int angle = 0;
  SDL_Rect cpos;
  cpos.x = cpos.y = 100;
  ...
  // create chopper angled surfaces
  for (int i = 0; i < ANGLE_STEPS; i++)
  {
    choppers[i] = rotozoomSurface(chopper, i * ANGLE_STEP, 1, 0);
  }
  ...

  // blit the currently requested angle
  SDL_BlitSurface(choppers[angle], NULL, screen, &cpos);
  ...

  // handle rotating the chopper
  if (keystate[keys[LEFTKEY]])
  {
    angle = (angle + 1) & (ANGLE_STEPS - 1);
    cpos.x = ?
    cpos.y = ?
  }
  if (keystate[keys[RIGHTKEY]])
  {
    angle = (angle - 1) & (ANGLE_STEPS - 1);
    cpos.x = ?
    cpos.y = ?
  }
\$\endgroup\$
2
  • 2
    \$\begingroup\$ You should look up some basic 2D linear algebra texts. The answer you figured out on your own is the basic principle of needing to translate your coordinate system to the appropriate origin before rotation. Graphics tutorials often cover this topic as well, so those may be a good place to look for a good linear algebra introduction. Sorry that I don't have a good link on hand to give you, but Google should find you some useful material. \$\endgroup\$ Commented Jan 5, 2012 at 11:38
  • \$\begingroup\$ Yea, that's a good point. Honestly been forever since I've had to use this kind of math and its a wakeup call to how much I've forgotten over time. \$\endgroup\$
    – erik
    Commented Jan 5, 2012 at 19:41

1 Answer 1

1
\$\begingroup\$

Never mind, figured it out after simply sketching it out.

I calculated the center point of both the old and new sprite to be drawn, and simply subtracted that offset from the x and y points to be drawn.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .