[Solved] How do I calculate the physics velocity at an specific angle of the object?

0 favourites
  • 5 posts
From the Asset Store
This is a single chapter from the "Construct Starter Kit Collection". It is the Student Workbook for its Workshop.
  • Hey folks.

    i have a math problem :D

    Abstract:

    A playership (physics) flys at a velocity. It can shot. Shots have the bullet behavior. The shots speed is e.g. 500 when the playership has 0 velocity. Lets say the playership flys at 45degrees with a velocity of 250 and shots. then i want the shot to fly with a velocity of 750 in this direction.

    Lets say the playership turns 180 Degrees, but still drifts with a 250 velocity in the same direction as before. Then i want the shot to fly at the new direction the playership is facing with a velocity of 250.

    How do i do that?

    Here is what i got so far but there must be something wrong with my thinking as shots are going crazy fast as soon as the ship moves:

    Set Bullet speed to

    Self.shotspeed+((playership2.angle)*playership2.physics.velocityX + sin(playership2.angle)*playership2.physics.velocityY)

    I also made a very professional grafic to clarify what i want to archive:

    1st panel: ship flys at 45 degrees with 250px/s. Shots should have a velocity of 500pixel/s + shipspeed.

    2nd panel: Ship flys in the same direction with same speed but faces the other direction. Shots should fly at a speed of 500px/s - shipspeed in at the ships angle

    3rd panel: Ship has no velocity, shots should fly in the direction the ship is facing at 500px/s (normal shot speed)

    4th panel: ship is drifting with 100px/s at 200degrees, ship is "looking" at an angle of 225 degrees. Shot speed should be 500 + the ships velocity at this angle.

    Any help is highly apprechiated :) thank you in advance!

    Kind regards

    Joschi

  • What if the ship is flying sideways, should the bullet fly straight up at 500 px/s? Or should it fly up and drift to the left?

    My guess is that the second picture is correct?

    In this case an easy solution is to apply two Bullet behaviors. One with speed 500 in the direction of the gun. And another with the ship velocity in the direction where the ship is moving.

    You can get the values for the second behavior with these expressions:

    shipVelocity = distance(0, 0, Ship.Physics.VelocityX, Ship.Physics.VelocityY)

    shipMovingAngle = angle(0, 0, Ship.Physics.VelocityX, Ship.Physics.VelocityY)

    .

    Or instead of Bullet behaviors you can set bullet position with events.

    When the bullet is spawned, save these values in instance variables: a1=bullet angle, s1=bullet speed, a2=ship moving angle, s2=ship speed.

    Then on every tick do this:

    Bullet Move s1*dt at angle a1
    Bullet Move s2*dt at angle a2
    
  • Hey Dop2000, thank you, i think that helps, maybe, i rly have to test it but my brains seems not to be functioning correctly right now haha

    Actually i just wand to add the speed from the direction the ship is currently facing...Currently the problem is, when the ship flys straight and shoots straight its faster than the bullets. But if i add the ship velocity to the bullets they also fly faster when the ship "drifts" backwards instead of flying slower. So i think the second bullet behavior is not neccesarry, because bullet should not drift with the ship but fly straight. I think that would feel best and meets the player expectations even if it is physically not correct...

    Kind regards

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Here is a demo with two methods, enable one event group at a time and see which one you prefer:

    dropbox.com/s/mecf2ytroqh8wh4/ShipFiring.c3p

  • Hey dop2000,

    Method1 was what i was looking for, smart way to get the right result, thank you so much :)

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