How do I accurate tossed projectiles using gravity? [SOLVED]

0 favourites
  • 6 posts
From the Asset Store
With this template you will learn how to use the GooglePlay Games native plugin
  • A long time ago in a different game making engine, I was told if I want a character like Hammer Bros. from Mario to toss a hammer where it will always hit the player if they stand still at any position, I would have to do trigonometry calculations. Though I no longer have the example I was given and don't know how such calculations could be performed in C3.

  • No trig is actually involved. All the math can be found with these two equations that define the path of a projectile.

    X=x0+vx*t

    Y=y0+xy*t+0.5*g*t^2

    Where:

    X/y is the destination

    X0/y0 is the start

    Vx/vy is the velocity

    T is the time

    G is the gravity.

    Anyways. You can solve that various ways. One is to just used a fixed vy and calculate vx.

    Vy=-200

    Vx=g*(x-x0)/(-vy+sqrt(vy*vy+2*(y-y0)*g))

    Anyways, that takes care of calculating the velocities. Just apply that to the movement behavior of your choice.

    If the behavior you want to use only lets you specify speed and angle of motion you can do this:

    Speed = distance(0,0,vx,vy)

    AngleOfMotion= angle(0,0,vx,vy)

    Finally if you apply that formula and the object just disappears the you go a NaN. Why? It just means it’s impossible to hit the target with the provided vy value.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Thanks for the highly detailed explanation. This should likely solve the problem.

    EDIT: I just implemented it and it works like a charm as long as I make the attacker account for Y position differences. But it's incredible seeing it always throw the projectile into the player no matter where they are even if I put in random Y velocities and limit how low they can throw. Thanks!

  • Another way you can solve it is by time instead.

    T=1

    Vx=(x-x0)/t

    Vy=(y-y0-0.5*g*t*t)/t

    Which would make the object have a velocity where it would take 1 second to reach the target.

    A third way I attempted to calculate is to have a fixed speed and just vary the angle. However it gets more complex.

  • How did you figure out all that? I was already trying to make the vectors change with a bunch of expressions like you did, but I definitely didn't get close to the accuracy and complexity of yours.

  • How? It’s all just algebra with the following equations:

    X=x0+vx*t

    Y=y0+vy*t+0.5*g*t*t

    Those have a lot of variables, so most you’ll assume you know the value of beforehand and you use algebra to solve for the unknown variable.

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