We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
Rotating Sprite (Read 433 times)
Rotating Sprite
Jun 18th, 2007, 7:36am
 
Hey,

I'm trying to rotate a sprite to follow the mouse. There is only Corner and corners image modes and this, as I can see is the problem. Just trying to do it in the Sprite example before I apply it. Have looked at arctangent but to no avail. Any help would be great.

Re: Rotating Sprite
Reply #1 - Jun 18th, 2007, 9:31am
 
 
Ok this works alright. Sure its not the best way but meh.




PImage teddy;
 
 

Teddy[] myTeddy;
float xpos;
float ypos;
float drag = random(30, 100);
 
void setup()
{
 size(600,600);
 frameRate(60);
 teddy = loadImage("teddy.gif");
 myTeddy = new Teddy[3];
 
 for (int i = 0; i < 3; i++)
 {
   myTeddy[i] = new Teddy();
}

 
}

void draw()
{
 background(102);
 
 float difx = mouseX - xpos-teddy.width/2;
 if(abs(difx) > 1.0) {
   xpos = xpos + difx/drag;
   xpos = constrain(xpos, 0, width-teddy.width);
 }  
 
 float dify = mouseY - ypos-teddy.height/2;
 if(abs(dify) > 1.0) {
   ypos = ypos + dify/drag;
   ypos = constrain(ypos, 0, height-teddy.height);
 }
 
 for (int i = 0; i < 3; i++)
 {
   myTeddy[i].update(mouseX, mouseY);
   myTeddy[i].run();
 }

 
 
}

class Teddy
{
 

 float angle = 0.0;
 float ex, ey;
 
 Teddy(){
 ex = xpos;
 ey = ypos;
 }
 
 void update(float mx, float my)
 {
   angle = atan2(my - height/2, mx - width/2) ;
 }
 
 void run()
 {
 pushMatrix();  

 translate( xpos+teddy.width/2, ypos+teddy.height/2,);
 rotate(angle - degrees(90));
 translate( -teddy.width/2, -teddy.height/2);
 image(teddy, 0, 0);

 popMatrix();


}}
Page Index Toggle Pages: 1