A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: Simple math problem (cannon rotation)

  1. #1
    Senior Member
    Join Date
    Oct 2002
    Posts
    107

    Simple math problem (cannon rotation)

    Hi.

    My problem is easy

    I need to know how to how to make a bullet go in the same direction as the cannon points. I don't need curves or anything, just a straight line forward.

    To explain a bit better: if the cannon points left, the bullet will go left... right-right, and so on for all 360 degrees.

    I just suck at Math and I wanna make a simple minigame so ty for replies

  2. #2
    Prid - Outing Nig 13's Avatar
    Join Date
    Jul 2006
    Location
    Norway
    Posts
    1,864
    Depends on how you are controlling the cannon. If you're moving it with Arrow Keys and changing its _rotation, then set the bullet the same as the cannon's _rotation, but if you're moving the cannon at the mouse's angle, then you'd still set the bullet's angle to the cannon's angle, aka, the _rotation. Tell me which it is, and I'll provide the correct Actionscript

    AS2 or AS3?
    I am back, guys ... and finally 18 :P

    BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS

  3. #3
    Senior Member
    Join Date
    Oct 2002
    Posts
    107
    The cannon actually rotates by itself. Only thing changing is the speed of the rotation (it can be negative, too)

    So, when you press a key, the cannon shoots the object in the cannon in the facing direction in a straight line

    Thats pretty much it

    Edit: Oh and yeah, its _rotation, simple as that

    thanks

  4. #4
    Senior Member tonypa's Avatar
    Join Date
    Jul 2001
    Location
    Estonia
    Posts
    8,223
    The very basic movement based on rotation uses 2 equations, one for x and other for y coordinate:

    //first convert _rotation from degrees into radians
    ang = Math.PI * (cannon._rotation)/180;
    //set the speed
    speed = 5;
    //change the coordinates
    dx = speed * Math.cos(ang);
    dy = speed * Math.sin(ang);
    //now add the components to both coordinates in enterframe event
    bullet.x += dx;
    bullet.y += dy;

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