1
\$\begingroup\$

I am building a game within a game engine. The game is a 2D platform, where the player can move left, right, and jump/fall down. Part of the game is the presence of physical projectiles falling from the 'sky'. When these objects are created (in the sky), impulse is applied to them toward the present location of the player [Pic 1]. Please note the impulse is applied one time using the location of the player at object location, it is not constantly being applied to the location of the moving player. The projectile should move toward that location of the player, not necessarily to the location - and it does - but as it reaches its destination the object starts to curl upward [Pic 2].

I have tried several formulas for setting the angle on each tick (every frame), namely

Set Angle Toward: PlayerWasAtX, LayoutHeight
Set Angle Toward: Self.X, LayoutHeight
Set Angle Toward: If (Self.X > Self.PlayerWasAtX) 
                  Then (Self.X - PlayerWasAtX) / 2
                  Else (PlayerWasAtX - Self.X) / 2
                  , LayoutHeight

Where PlayerWasAtX is the stored location of the player upon projectile creation, LayoutHeight is the total height of the canvas, and Self.X is the X-coordinate of the projetile.

In those examples, example 1 would curl, example 2 would keep the object straight down at all times even when falling at a curve, and iirc example 2 just acted wacky.. or it acted the same as example 1.

Any help would be appreciated. Here are my reference pictures:

Pic 1 Pic 2

\$\endgroup\$
1
  • \$\begingroup\$ Closing and migrating: this is not a mathematics question at all. \$\endgroup\$
    – Willie Wong
    Commented Mar 14, 2014 at 8:34

1 Answer 1

1
\$\begingroup\$

You need to set the angle to be the derivative of motion or in lament terms, the angle needs to be between the previous position and the current position.

Specifically, store the previous position prev.x, prev.y somewhere. The on the next frame where you already have calcualted the new position curr.x, curr.y, you calculate the angle between prev and curr and use that for the projectile.

I don't understand the mechanism you are using but it appears to rely on X alone which is bad. The angle should be determined by x & y and relying on the current x & y and the previous ones.

\$\endgroup\$
3
  • \$\begingroup\$ Thank you for your response. I tried your suggestion, setting the angle each frame using the angle function, inputting x1, y1 as the current coordinates and x2, y2 as the prior coordinates. The projectile just spins around furiously about its origin. I have also tried setting the angle manually, using the formula atan(deltaY/deltaX) * 180 / pi, but the projectile either stays straight down, or flips between straight down and straight up. \$\endgroup\$
    – Rawrcasm
    Commented Mar 17, 2014 at 14:09
  • \$\begingroup\$ Update: I've altered the 2nd formula to multiply by 57.2957795 to convert the radian result to degrees, and although there is some progress, it's not quite right. There are still intermittent flips - sometimes it's just pointed upward and stays that way - and it's also rotated just slightly too much when it does fall correctly, i'd say by about 25 degrees or less. \$\endgroup\$
    – Rawrcasm
    Commented Mar 17, 2014 at 14:22
  • \$\begingroup\$ lol, Sorry, I lied. I had the origin of the projectile misplaced. Your suggestion does indeed work. Thank you very much. \$\endgroup\$
    – Rawrcasm
    Commented Mar 17, 2014 at 14:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .