#6877 - hnager - Wed Jun 04, 2003 2:11 am
This one has been haunting me for a day and a half now - I'm working on some sprite tests inorder to teach myself gba development - below is a snipet of a 'tank game' which I'm making...I'm using 16 pre rotated sprites (one thing at a time...sprite rotation will come later). You can see that the right/left buttons affect the tank's current frame (used as an index in an array and set in attribute2).
My problem is with up/down. To start - I've had no luck using a lookup table because the index is cast to a (float) : PI*angle/180 - I went back to using the math functions for now (one thing at a time ;). The other problem I'm having is that the motion just doesn't seem 'right'. It should be like a classic tank game...but it's both too fast (and responsive) and the speed doesnt seem consistent for all the angles.
In short - help! My math is rusty and I could stare at it all night and miss the obvious - if there is one.
// DEFINES //////////////////////
#define TANK_SPEED 2
#define PI (float)3.14159
etc etc
main(){
...
while(1){
if(!(KEYS & KEY_UP)){
angle = (90+360-22.5*tank1_direction);
dx = TANK_SPEED * cos(PI*angle/180);
dy = TANK_SPEED * sin(PI*angle/180);
tank1.x += (int)(dx+.5);
tank1.y -= (int)(dy+.5);
}
if(!(KEYS & KEY_DOWN)){
angle = (90+360-22.5*tank1_direction);
dx = TANK_SPEED * cos(PI*angle/180);
dy = TANK_SPEED * sin(PI*angle/180);
tank1.x += (int)(dx+.5);
tank1.y -= (int)(dy+.5);
//tank1.y += 1;
}
if(!(KEYS & KEY_LEFT)){
if(--tank1_direction < 0) tank1_direction = 15;
}
if(!(KEYS & KEY_RIGHT)){
if(++tank1_direction > 15) tank1_direction = 0;
}
...
} //end while
...
} //end main
My problem is with up/down. To start - I've had no luck using a lookup table because the index is cast to a (float) : PI*angle/180 - I went back to using the math functions for now (one thing at a time ;). The other problem I'm having is that the motion just doesn't seem 'right'. It should be like a classic tank game...but it's both too fast (and responsive) and the speed doesnt seem consistent for all the angles.
In short - help! My math is rusty and I could stare at it all night and miss the obvious - if there is one.
// DEFINES //////////////////////
#define TANK_SPEED 2
#define PI (float)3.14159
etc etc
main(){
...
while(1){
if(!(KEYS & KEY_UP)){
angle = (90+360-22.5*tank1_direction);
dx = TANK_SPEED * cos(PI*angle/180);
dy = TANK_SPEED * sin(PI*angle/180);
tank1.x += (int)(dx+.5);
tank1.y -= (int)(dy+.5);
}
if(!(KEYS & KEY_DOWN)){
angle = (90+360-22.5*tank1_direction);
dx = TANK_SPEED * cos(PI*angle/180);
dy = TANK_SPEED * sin(PI*angle/180);
tank1.x += (int)(dx+.5);
tank1.y -= (int)(dy+.5);
//tank1.y += 1;
}
if(!(KEYS & KEY_LEFT)){
if(--tank1_direction < 0) tank1_direction = 15;
}
if(!(KEYS & KEY_RIGHT)){
if(++tank1_direction > 15) tank1_direction = 0;
}
...
} //end while
...
} //end main