Math for projectile relative to speed and angle

0 favourites
  • 13 posts
From the Asset Store
Have you ever dreamed of playing in a Speedball game or tournament?
  • I'm having trouble working out the math for my 2d asteroids-style shooter. In particular when the player is firing a projectile from an angle different than their current angle of movement. This image will hopefully describe the issue clearly:

    <img src="https://lh6.googleusercontent.com/-Qqh-EwBXasU/T1vldGGu_HI/AAAAAAAAAec/yJ3AZ-RNcik/s720/angle_bullet_question.png" border="0" />

    The actual ship is using Physics for it's movement.

    The player's speed is determined using this formula:

    sqrt(player_ship.Physics.VelocityX ^ 2 + player_ship.Physics.VelocityY ^ 2)

  • you can use

    distance(0,0,player_ship.Physics.VelocityX,player_ship.Physics.VelocityY)

    it's the same (well ok there's no much gain in term of place on that one)

    For your bullet if it's a bullet behavior you have to give the angle of movement and the speed if I remember so it would be

    angle of the object = angle of the ship

    bullet.movingangle = angle(vector Velocity of the bullet + vector velocity of the ship)

    bullet.speed = magnitude(vector Velocity of the bullet + vector Velocity of the ship)

    In short you have to add the two vector and then retrieve the angle and speed of the solution

    To add the two vector do it this way :

    Global number bulletSpeed = 500 //put what you want
    +On shot
      Local number bVx = 0  //bullet X component of default velocity vector
      Local number bVy = 0  //bullet Y component of default velocity vector
      Local number sVx = 0  //ship X component of velocity vector
      Local number sVy = 0  //ship Y component of velocity vector
      Local number rVx = 0  //result X component of velocity vector
      Local number rVy = 0  //result Y component of velocity vector
      player_ship: Spawn projectile
      System: set bVx to cos(player_ship.angle)*bulletSpeed
      System: set bVy to sin(player_ship.angle)*bulletSpeed
      System: set sVx to player_ship.Physics.VelocityX
      System: set sVy to player_ship.Physics.VelocityY
      System: set rVx to bVx + sVx
      System: set rVy to bVx + sVy
      Projectile: set angle to player_ship.angle
    // if your bullet is just a physic object you can set it's velocity X and Y to rVx and rVy... For bullet behavior you need two more steps
      Projectile: Bullet set moving Angle to angle(0,0,rVx,rVy)
      Projectile: Bullet set speed to distance(0,0,rVx,rVy)

    Tell me if it worked, not sure.

  • Yann,

    Thank you so much for the help. Unfortunately it's still not behaving exactly the way I need it to. This only seems to work if the ship's angle is the same angle as it's velocity (in other words it's facing it's current direction of movement). But if the ship is facing a direction that is different than it's angle of movement the projectile doesn't behave properly.

    Imagine if you were driving a car in space (or a vacuum with no resistance) and you throw a tennis ball out of the side window. The tennis ball should move away from the car (at a 90 degree angle) but it also has the forward momentum of the car and no wind resistance. So while the ball is moving away from the side of the car it will also be moving in the same direction as the car. From the observer in the car it would look the same as if the car was stationary and he threw the tennis ball.

    That's the effect I want when the ship is moving in one direction, rotates his ship (but not changing his angle of movement) and fires a shot. The shot should move relative to velocity of the ship and away from whatever angle the ship is currently facing.

    Thanks again. Hope this helps explain what I'm looking for.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • I don't understand your problem

    seems pretty fine here

    shipShooter.capx

    here I made the ship volontarily shot at 90 degree to test the "bullet should follow the ship"

    And even if you have the weird impression it goes sideway, if you think horizontally it follow the ship quite well.

    ship90degreeShooter.capx

    The impression you have that the bullet doesn't follow, might be caused by the fact that you are accelerating. Your bullet just get the instant velocity the moment you shoot. You can see that in the second capx, when you attain maxSpeed, bullets follow the ship. But when you change direction and your velocity is not constant anymore, the bullet doesn't follow anymore.

    That's an expected behavior

  • Yann

    The "set angle of motion" action wasn't having any effect in your example for some reason, but if you set the angle to "angle(0,0,rVx,rVy)" then it works as expected.

    Using FF 10.0.2

  • R0J0hound

    That's weird I can't see any difference. Using FF or Chrome.

  • R0J0hound

    I tried that and now I am getting the proper movement for the missile. But it causes a new problem. The missile is facing the direction that it's actually going, but I want it to always point away from the front of the ship, even though it's "global" movement may not be in that direction. I updated Yann's example and it shows pretty clearly what the issue is.

    http://dl.dropbox.com/u/28484936/ship_shooter.capx

  • Yann

    I found the cause was a bug: http://www.scirra.com/forum/topic49991_post314880.html#314880

    Are you certain it's working for you? The test I used was get the ship moving in a direction then turn the ship 90 degrees and shoot. The bullets should move with the ship.

    Your 2nd capx works because the angle is 0, which is the only case where the bug doesn't appear.

    robit_studios

    I found that Yann's original example wasn't working quite right due to a bug with the bullet behavior. But I imagine it will be fixed quickly and then Yann's sterling example should work as intended.

    A workaround solution is to not use the bullet behavior at all but use variables for x and y velocities, then move with events:

    http://dl.dropbox.com/u/5426011/fixed/space%20shoot.capx

  • There a simple solution to that robit. Make the bullet invisible and make another sprite follow it at the proper angle (:

  • R0J0hound

    Oh! Seeing your fix, I see how wrong the movement was now. And not using bullet for now will probably be simpler than a sprite following another sprite as I just suggested.

  • Yann & R0J0hound

    Thank you both for your help. I've got my missile working just the way I want it to. I'm glad to hear part of my problem is a bug because I was beginning to go crazy trying to figure out what was wrong. Hope it'll be fixed soon.

  • This is probably a little late but I found an easier solution...

    Add physics behavior to the bullet and spaceship behavior to the ship

    The bullet's physics should have no friction or dampening or gravity (obviously)

    When you spawn the bullet

    Apply physics impulse (some small number like 3.5 or something)

    Apply physics impulse (ship.Spaceship.VectorX/100,ship.Spaceship.VectorY/100)

    It works perfectly and no trig or extra variables needed!

  • ...the first physics impulse is apply physics impulse at angle(ship.angle)

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)