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!!