0
\$\begingroup\$

Previous thread on projectile slopes

What is the velocity of a translating projectile in 2D?

What I'm trying to do

Get the slope at a constant speed for both X & Y Coordinates though I don't have an understanding on how to use Vectors in programming so I'm going off this guys equations but with both X & Y since I don't understand vectors. I can't tell at this point if there a direction or coordinate.

What I've done so far

Got everything ready

What I need help with

What do I have to put in as Syntax to normalize these 2 points even though their coordinates (Based off the answer on the given thread)

\$\endgroup\$
  • \$\begingroup\$ Learn vectors, you aren't going anywhere if you don't. \$\endgroup\$ – liggiorgio Jun 4 '16 at 22:31
0
\$\begingroup\$

Here's the documentation on the type Vector2 in libGDX. https://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/math/Vector2.html

From this I can guess that code you could use would be something like this:

//Setup the positions of the player and the target position
Vector2 targetPos = new Vector2(Gdx.input.getX(), Gdx.input.getY());
Vector2 playerPos = new Vector2(Player.x, Player.y);

//Now create the vector from the player to the target
Vector2 toTargetVec = new Vector2(targetPos(targetPos.sub(playerPos)));
Vector2 directionalVec = new Vector2(toTargetVec.nor());

//Set the projectile's position
projectileX = Player.x;
projectileY = Player.y;

//Now we use this to start the projectile moving towards the target
projectileVX = directionalVec.x * projectileSpeed;
projectileVY = directionalVec.y * projectileSpeed;

I would highly recommend you learn how vectors work, they're used a lot in game programming.

PS. If you want to find the angle of the projectile, here's how:

projectileAngle = toTargetVec.angle();
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.