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.
IndexProgramming Questions & HelpPrograms › Help with rotation of a triangle
Page Index Toggle Pages: 1
Help with rotation of a triangle (Read 1278 times)
Help with rotation of a triangle
Feb 14th, 2010, 12:33pm
 
Hello,

I am looking for someone to explain to me how I go about  rotating this...

beginShape();
   vertex(388,306);
   vertex(396,325);
   vertex(390,325);
   vertex(388,321);
   vertex(386,325);
   vertex(380,325);
   vertex(388,306);
   endShape();

the shape is going to be my rocket  i have got it firing up and down with gravity working against it... i have been pulling my hair out trying to figure out how to turn the ship left and right !!

I hope someone can help.
Re: Help with rotation of a triangle
Reply #1 - Feb 14th, 2010, 1:29pm
 
Code:
float rot = 0.0;

void setup(){
 size(400, 400);
 smooth();
 noStroke();
 frameRate(4);
}

void draw(){

 background(0);

 //push matrix helps you to manipulate such things like rotation and translation
 //ACCORDING to the shape, and not the whole sketch
 pushMatrix();

 //position of your thing
 translate(width/2, height/2);

 //in a cercle, you have TWO_PI
 //to rotate in 4 steps, you need to get TWO_PI/4 (HALF_PI)
 rot += HALF_PI;
 rotate(rot);

 //the center of your shape needs to be at the coordinate (0,0)
 //so like you see below, there is actually negative numbers
 beginShape();
 vertex(0,-10);
 vertex(8,10);
 vertex(2,10);
 vertex(0,5);
 vertex(-2,10);
 vertex(-8,10);
 vertex(0,-10);
 endShape();

 popMatrix();
}

Re: Help with rotation of a triangle
Reply #2 - Feb 14th, 2010, 4:45pm
 
I will see if I can incorporate it into my code and get back to you! I appreciate your help! I am very new to processing! Its the first time I have ever seen the push and pop matrix thingy!! I will look them up to see exactly what they do! Thanks again!
Re: Help with rotation of a triangle
Reply #3 - Feb 15th, 2010, 12:56am
 
Another option is to apply a little maths and do the rotation manually.  You may want to do this if knowing the locations of the points of the triangle in the sketch window is important (e.g. you need to use them in collision detection).  TBH I could be making life difficult for myself with this approach (and for all I know it may be less efficient), but I like knowing that all the point coordinates for my objects are in relation to the sketch window.

Anyway - less of the justification; here's some code:

Code:
// method for calculating rotated point coordinates
PVector rotatePoint(float x, float y, float rotation) {
   PVector rotatedPoint = new PVector();
   float angle  = radians(rotation);
   rotatedPoint.x = cos(angle) * x - sin(angle) * y;
   rotatedPoint.y = cos(angle) * y + sin(angle) * x;
   return rotatedPoint;
 }

// snipped example of use:
tip = space.rotatePoint(10,0,rotation);
leftCorner = space.rotatePoint(-10,-5,rotation);
rightCorner = space.rotatePoint(-10,5,rotation);

beginShape();
 vertex(x + tip.x,y + tip.y);
 vertex(x + leftCorner.x, y + leftCorner.y);
 vertex(x + rightCorner.x, y + rightCorner.y);
 vertex(x + tip.x,y + tip.y);
endShape();


This is taken from my asteroids game.  The x and y coordinates in the vertex calls represent the 'centre' of the ship, from which the points are offset.  (Note that these, as well as 'tip', 'leftCorner', 'rightCorner', etc. are class properties, but could be locally declared variables.)  The nice thing here is I can fire a bullet from the location of the 'tip' without having to do any extra work to take account of transformations that could have been made with push/popMatrix; though as I said, that might not be so complex after all...  I'm just wary of relying on helper methods that I might not be able to use in other languages.
Page Index Toggle Pages: 1