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 & HelpSyntax Questions › translate(), rotate(), pushMatrix()... I've looked
Page Index Toggle Pages: 1
translate(), rotate(), pushMatrix()... I've looked (Read 1438 times)
translate(), rotate(), pushMatrix()... I've looked
May 25th, 2009, 6:25pm
 
I really hate asking this since this question has be answered several times. The problem is... well, it's not working for me.

I'm calling this during my draw():
Code:
if(_rotation!=0){ //only use transform on rotating objects;
float offsetX = -this._x-(this._width/2);
float offsetY = -this._y-(this._height/2);
pushMatrix();
rotate(_rotation);
translate(offsetX,offsetY);
image(b,_x,_y,_width,_height);
popMatrix();
}


Okay, so walking through this... we calculate the center point of the image. That's my two floats, returning a float in the middle of the image. pushMatrix(), then rotate on another float, translate, blit, pop.

Now earlier I tried to translate then rotate, but that doesn't seem to work. Also I have to image() before popping, otherwise that doesn't work either.

So what I have is the image is drawn with the center point on the origin (0,0) of the screen rotating in place.

If I try to move the image back with:

image(b,_x+100,_y+100,_width,_height);

or whatever, it'll still rotate around the origin.

It's kinda sad I got collision detection working but I'm struggling on this.

TAB1: Code:
ArrayList Sprites; // build an array of all the sprites, leave this above the class declarion
class bSprite { //Blaine sprite Class
PImage b;
float _x;
float _y;
float _yspeed;
float _xspeed;
float _width;
float _height;
float _rotation;

bSprite (String img, float tempXpos, float tempYpos, float tempXspeed, float tempYspeed){
// EXAMPLE: guy = new bSprite("buster.jpg", 10, 10, 1 ,1);
b = loadImage(img);
_x = tempXpos;
_y = tempYpos;
image(b,_x,_y);
_xspeed = tempXspeed;
_yspeed = tempYspeed;
_width = b.width;
_height = b.height;
_rotation = 0;
Sprites.add(this); // every bSprite is added to the Sprites array

}
void move(){
_x = _x+_xspeed;
_y = _y+_yspeed;
}
void display(){
if(_rotation!=0){ //only use transform on rotating objects;
float offsetX = -this._x-(this._width/2);
float offsetY = -this._y-(this._height/2);
pushMatrix();
rotate(_rotation);
translate(offsetX,offsetY);
image(b,_x,_y,_width,_height);
popMatrix();
}
else{ // unless rotating, draw image;
image(b,_x,_y,_width,_height);
}
}

boolean isHit(bSprite target){

float AX1 = this._x;
float AY1 = this._y;
float BX1 = target._x;
float BY1 = target._y;
float AX2 = this._x+this._width;
float AY2 = this._y+this._height;
float BX2 = target._x+target._width;
float BY2 = target._y+target._height;
//AX1<AX2||AY1<AY2||BX1<BX2||BY1<BY2||
if(BY2<AY1||AY2<BY1||BX2<AX1||AX2<BX1){
//if any of these are true, then no collision is possible
return false;
}
else {
//additional collision detecting can be added in here
return true;
}
}

}









TAB2: Code:
// declare all the sprites

bSprite guy;
bSprite moon;
bSprite dude;
bSprite title;

void setup(){
smooth();
Sprites = new ArrayList();
size(700,500);
title = new bSprite("dodgeMaulTitle.png", 90,90,0,.5);
moon = new bSprite("roll.png", 40,40,.2,.2);
guy = new bSprite("buster.jpg", 300, 300, 0 ,0);
dude = new bSprite("moon.png", 100,100,0,0);


}
void draw(){
guy._rotation+=.02;
background(125);
for (int i = Sprites.size()-1; i>=0; i--){
bSprite blit=(bSprite)Sprites.get(i);
;
blit.display();
}

for (int i = Sprites.size()-1; i>=0; i--){
bSprite blit=(bSprite)Sprites.get(i);
;

if(blit._x>width){
blit._x=0-blit._width;

}
else if (blit._y>height){
blit._y=0-blit._height;
}


else{
blit.move();

}
}
// print(guy.isHit(moon));

}






That's 3 questions in as many days... I'll try and take a few off.

Thanks!!
Re: translate(), rotate(), pushMatrix()... I've looked
Reply #1 - May 26th, 2009, 1:52am
 
IIRC it rotates around the point you translate to, which is why it changes when you put translate first.  Maybe that helps?
Re: translate(), rotate(), pushMatrix()... I've looked
Reply #2 - May 26th, 2009, 4:30am
 
If you want to rotate around the center, you must translate first, rotate, then translate back.
Re: translate(), rotate(), pushMatrix()... I've looked
Reply #3 - May 26th, 2009, 8:02am
 
PhiLho  wrote on May 26th, 2009, 4:30am:
If you want to rotate around the center, you must translate first, rotate, then translate back.


Thanks! I also had my offsets reversed... the translate was counter-intuitive for me. But it's working now.

So for posterity and future searches (since there really isn't a good example of rotating an image out there. They all use width/2 and height/2 for translate).

Here is an example of a working, how to rotate an image.

Code:

float offsetX = -this._x-(this._width/2);
float offsetY = -this._y-(this._height/2);
pushMatrix();
translate(-offsetX,-offsetY);
rotate(_rotation); //rotate however much you want as a float in radians.
translate(offsetX,offsetY);
image(b,_x,_y,_width,_height);
popMatrix();
Re: translate(), rotate(), pushMatrix()... I've looked
Reply #4 - May 26th, 2009, 10:38am
 
A generic solution is to translate to where you want the centre of the image to be, rotate by the angle you want, then translate -image.width,-image.height
Page Index Toggle Pages: 1