A Flash Developer Resource Site

Results 1 to 10 of 10

Thread: Rotating projectiles

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    5

    Unhappy Rotating projectiles

    Hello,

    I'm having an issue with my game that I can't fix, I'm clueless.


    I'm trying to rotate my bullet (class-bassed MovieClip) so that it's always facing the direction it's traveling to, and I have no idea how to make that happen. The picture here at the bottom should illustrate how I want it to be and how it's right now.


    The game is your average shooter at the moment. You click, the game calculates the rotation from that click and then sends the bullet to that direction. Gravity and friction affects the bullets, simple. Fixing the rotation homever is not simple, for me.

    I would really, really appreciate any help. Thank you!


    Illustration picture; http://i44.tinypic.com/fayk36.png

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    You know which direction it's moving in, so you just need to get the angle from the direction. Use Math.arctan2 to get an angle from the difference between two adjacent points in the bullet path.

  3. #3
    Senior Member
    Join Date
    Feb 2005
    Posts
    1,836
    What's the movement code for your projectiles right now? Rotating will be heavily based upon that and probably Math.atan2.
    .

  4. #4
    Junior Member
    Join Date
    Feb 2012
    Posts
    5
    Thank you for answering, I think this will answer how the movements works.


    gravity = 5
    friction = -3
    vx = 0
    vy = 0
    SpeedF = 15


    this.x += vx;
    this.y += vy;


    xSpeed = Math.cos(((currentRotation + 90) * degreesToRadians));
    ySpeed = Math.sin(((currentRotation + 90) * degreesToRadians));


    x += xSpeed * SpeedF;
    y += ySpeed * SpeedF;

  5. #5
    Senior Member
    Join Date
    Feb 2005
    Posts
    1,836
    That's still not everything, where are vx/vy set?
    .

  6. #6
    Junior Member
    Join Date
    Feb 2012
    Posts
    5
    Oh yeah sorry I forgot;


    vy += gravity;
    vx += friction;

  7. #7
    Senior Member
    Join Date
    Feb 2005
    Posts
    1,836
    What's the deal with the two differen x/y assignments? It seems like you're assigning the x/y position twice. What is happening with your code?
    .

  8. #8
    Junior Member
    Join Date
    Feb 2012
    Posts
    5
    Quote Originally Posted by swak View Post
    What's the deal with the two differen x/y assignments? It seems like you're assigning the x/y position twice. What is happening with your code?
    As you probably can tell, I'm very new at this. So it's possible that the code is bad. I guess it would be better if y/xSpeed affected vx/vy directly instead of the y/x position.

    SpeedF is not always 15, it's also affected by a meter that can sink, which will also sink SpeedF.


    CurrentRotation is the rotation of the shot.

  9. #9
    Senior Member
    Join Date
    Feb 2005
    Posts
    1,836
    What I mean is if you're assigning xy twice than the second one should override the first one thus rendering the first one pointless. It might be better if you could post your code. On the other hand looking at how I think your code should work than the second x/y assignment should actually be vx and vy. Is this right?
    .

  10. #10
    Junior Member
    Join Date
    Feb 2012
    Posts
    5
    Quote Originally Posted by swak View Post
    What I mean is if you're assigning xy twice than the second one should override the first one thus rendering the first one pointless. It might be better if you could post your code. On the other hand looking at how I think your code should work than the second x/y assignment should actually be vx and vy. Is this right?
    Really? I'm pretty sure that += only affects, not overriding the results. So right now, the Y/X position should be affected both by vx/vy, and xSpeed/ySpeed, I believe.

    I tried now to change the second Y/X assignment to vy/vx, but then the code just didn't work. I tried every combination and played around with everything else in the code but the bullets were just acting weird.
    Last edited by Carl Blom; 02-10-2012 at 08:40 AM.

Posting Permissions

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




Click Here to Expand Forum to Full Width

HTML5 Development Center