Need Help with Player Movement March 29, 2009 04:11PM | Registered: 15 years ago Posts: 83 |
/*********************************** * Function to define both players * directional movements & colors ************************************/ #define NORTH 0 #define EAST 1 #define SOUTH 2 #define WEST 3 #define RED 0xFFFF0000 #define BLUE 0xFF0000FF /*********************************** * Defines all variables for player1 * and player2 in structure form ************************************/ struct p1 { int x; int y; int r; int height; int width; int direction; int life; } p1 = { 45, 240, 0, 32, 16, EAST, 5 }; struct p2 { int x; int y; int r; int height; int width; int direction; int life; } p2 = { 550, 240, 0, 32, 16, WEST, 5 }; struct p1trail { int x; int y; int height; int width; int direction; int color; } p1trail = { 40, 240, 4, 4, EAST, RED }; struct p2trail { int x; int y; int height; int width; int direction; int color; } p2trail = { 555, 240, 4, 4, WEST, BLUE }; // @brief Fades the menu in and does button animation at start void fade_in(); // @brief Updates the buttons for the menu void menu_update(); // @brief Primary menu loop void menu_loop(); // @brief Primary game loop void grid_loop();
// draw the players GRRLIB_DrawImg(p1.x,p1.y,p1.height,p1.width,tex_red,p1.r,1,1,255); GRRLIB_Rectangle(p1.x-5,p1.y+5,p1trail.width,p1trail.height,p1trail.color,1); GRRLIB_DrawImg(p2.x,p2.y,p2.height,p2.width,tex_blue,p2.r,1,1,255); GRRLIB_Rectangle(p2.x+33,p2.y+6,p2trail.width,p2trail.height,p2trail.color,1); GRRLIB_Render(); checkcollision=false; /***************************** * player 1 input ******************************/ if (WPADDOWN1 & WPAD_BUTTON_UP) { // pivot 90 degrees to the left if (--p1.directionFor some odd reason, when I put the code in the post, it doesn't show the player control for turning right for either player. Anyway, not sure exactly what's up with my code, but it just doesn't rotate correctly. Is there an easier way to define the Left/Right movements?WEST) { p1.direction = NORTH; p1.r = p1.r+90; } } /***************************** * player 2 input ******************************/ if (WPADDOWN2 & WPAD_BUTTON_UP) { // pivot 90 degrees to the left if (--p2.direction WEST) { p2.direction = NORTH; p2.r = p2.r+90; } } /***************************** * defines player1 movement in * the direction it's pointing ******************************/ switch(p1.direction) { case NORTH: { if (--p1.y<0) p1.y = 480; p1trail.y--; } break; case SOUTH: { if (++p1.y>480) p1.y = 0; p1trail.y++; } break; case EAST: { if (++p1.x>640) p1.x = 0; p1trail.x++; } break; case WEST: { if (--p1.x<0) p1.y = 640; p1trail.x--; } break; default:break; } /***************************** * defines player2 movement in * the direction it's pointing ******************************/ switch(p2.direction) { case NORTH: { if (--p2.y<0) p1.y = 480; p2trail.y++; } break; case SOUTH: { if (++p2.y>480) p1.y = 0; p2trail.y++; } break; case EAST: { if (++p2.x>640) p1.x = 0; p2trail.x++; } break; case WEST: { if (--p2.x<0) p1.y = 640; p2trail.x++; } break; default:break; }
Re: Need Help with Player Movement March 29, 2009 07:33PM | Registered: 15 years ago Posts: 25 |
Re: Need Help with Player Movement March 29, 2009 08:28PM | Registered: 16 years ago Posts: 51 |
Re: Need Help with Player Movement March 29, 2009 11:47PM | Registered: 15 years ago Posts: 83 |
OMG Fail....I totally missed that, you're completely right. Damned copy/paste typos. Let me fix those and recompile/test and see if that fixes it.Quote
JustWoody
Well there is definitely a bug in the WEST cases as I think it should read p1.x instead of p1.y in the assignment statement after the IF
Re: Need Help with Player Movement March 30, 2009 06:14AM | Registered: 15 years ago Posts: 83 |
/***************************** * player 1 input ******************************/ if (WPADDOWN1 & WPAD_BUTTON_UP) { // pivot 90 degrees to the left p1.direction = p1.direction--; p1.r = p1.r-90; } else if (WPADDOWN1 & WPAD_BUTTON_DOWN) { // pivot 90 degrees to the right p1.direction = p1.direction++; p1.r = p1.r+90; }Sorry, never had this many problems with something simple before. I also tried it with p1.direction = p1.direction+1 and it had the same effect.
Re: Need Help with Player Movement March 30, 2009 09:34AM | Registered: 16 years ago Posts: 1,012 |
Re: Need Help with Player Movement March 30, 2009 12:39PM | Registered: 15 years ago Posts: 25 |
He could just % 4 his result, which would also allow infinite spin. That is to say,Quote
daniel_c_w
... 1) your new code does not check, whether the direction is smaller than 0 or higher than 3.
/***************************** * player 1 input ******************************/ if (WPADDOWN1 & WPAD_BUTTON_UP) { // pivot 90 degrees to the left p1.direction = (p1.direction-1) % 4; p1.r = (p1.r-90) % 360; } else if (WPADDOWN1 & WPAD_BUTTON_DOWN) { // pivot 90 degrees to the right p1.direction = (p1.direction+ 1) % 4; p1.r = (p1.r+90) % 360; }
Re: Need Help with Player Movement March 30, 2009 09:09PM | Registered: 15 years ago Posts: 83 |
The rotation value solely handles rotating the PNG image of the bike, nothing more. It was just a simple way of doing it, since the rotation value is preset when GRRLIB loads the actual PNG.Quote
daniel_c_w
And why do you keep track of the direction AND the rotation (I assume r = rotation)?
Do you have any usage for both values?
This caused it stop turning after 4 turns again. The problem is not the turning itself, the problem is when it turns, it's supposed to continue moving in the direction it turned. In this case, it doesn't, it just stops moving. Would it be less of a pain in the ass if I used all four D-Pad buttons for movement, instead of just using LEFT/RIGHT?Quote
Myu0
He could just % 4 his result, which would also allow infinite spin. That is to say,/***************************** * player 1 input ******************************/ if (WPADDOWN1 & WPAD_BUTTON_UP) { // pivot 90 degrees to the left p1.direction = (p1.direction-1) % 4; p1.r = (p1.r-90) % 360; } else if (WPADDOWN1 & WPAD_BUTTON_DOWN) { // pivot 90 degrees to the right p1.direction = (p1.direction+ 1) % 4; p1.r = (p1.r+90) % 360; }
Re: Need Help with Player Movement March 31, 2009 12:37AM | Registered: 15 years ago Posts: 25 |
That would suggest that none of the Compass point conditions are triggering, implying that p1.direction is something other than 0,1,2 or 3 at that point, which would be very weird if you're mod 4ing it (and int is by default considered signed, so negatives should work fine). Do you know what values the direction variable is taking when it stops responding?Quote
RazorChrist
This caused it stop turning after 4 turns again. The problem is not the turning itself, the problem is when it turns, it's supposed to continue moving in the direction it turned. In this case, it doesn't, it just stops moving.
Re: Need Help with Player Movement March 31, 2009 01:18AM | Registered: 15 years ago Posts: 83 |
It stops responding 90% of the time when I'm moving WEST. I say most of the time, because it seems to work somewhat when I push the buttons slowly, but if I button mash one direction or another, after about 3-4 button presses is when it stops turning. Maybe that's me and not actually the problem, just thought I'd mention it. But anyway, most often I'm pointed WEST when it happens.Quote
Myu0
That would suggest that none of the Compass point conditions are triggering, implying that p1.direction is something other than 0,1,2 or 3 at that point, which would be very weird if you're mod 4ing it (and int is by default considered signed, so negatives should work fine). Do you know what values the direction variable is taking when it stops responding?
Re: Need Help with Player Movement March 31, 2009 04:07AM | Registered: 16 years ago Posts: 1,012 |
Quote
Myu0
He could just % 4 his result, which would also allow infinite spin.
Re: Need Help with Player Movement April 01, 2009 12:09PM | Registered: 15 years ago Posts: 83 |
/***************************** * player 1 input ******************************/ if (WPADDOWN1 & WPAD_BUTTON_UP) { // pivot 90 degrees to the left p1.direction = (p1.direction-1) % 4; p1.r = p1.direction * 90; } else if (WPADDOWN1 & WPAD_BUTTON_DOWN) { // pivot 90 degrees to the right p1.direction = (p1.direction+1) % 4; p1.r = p1.direction * 90; }I tried it with %3 at first, but that didn't work right for either direction. I think it has to be %4 due to having four directional variables (0-3). But like I said, turning RIGHT works fine, it's the turning LEFT that doesn't work. Is this because of the -1?
Re: Need Help with Player Movement April 01, 2009 03:04PM | Registered: 15 years ago Posts: 25 |
Re: Need Help with Player Movement April 01, 2009 03:15PM | Registered: 16 years ago Posts: 1,012 |
Quote
RazorChrist
p1.direction = (p1.direction-1) % 4;
Quote
Myu0
I thought -1 % 4 was suppsed to resolve to 3, but the powerPC mod function doesn't quite work that way.
Re: Need Help with Player Movement April 02, 2009 09:04AM | Registered: 15 years ago Posts: 83 |