Adventure Game Studio

AGS Support => Modules, Plugins & Tools => Topic started by: Afflict on Thu 02/03/2006 19:56:24

Title: Rotating wheel
Post by: Afflict on Thu 02/03/2006 19:56:24
I have a rotating wheel that needs to be animated as the player spins it with the mouse wheel.

How will I go about doing this? I want to add momentum onto it aswell

EG:

(http://img330.imageshack.us/img330/6332/animation15qp.gif)

Completely stummped on it!
Title: Re: Rotating wheel
Post by: DoorKnobHandle on Thu 02/03/2006 20:05:51
I don't have time to explain the way I would do this to you in detail, but here's a very quick description, maybe someone else can go into detail:

- Look up the DynamicSprite-functions (and the DynamicSprite.rotate function particularly) in the manual (you need a version >2.71 for "DynamicSprite.rotate" I believe).
- This function allows you to rotate a sprite by any degree, first try to implement this. Only continue if you have the first part of simple rotation working.
- Then add momentum and mousewheel control. You'll need to play around with this. Just add a value to a variable when turning the mousewheel up and subtract a value to the same variable if turning the mousewheel down.
- Finally rotate the sprite by the variable.
- Then you could add momentum. Just make the variable decrease slowly by itself over time.
Title: Re: Rotating wheel
Post by: Afflict on Thu 02/03/2006 22:38:20
Ok cant find dynamicsprite functions in the manaul...  :'(

Ive tried some other methods also not working



#sectionstart on_mouse_click  // DO NOT EDIT OR REMOVE THIS LINE
function on_mouse_click(MouseButton button) {
  // called when a mouse button is clicked. button is either LEFT or RIGHT
  if (IsGamePaused() == 1) {
    // Game is paused, so do nothing (ie. don't allow mouse click)
  }
  else if (button == eMouseLeft) {
    ProcessClick(mouse.x, mouse.y, mouse.Mode );
  }
  else  {
oCircle.Graphic = oCircle.Graphic+1 ;
  mouse.SelectNextMode();
if (oCircle.Graphics > 13) oCircle.Graphics = 10 ;
 
}
}
#sectionend on_mouse_click



Its giving me the undefined token error 'cCircle'
edit ok I got the circle working many thanks to ASHEN now working on momentum.
Title: Re: Rotating wheel
Post by: Kweepa on Thu 02/03/2006 23:43:27

// PUT THIS AT THE TOP OF THE GLOBAL SCRIPT
float wheelAngularVelocity = 0.0;
float wheelAngle = 0.0;

#define SPRITE_WHEEL 0 // change to whatever yours is

// MERGE THIS WITH YOUR on_mouse_click
function on_mouse_click(MouseButton button)
{
  // you need to enable the mouse wheel support
  if (button == eMouseWheelNorth)
  {
    wheelAngularVelocity += 25.0;
  }
  else
  if (button == eMouseWheelSouth)
  {
    wheelAngularVelocity -= 25.0;
  }
}

// MERGE THIS WITH YOUR repeatedly_execute
function repeatedly_execute()
{
  float timeStep = 1.0/IntToFloat(GetGameSpeed());

  float deceleration = 5.0;
  if (wheelAngularVelocity > 0.0)
  {
    wheelAngularVelocity -= timeStep*deceleration;
    if (wheelAngularVelocity < 0.0) wheelAngularVelocity = 0.0;
  }
  else
  {
    wheelAngularVelocity += timeStep*deceleration;
    if (wheelAngularVelocity > 0.0) wheelAngularVelocity = 0.0;
  }

  wheelAngle += timeStep*wheelAngularVelocity;
  while (wheelAngle < 0.0) wheelAngle += 360.0;
  while (wheelAngle >= 360.0) wheelAngle -= 360.0;
 
  int angle = FloatToInt(wheelAngle);

  // draw your wheel - here I'm using a dynamic sprite
  // but you could use a lookup table and a set of sprites
  DynamicSprite *wheelSprite = DynamicSprite.CreateFromExistingSprite(SPRITE_WHEEL);
  if (angle > 0 && angle < 360)
  {
    wheelSprite.Rotate(angle);
  }
  RawDrawImage(100, 100, wheelSprite.Graphic);
  wheelSprite.Delete();
}
Title: Re: Rotating wheel
Post by: Afflict on Sun 05/03/2006 19:52:36
Thanks to steveMcCrea and Ashen we have 2 working module.

The one uses all the rendered frames to simulate the rotation and the other one uses a single dynamic sprite and rotates it. The problem being that its not on a single axis. Its flying all over the place.

Anybody have any idea on how to fix it?

Note the sprite is perfectly centred!
Title: Re: Rotating wheel
Post by: Ashen on Sun 05/03/2006 22:30:45
It's probably because the rotated sprite isn't the same width/heigth as the original.
You'd need to change the position of the RawDrawImage (from the line RawDrawImage(100, 100, wheelSprite.Graphic); to account for that, e.g.:


RawDrawImage(100-(wheelSprite.Width/2), 100-(wheelSprite.Height/2), wheelSprite.Graphic);


should centre the wheel, so it spins about (100, 100), rather than having it's top-left corner there.
Title: Re: Rotating wheel
Post by: Afflict on Sun 05/03/2006 22:56:47
Hey thanks ashen that worked! Prefectly I am still not sure what it did but WOW appreciated!

Now it still wobles about 1 pixel i dont mind but the smudging is annoying! Any ideas?
Title: Re: Rotating wheel
Post by: Scorpiorus on Sun 05/03/2006 23:40:11
If you mean it's jittering, that may probably be caused by some truncation/rounding errors.


As for smudging, you need to restore the room's background image before drawing the wheel:

RawRestoreScreen(); // just before drawing the wheel
RawDrawImage(100-(wheelSprite.Width/2), 100-(wheelSprite.Height/2), wheelSprite.Graphic);


Also, put the following line under Player enters room (after fade-in):
RawSaveScreen();
Title: Re: Rotating wheel - Solved!
Post by: Afflict on Mon 06/03/2006 06:03:16
Thank you ALL!