Everybody! This is important. In a few days, these forums will be moving over to using the totally sweet Discourse platform. To ensure this migration happens smoothly with no loss of content, these forums are currently in a read-only mode. I do apologize for the inconvenience.

There is never a good time to turn the forums off for an extended period of time, but I promise the new forums will be a billion times better. I'm pretty sure of it.

See you all on the other side in a few days, and if you have any (non-technical) questions, please e-mail me at kirupa@kirupa.com. For technical questions, try to find a tutorial that corresponds to what you are looking for and post in the comments section of that page.

Cheers,
Kirupa

Results 1 to 5 of 5

Thread: Shooting bullets from a moving player

  1. #1

    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?

  2. #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.

  3. #3
    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.

  4. #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?

  5. #5
    I solved the problem just writing
    Code:
    //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
    ...
    in the moveBullets() function.
    Last edited by SimoneS93; July 7th, 2012 at 09:09 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012