Welcome! Log In Create A New Profile

Advanced

sprite location bug

Posted by g_man 
sprite location bug
March 31, 2010 07:53AM
In my code I create an arrow who's size and direction are relative to the location of the wiimote. The code for writing the size and the direction works perfectly, whithout error, but I'm having trouble placing the arrow in the correct location. I works fine when my code for the location is simply
arrow.SetPosition(initX,initY);
where initX and initY is the location I want the center of the back of the arrow. When I run this, the size and direction work great, but the position is off. When the code is this
arrow.SetPosition(initX-arrowimg.GetWidth()/2,initY-arrowimg.GetHeight());
It doesn't appear. here is my entire source that applies, the rest has to do with unrelated events.
int main(int argc, char **argv)
{
	/****************
	* VARIABLES :)	*
	****************/
	
	Image arrowimg;
	Sprite arrow;
	
	
	bool bulletAlive = false;
	bool levelDone = false;
	bullet shootingBullet;
	int i;
	
	int initX = 50;
	int initY = 50;
	
	int startX;
	int startY;

	float spd=3;
	float ang=85*(M_PI/180);

	LayerManager manager(16);

	// Create the game window and initalise the VIDEO subsystem
	GameWindow *gwd = new GameWindow;
		gwd->InitVideo();
	
	gwd->SetBackground((GXColor){ 0, 0, 0, 255 });

	/********************
	*	Set up images	*
	*	to be placed	*
	*	on screen		*
	********************/
	manager.Append(&ptrspr);
	manager.Append(&shootingBullet.bulletQuad);
	manager.Append(&planet1);
	manager.Append(&target);
	
	
	for(;;)
	{
	
		do {
		
		WPAD_ScanPads();
		PAD_ScanPads();
		
		WPAD_IR(0,&ir);
		
		arrowimg.LoadImage(arrow_png);
		arrow.SetImage(&arrowimg);
	
		arrow.SetRefPixelPosition(arrowimg.GetWidth()/2,arrowimg.GetHeight());
		
		ptrspr.SetPosition(ir.x-8,ir.y);
		
		int buttonsDownCube = PAD_ButtonsDown(0);
		int buttonsDown = WPAD_ButtonsDown(0);
		int buttonsHeld = WPAD_ButtonsHeld(0);
		int buttonsUp = WPAD_ButtonsUp(0);
		
		if(buttonsDown&WPAD_BUTTON_HOME||buttonsDownCube & PAD_BUTTON_START)
			exit(0);//homemenu(gwd);
		
		/********************
		*	Create Arrow	*
		*	and send off	*
		*	bullet			*
		*********************/
		if(buttonsDown&WPAD_BUTTON_A){
		if(bulletAlive==false){
			startX = ir.x;
			startY = ir.y;
			//arrow.SetPosition(initX-arrowimg.GetWidth()/2,initY-arrowimg.GetHeight());
			arrow.SetPosition(initX,initY+arrowimg.GetHeight());
			Xx = initX;
			Yy = initY;
			manager.Append(&arrow);
		}
		}

		if(buttonsHeld&WPAD_BUTTON_A){
		if(bulletAlive==false){
			arrow.SetStretchWidth(calcDistance(startX,startY,ir.x,ir.y)/150);
			arrow.SetStretchHeight(arrow.GetStretchWidth());
			if(startX!=ir.x){
				ang = atan(
				(ir.y-startY)/
				(startX-ir.x));
				
				
				if((startY-ir.y>0&&ir.x-startX>0)||(startY-ir.y<0&&ir.x-startX<0)){
					//flip on TL 45
					ang = (.75*M_PI) + (.75*M_PI-ang);
				}	
				
				if(startY-ir.y<0&&ir.x-startX>0){
					//flip on TR 45
					ang = (1.25*M_PI) + ((1.25*M_PI)-ang);
				}

				if(startY-ir.y>0&&ir.x-startX<0){
					//flip on T 90
					ang = (.5*M_PI) + ((.5*M_PI)-ang);
					//add .5 PI
					ang += .5*M_PI;
				}

				if(startY-ir.y>0&&ir.x-startX>0){
					ang += M_PI;
				}

				ang += M_PI;
				
			} else if(ir.y > startY){
				ang = 1.5*M_PI;
			} else {
				ang = .5* M_PI;
			}
			arrow.SetRotation((ang*(180/M_PI))/2);
		}	
		}

		if(buttonsUp&WPAD_BUTTON_A){
		if(bulletAlive==false){
			spd = calcDistance(startX,startY,ir.x,ir.y)/15;
			if(startX!=ir.x){
				ang = atan(
				(ir.y-startY)/
				(startX-ir.x));
				if(ir.x>startX) ang += M_PI;
			} else if(ir.y > startY){
				ang = 1.5*M_PI;
			} else {
				ang = .5* M_PI;
			}
			shootingBullet.resetBullet(initX,initY,spd,ang,0,0);
			bulletAlive = true;
			manager.Remove(&arrow);
		}
		}
	
		/************************
		*	Math for Planets	*
		*	and also getting	*
		*	rid of dead			*
		*	bullets				*
		************************/
		
		manager.Draw(0,0);
		
		gwd->Flush();
	} while (levelDone == true);	
	}
	return 0;
}
If you want my entire source then just ask, and if so should I post it between code blocks, or should I find a file hosting site. If so, what is a good one.
Re: sprite location bug
April 01, 2010 04:57AM
Ok, I have some more information. The cause of me not seeing the arrow is because I subtract my arrow height which is returned to me by arrowimg.GetHeight(). Normally this would be fine, but it is returning a height of 4294967296. When my real image height is 112, and my width is 44. please reply, thanks
Re: sprite location bug
April 01, 2010 06:27AM
An unsigned integer has the range of 0 to 4294967295. You are getting 4294967296, which is really close. The problem is, if it were stored as, say, -1, it would show up as: 4294967294.

I'm not entirely sure what's going on. Can you load a different image? If so, then something's wrong with the first image, and we can work from there.
Re: sprite location bug
April 01, 2010 04:12PM
I'll post more when I can test it again, but other images load fine on the screen, and the arrow foes too, just no where I want it. I have run trials with the arrow in the mddle of the screen and it works perfectly, which leaves arrowimg.GetHeight() to be the fault or the position I am placing it
Sorry, only registered users may post in this forum.

Click here to login