![]() |
|
Projectile angled movement |
armond
Member #8,282
January 2007
|
This is for a sidescrolling shooting game. I admit I'm such an idiot in Trig so if someone could please tell me what's wrong with this formula?
I'm calling the code like this... currentWeapon->fire(RIGHT, 45); The bullets won't appear in the screen anymore... I'm trying to make an angled shot. EDIT: I can't seem to make another post so I'll make the updates here. I seem to get it working now. But why is the angle 45 now shooting at 45 degrees? As I recall from my trigonometry class, it shouldn't be going down! Here's the not so fixed code...
void Ship::fireWeapon() { if (currentWeapon != NULL) { currentWeapon->fire(RIGHT, 90); } }
|
Ceagon Xylas
Member #5,495
February 2005
![]() |
Dammit... Had my entire post typed out and then Firefox decided it wanted to go back for some reason. Okay, pretty much, use Radians instead of fixed. Get rid of the 'direction,' the angle will take care of that all by itself. It would help much if you posted your class or struct. void Vulcan::update() { //make sure angle's in range if(angle<0) angle+=M_PI*2; if(angle>M_PI*2) angle-=M_PI*2; float newX=speed*fcos(angle); //I recommend storing your x and y float newY=speed*fsin(angle); //values as float too. move(newX,newY); animation->animate(); } void Weapon::fire(float angle,float speed); If you're worried about shooting left and right, it's only a matter of specifying the angle correctly. if(player.fire) { if(player.direction==LEFT) currentWeapon->fire(3.14,90); else if(player.direction==RIGHT) currentWeapon->fire(0,90); }
|
armond
Member #8,282
January 2007
|
Thank you for your suggestions! I will incorporate your ideas with my code once I solve the other problem I'm having right now with page flipping. Thanks! |
ImLeftFooted
Member #3,935
October 2003
![]() |
Quote:
//make sure angle's in range if(angle<0) angle+=M_PI*2; if(angle>M_PI*2) angle-=M_PI*2;
//make sure angle's in range while(angle<0) angle+=M_PI*2; while(angle>M_PI*2) angle-=M_PI*2;
|
Ceagon Xylas
Member #5,495
February 2005
![]() |
I have been thinking about changing that all night. |
armond
Member #8,282
January 2007
|
Hi! When I use the if version posted by Dustin Dettmer, my projectiles just go straight. When I use the while version posted by Ceagon Xylas, some of the bullets go straight and some would circle around before going straight. I just need them to be fixed on a certain angle. Let's say projectile one would move in the 45 degree angles, projectile 2 on the zero degree(or 360) angle and the third one on the 315 angle. I reverted back the code but you could see the ones I've tried commented out.
and the projectiles would be initialized like this(...
Are the codes you posted meant to go around in circles? Thanks! |
CursedTyrant
Member #7,080
April 2006
![]() |
Ceagon Xylas said:
if(player.fire) { if(player.direction==LEFT) currentWeapon->fire(3.14,90); else if(player.direction==RIGHT) currentWeapon->fire(0,90); }
3.14 is so inaccurate... --------- |
Ceagon Xylas
Member #5,495
February 2005
![]() |
if(player.fire) { if(player.direction==LEFT) currentWeapon->fire(M_PI,90); else if(player.direction==RIGHT) currentWeapon->fire(0,90); }
Better? Quote: When I use the while version posted by Ceagon Xylas, some of the bullets go straight and some would circle around before going straight. Could you possibly attatch a compiled version that uses the code I posted? (So I can see what you mean) |
armond
Member #8,282
January 2007
|
Ceagon Xylas Sorry but I can't seem to repeat the screnario after having tweaking my code. Instead, please allow me to thank you guys by showing the effect I really wanted to have. I still don't use the real angles that I should be using but I will take a look into this thread from time to time to improve my code. Thank you guys! Please see attachment to see my progress in my game. At the title screen: 1 = Quit Game In Game: There is no ending for the game yet... And the first level is still very much under construction. |
Ceagon Xylas
Member #5,495
February 2005
![]() |
Works great for me. Good luck! |
|