Results 1 to 5 of 5
July 5th, 2012, 01:25 PM #14Registered User
Shooting bullets from a moving player
I know probably there're already solutions to this problem, but I cant't find them. The problem is that I have a player which can be moved by arrow keys and rotate towards the mouse coordinates, and has a vx and a vy properties. When I fire a bullet, it goes towards the mouse too and also has a vx and a vy properties, but if the player is moving towards the mouse, it overcomes the bullets or it seems the bullets are moving slower. I tried to add to the bullets' vx and vy the ship's vx and vy, but it doesn't work (some bullets move faster than the others). How can I solve the problem?
July 5th, 2012, 02:58 PM #2
If I'm understanding you correctly, this isn't a problem at all. When a player is firing many bullets, and starts moving in the same direction that he is firing, then two things will happen. First, from the target's perspective, the bullets seem to be coming more frequently (at the same speed). Second, from the player's perspective, the same bullets are actually firing at the usual frequency, but they appear to be moving slower. This is called the doppler effect, and it is the natural behavior of things. If this is what you're describing, then I do not see a reason to change it. If you did, the game would begin to feel quite strange.
On the other hand, if you mean that the bullet velocities are always made relative to the player right now, then they need to be made relative to the stage/canvas/whatever you are using to achieve the proper effect.
July 6th, 2012, 03:34 AM #34Registered User
For the moment, I just increase the bullets' speed and it looks more natural, but if the bullets' speed it's too low, I think the problem exists, considering that the player is faster than the bullets, so it overtakes them. I play several games where the bullets seem to move always at the same speed, no matter the player's speed.
July 6th, 2012, 02:25 PM #4
That should always be the case - the player's velocity (relative to the world) should be completely independent of the bullet velocities (also relative to the world). How are you set up right now? Is the camera always following the player / is the ground moving instead of the player? Or is the player moving and the view of the level not? Is this a top-down or a side-scroller? Finally, what DisplayObject is the player a child of? What about the bullets?
July 7th, 2012, 04:24 AM #54Registered User
I solved the problem just writing
//add player's vx only if it's moving in the same x-direction if (ship.vx * bullet.vx > 0) bullet.x += bullet.vx + ship.vx; else bullet.x += bullet.vx; //the same for vy ...
Last edited by SimoneS93; July 7th, 2012 at 09:09 AM.