Welcome! Log In Create A New Profile

Advanced

Calibrating offset for wiimote cursor

Posted by pcmantinker 
Calibrating offset for wiimote cursor
January 21, 2009 09:18PM
Hi,

I figured out how to display a cursor on the screen at the wiimote's x/y position, but the offset of the cursor position needs to be corrected. Currently I have found the x offset is 82 when x is 0 and the y offset is 82 when y is 0. When x is 640, the x offset is 22. Likewise, when y is 480, the y offset is 48. I have checks to see if the x and y values are within a certain range to apply the correct offsets to the cursor, but the cursor movement isn't smooth when it nears the edge of the right side of the screen or bottom of the screen. It looks at the offset value and applies it but makes the cursor jump. I was wondering if someone knows of how I can smooth the movement or has a better method. Any help is appreciated. Thanks.

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 


#include "GRRLIB/GRRLIB.h"

#include "gfx/logo.h"
#include "gfx/player1_point.h"
#include "gfx/player1_grab.h"
#include "gfx/player1_openhand.h"

#include "GRRLIB/fonts/GRRLIB_font1.h"

#define screenwidth (640)
#define screenheight (480)

void displayCursor();
void WiiResetPressed();
void WiiPowerPressed();
void WiimotePowerPressed(s32 chan);

Mtx GXmodelView2D;
GXRModeObj *rmode = NULL;

bool displaypointer = true;
bool displaygrab = false;
bool displayopenhand = false;

int fullOFFSET = 104;
	
ir_t ir1;

u8 HWButton = 0;


