Welcome! Log In Create A New Profile

Advanced

Need Help with Player Movement

Posted by RazorChrist 
Need Help with Player Movement
March 29, 2009 04:11PM
Okay guys, after a few weeks of coding and failing, I need some help. I'm trying to do a simple port of a 2d tron game, and for some reason, the player direction isn't working right and the cycle trails aren't moving correctly. I hate asking for help, cus this is such a simple game to code, but something's wrong and I haven't been able to figure it out yet. Here's the part of the code that I believe to be the problem:

troncycles.hpp
/***********************************
* 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();

troncycles.cpp
// 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.directionWEST) {
		p1.direction = NORTH;
		p1.r = p1.r+90;
	}
}
		
/*****************************
* player 2 input
******************************/
if (WPADDOWN2 & WPAD_BUTTON_UP) {
	// pivot 90 degrees to the left
	if (--p2.directionWEST) {
		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;
}
For 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?
Re: Need Help with Player Movement
March 29, 2009 07:33PM
Hello RC,

A couple of comments to make:

1) Did you make sure to scan your WPads in each loop to test for controller input? If your bikes aren't turning in response to input, it could be that you're not actually retrieving your input correctly!
2) Your code within your controller handling conditions seems to be testing for a decremented variable directionWEST which doesn't appear to have been defined in your Struct. What is that additional nested if-statement trying to do?

Also, a suggestion - why not use a common player class or struct, rather than defining everything twice?

- Myu0
Re: Need Help with Player Movement
March 29, 2009 08:28PM
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

Also, not a bug but for efficiency you all the struts are actually the same structure so you should define this structure then set each of the four variables to the defined structure type. I would also think about making a function from the movement changes as this is the same code for both 1 and 2 players, just pass in the variables for whichever player you want to do the movement against.



Edited 1 time(s). Last edit at 03/29/2009 08:28PM by JustWoody.
Re: Need Help with Player Movement
March 29, 2009 11:47PM
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
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.
Re: Need Help with Player Movement
March 30, 2009 06:14AM
Yeah that fixed it up, however, I'm still having a small problem with the direction code. I changed up the code a bit, and everything rotates the way it's supposed to now, except if (for example) I hit LEFT more than 4 times in a row...it stops moving and rotating, until I hit RIGHT a few times, and vise versa. Here's the new code:
/*****************************
* 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
I see 2 problems:

1) your new code does not check, whether the direction is smaller than 0 or higher than 3.

2) p1.direction = p1.direction--; is a rather interresting line. does the postdecreament work before or after the varable is set?
Anyway, you should replace it with either:
p1.direction = p1.direction - 1;
or p1.direction--;
or --p1.direction;


And why do you keep track of the direction AND the rotation (I assume r = rotation)?
Do you have any usage for both values?



Edited 2 time(s). Last edit at 03/30/2009 09:39AM by daniel_c_w.
Re: Need Help with Player Movement
March 30, 2009 12:39PM
Quote
daniel_c_w
... 1) your new code does not check, whether the direction is smaller than 0 or higher than 3.
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;
}



Edited 1 time(s). Last edit at 03/30/2009 12:41PM by Myu0.
Re: Need Help with Player Movement
March 30, 2009 09:09PM
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?
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
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;
}
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?



Edited 1 time(s). Last edit at 03/30/2009 09:20PM by RazorChrist.
Re: Need Help with Player Movement
March 31, 2009 12:37AM
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.
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 01:18AM
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?
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.
Re: Need Help with Player Movement
March 31, 2009 04:07AM
Quote
Myu0
He could just % 4 his result, which would also allow infinite spin.

that is propably the best approach,
but what is (int)((0 - 1) % 4)?
(I did not test it on powerpc-gekko-gcc, but in visual studio c++ it is -1, since int is signed)

so we have (-1) when we want to have (3), which is the error-ridden west-case

he should do:
direction = (direction + 3) % 4
and r = direction * 90



Edited 1 time(s). Last edit at 03/31/2009 04:12AM by daniel_c_w.
Re: Need Help with Player Movement
April 01, 2009 12:09PM
Okay, I took the above advice and it's 50% working. Turning RIGHT (using DOWN button on Wii) rotates correctly, and allows you to continue making right-hand turns with no problems. However, turning LEFT (using UP button on Wii) is still bugged for some reason. Here's the new code:
/*****************************
* 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
Quote
RazorChrist
Is this because of the -1?
It would seem so. I thought -1 % 4 was suppsed to resolve to 3, but the powerPC mod function doesn't quite work that way.

So, the solution is to change (-1) to (+3). That'll fix it.



Edited 1 time(s). Last edit at 04/01/2009 03:05PM by Myu0.
Re: Need Help with Player Movement
April 01, 2009 03:15PM
Quote
RazorChrist
p1.direction = (p1.direction-1) % 4;

change that to:
p1.direction = (p1.direction + 3) % 4;


Quote
Myu0
I thought -1 % 4 was suppsed to resolve to 3, but the powerPC mod function doesn't quite work that way.

It would have worked, if he used a signed int.



Edited 1 time(s). Last edit at 04/01/2009 03:18PM by daniel_c_w.
Re: Need Help with Player Movement
April 02, 2009 09:04AM
Quote
daniel_c_w
change that to:
p1.direction = (p1.direction + 3) % 4;

That fixed it. Thanks guys. I'm sure I'll need more help down the road. Just wish #wiidev was more helpful. Oh well.



Edited 1 time(s). Last edit at 04/02/2009 09:05AM by RazorChrist.
Sorry, only registered users may post in this forum.

Click here to login