How to do a projectile in a game?

I’m making a 2D projectile that is going to hit the player whether the player is on the plane unlike an enemy straight forward projectile.

My issue can be simplified by extending a Path3D class a few more units and that’ll be done with.

So far, basing on high school (X,Y) math, I came up with this:

  1. Get the slope of the enemy point (X,Y) and the player point (X, Y).
var slope = (player.y - enemy.y) / (player.x - enemy.x);
  1. Using the slope and one of the points of the new extended coordinate and plug it in for the next:
//Note fixedEdge marks beyond the outer edge of the game scene
var newEndPointX = player.x >= 0 ? fixedEdge + 20 : -fixedEdge - 20;
var newY = slope * (newPointX - enemy.x) + enemy.y;

var endVector = new BABYLON.Vector3(newPointX, newY, 0);
  1. Apply this endVector to a Path3d and have it fire away

When I tried this. There are too many problems. Many are hitting the target but they have very high extremes in terms of speed or distance or even hitting in the correct direction - in the opposite direction.

What am I doing wrong here? I’m clearly missing something here. Thanks for your time.

Hey @pseudoCK

Sounds like an interesting idea.

It’s always best to start with a playground that others can look at to help you solve the issue.

Is it possible for you to do a very simple playground to demonstrate the issue you’re having?

It was a difficult code to create from my project but here it is: https://playground.babylonjs.com/#8HZFUZ#4

I noticed some kind of infinity well is created when x is closer to zero.

Thanks

Managed to solve my own problem.

https://playground.babylonjs.com/#8HZFUZ#53

Turned out that method I used was wrong, but using the Math.atan solved it exactly.

Thanks.

2 Likes

I guessed I spoke too soon. It only works when the source it at the origin.

https://playground.babylonjs.com/#8HZFUZ#230

It seems off so it’s not resolved.

Edit: for the final solution I can find. It’s resolved for now. Thanks.

1 Like