int main(){

	u8 *tex_logo=GRRLIB_LoadTexture(logo);
	u8 *p1pointer=GRRLIB_LoadTexture(player1_point);
	u8 *p1grab=GRRLIB_LoadTexture(player1_grab);
	u8 *p1openhand=GRRLIB_LoadTexture(player1_openhand);
	u8 *tex_font1=GRRLIB_LoadTexture(GRRLIB_font1);
	
   	float rot=0;
	float alpha=255;
	int x=0;
	int y=0;
	int X_OFFSET = 0;
	int Y_OFFSET = 0;
	
	// Init video layer
    VIDEO_Init();
	
	// Init wiimote layer
        WPAD_Init();
        WPAD_SetIdleTimeout(120); // Wiimote is shutdown after 2 minutes of innactivity.
	WPAD_SetDataFormat(0, WPAD_FMT_BTNS_ACC_IR);	// enable accelerometers and IR
	WPAD_SetVRes(0, 640, 480);
	
	// Obtain the preferred video mode from the system
	// This will correspond to the settings in the Wii menu
	rmode = VIDEO_GetPreferredMode(NULL);
	
	
	// Set up the video registers with the chosen mode
	VIDEO_Configure(rmode);
		
	//Make the display visible
	VIDEO_SetBlack(FALSE);

	// Flush the video register changes to the hardware
	VIDEO_Flush();
		
	WPAD_Init();
		

    GRRLIB_InitVideo();
    GRRLIB_Start();
	
	
	
    while(1){
       			
	WPAD_ScanPads();
		
	WPAD_IR(0, &ir1); 
	x=ir1.x;
	y=ir1.y;
		
        u32 wpaddown = WPAD_ButtonsDown(0);
        u32 wpadheld = WPAD_ButtonsHeld(0);
	u32 wpadup = WPAD_ButtonsUp(0);
		
	SYS_SetResetCallback(WiiResetPressed);
	SYS_SetPowerCallback(WiiPowerPressed);

		
        GRRLIB_FillScreen(0xFF000000);

        GRRLIB_DrawImg(144, 176, 352, 128, tex_logo, rot, 1, 1, alpha );
		
		if(x==0)
		{
			X_OFFSET = -82;
		}
		if(x<=screenwidth && x>=screenwidth - 5)
		{
			X_OFFSET = -22;
		}
		
		if(y==0)
		{
			Y_OFFSET = -82;
		}
		if(y<=screenheight && y>=screenheight - 5)
		{
			Y_OFFSET = -48;
		}
		
		GRRLIB_Printf(50,30,tex_font1,0xFFFFFFFF,1,"x oset: i% y oset: %1", X_OFFSET, Y_OFFSET);
		
		GRRLIB_Printf(50,50,tex_font1,0xFFFFFFFF,1,"wii x : %i wii y: %i", x, y);
		GRRLIB_Printf(50,70,tex_font1,0xFFFFFFFF,1,"scrn x : %i scrn y: %i", x + X_OFFSET, y + Y_OFFSET);
		
		// check icon display states
		if (displaypointer)
		{
			GRRLIB_DrawImg(x + X_OFFSET, y + Y_OFFSET, 96, 96, p1pointer, ir1.angle, 1, 1, 255 );
		}
		
		if (displaygrab)
		{
			GRRLIB_DrawImg(x + X_OFFSET, y + Y_OFFSET, 96, 96, p1grab, ir1.angle, 1, 1, 255 );
		}
		
		if (displayopenhand)
		{
			GRRLIB_DrawImg(x+ X_OFFSET, y + Y_OFFSET, 96, 96, p1openhand, ir1.angle, 1, 1, 255 );
		}
		
		GRRLIB_Render();
		
		if (wpadup & WPAD_BUTTON_A)
		{
		displaypointer = true;
		displayopenhand = false;
		displaygrab = false;
		}
		
		if (wpadup & WPAD_BUTTON_B)
		{
		displaypointer = true;
		displayopenhand = false;
		displaygrab = false;
		}
		
		if (wpaddown & WPAD_BUTTON_A)
		{
		displaypointer = false;
		displayopenhand = false;
		displaygrab = true;
		}
		
		if (wpaddown & WPAD_BUTTON_B) 
		{
		displaypointer = false;
		displayopenhand = true;
		displaygrab = false;
		}
		
        if (wpaddown & WPAD_BUTTON_HOME) exit(0);
        if (wpaddown & WPAD_BUTTON_RIGHT) X_OFFSET+=2;//rot+=2;
        if (wpaddown & WPAD_BUTTON_LEFT) X_OFFSET-=2;//rot-=2;
	if (wpaddown & WPAD_BUTTON_UP) Y_OFFSET-=2;
	if (wpaddown & WPAD_BUTTON_DOWN) Y_OFFSET+=2;
        if (wpadheld & WPAD_BUTTON_MINUS) if((alpha-=3)<0) alpha=0;
        if (wpadheld & WPAD_BUTTON_PLUS) if((alpha+=3)>255) alpha=255;
	
	if(HWButton)
			break;

    }
	
	if(HWButton)
	{
		SYS_ResetSystem(HWButton, 0, 0);
	}

	
    return 0;
}

/**
 * Callback for the reset button on the Wii.
 */
void WiiResetPressed()
{
	HWButton = SYS_RETURNTOMENU;
}
 
/**
 * Callback for the power button on the Wii.
 */
void WiiPowerPressed()
{
	HWButton = SYS_POWEROFF;
}
 
/**
 * Callback for the power button on the Wiimote.
 * @param[in] chan The Wiimote that pressed the button
 */
void WiimotePowerPressed(s32 chan)
{
	HWButton = SYS_POWEROFF;
}


Re: Calibrating offset for wiimote cursor
January 21, 2009 09:58PM
Something like this?
newX = x - 82 * (1 - x / 640) - 22 * (x/640);
newY = y - 82 * (1 - y / 480) - 42 * (y / 480);

This is a somewhat general calibration-calculation.



Edited 2 time(s). Last edit at 01/21/2009 10:00PM by Dykam.
Re: Calibrating offset for wiimote cursor
January 22, 2009 12:50AM
Thanks for your quick reply. I have it working with all four wiimotes simultaneously which is pretty cool.
Sorry, only registered users may post in this forum.

Click here to login