newbiecplusplus
newbiecplusplus

Reputation: 13

Can't figure out the maths behind a quadratic curve needed for a slingshot

Would like to apologise if this is too maths based.

I have a project where I have to create an AngryBirds game with our teacher's custom game engine, however I am stuck on the maths behind the slingshot. We are not allowed to use any standard libraries. The top left is 0, 0 and the y-axis increases when you go down. The total width of the window is 1280 pixels and the height is 720 pixels. I am trying to make the bird travel further as you pull the bird further left from the sling origin which is 257, 524. I used the y value from release at the start so that the bird doesn't go somewhere else in the y-axis straight after letting go. Currently the bird increases in the y-axis, which is to be expected given that is exactly what my code does. I created a variable determining how far from the origin of the slingshot the bird is once the mouse has been let go and I would like to use this value in a speed calculation. I don't know what values to use in a quadratic formula to make the bird stay on the screen. I have tried to illustrate the window to make it clearer.

float y = getY() + getX()/10 * getX()/10 * (game_time.delta.count() / 10000.f);
setY(y);

//window illustration
------------------------------------------------------------------------------
| (0, 0)                                                                     |
|                                                                            |
|                                                                            |
|                        o         o                                         |
|                 o                                                          |
|             o                              o                               |
|                                                                            |
|bird-> o\ / (257, 524)                             o                        |
|         |                                                                  |
|_________|______________________________________________________(1280, 720)_|

Upvotes: 1

Views: 67

Answers (1)

gordan.sikic
gordan.sikic

Reputation: 1640

You have two problems:

  • lack of knowledge of elementary physics, related to an oblique shot.
  • window origin being in top-left corner implies left the coordinate system.

For the first part, I'd suggest you read some article about an oblique shot physics, like kinematics of projectile motion.

In brief:

  • divide the bird motion into horizontal and vertical parts:

    • horizontal part is the motion with constant speed
    • vertical motion is the motion influenced by the constant force
  • calculate horizontal and vertical components of velocity & position independently as a function of time

  • use calculated position to draw the "bird"

The second problem is easily solved by placing your coordinate system into the lower left part of the window, with y pointing up. This way you have a "right-hand" coordinate system that will be used for all calculations using equations found on the aforementioned link.

When you need to actually 'draw' the bird, use the following transformation for y coordinate:

y_draw = window_height - y_calculated;

Don't forget to add appropriate offsets for x and y to compensate for the fact that the origin for calculus is different from the position of the slingshot.

Upvotes: 1

Related Questions