perform simple rotation

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jwebster429
Forum Newbie
Posts: 2
Joined: Fri Jul 09, 2010 1:24 pm

perform simple rotation

Post by jwebster429 »

Hi,
I'm trying to create a simple rotation animation using php.
Here's the code

Code: Select all

function convertArray($myarray)
  {
  $num_vertices =4;
  $newvalues = array();
  $convertedvalues = array();
  $counter = 0;
  for ($j=0;$j < $num_vertices;$j++)
    {
    $newvalues[]= $myarray[$counter]+ 200;
     $counter++;
     $newvalues[]= (-1*$myarray[$counter])+ 200 ;
     $counter++;
   
    }
    return $newvalues;
  }
function angleConvertArray($myarray,$angle)
  {
    $num_vertices =4;
    $newvalues = array();
    $counter = 0;
     for ($j=0;$j < $num_vertices;$j++)
      {
        $x=$myarray[$counter];
        $counter++;
        $y=$myarray[$counter];
        $counter++;
        $newx=$x*cos($angle)-$y*sin($angle);
        $newy=$x*sin($angle)+$y*cos($angle);
        $newvalues[]=$newx;
        $newvalues[]=$newy;
      }
     return $newvalues;
  
  }
 //*****************************************************
 
$values = array(  10, 0, 0, 100, -10, 0, 10, 0);
 for ($j=0;$j < 10;$j++)
    {
 
   
      $angle = $j * 0.2 ;

      $angledvalues = angleConvertArray($values,$angle);
      $convertedvalues = convertArray($angledvalues); 
      $image = imagecreatetruecolor(400, 400);
      $bg  = imagecolorallocate($image, 200, 200, 200);
      $blue = imagecolorallocate($image, 0, 0, 255);
      imagefilledpolygon($image, $convertedvalues, 4, $blue);
      header('Content-type: image/png');
      imagepng($image);
      imagedestroy($image);
    }


Essentially this creates a box ($image = imagecreatetruecolor(400,400);) and is supposed to rotate a primitive clock hand through ten iterations (using the iteration variable $j and multiplying it by 0.2).

Though I can get given rotation points to work (say by nixing the for loop and inserting some arbitrary $angle) it does not actually perform an animation. I don't really know if this is the right way to go about it.

I don't have much experience with using php so I don't really know what the best approach would be.

And of course there are more efficient ways of handling certain elements esp. the use of two arrays to perform the transformation from modelspace to screenspace, I know that, I just didn't want to get into matrix manipulations until I got the underlying system down ( please, no lectures on graphics math!)

Thanks in advance for any information on how to perform this animation.

Jeff
websitesca
Forum Newbie
Posts: 16
Joined: Fri Jul 09, 2010 2:47 pm

Re: perform simple rotation

Post by websitesca »

Hey! I think you might want to use Flash for this type of thing. The only other option would be to render an animated GIF - you can do that too. You'd have to render each of the frames of your animation then merge them. Here is some example code on that:

http://www.phpclasses.org/package/3163- ... mages.html

Hope that helps!

Georges,
http://www.websites.ca
jwebster429
Forum Newbie
Posts: 2
Joined: Fri Jul 09, 2010 1:24 pm

Re: perform simple rotation

Post by jwebster429 »

Hi,
I checked out that link. For some reason it wouldn't let me register so I couldn't download the relevant class, assuming that is what you were recommending.

Ultimately I'm looking for something interactive, for example to be able to implement a simple 2d game. I'm not sure if this approach would work in such an application. Looking around online it seems like everyone is using Flash for 2d games. I've always been "antiFlash" but I guess I'll have to embrace it. The consensus is that php/javascript produces jumpy, flickery results.

Still, it would be nice to see if I could get some kind of interactive animation out of the php/javascript realm.

Jeff
Post Reply