Wednesday, October 30, 2019

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


What I'm trying to do.


So I want to have a projectile that starts at point (x1, y1) and head in the direction of my mouse say point (x2, y2).


To clarify I'm pretty sure it's a vector but I don't know much about them.


Language / Librarys


I'm making the game in Java and LibGDX so Java.MathUtils would come in to play i'm pretty sure. If anyone that can answer don't know these languages, psuedocode will be just fine.


How I've tried it


I'm pretty sure the formula is something alongs the lines of velX := cos(radians) * speed velY := sin(raidans) * speed


And radians starts out as Pi/2 though the tutorial was pretty rough.



Then it would be x += velx * deltatime and same with y.


Can anyone help me out with the translation formula



Answer



You need to learn a little bit more about vectors but I'm going to explain it anyways.


First of all, let's say that your 'game unit' is in pixels and that points (coordinates) and vectors have two components x and y.


Let's assume that you have two given points A = (x1, y1) and B = (x2, y2).


To get the vector between A and B, you have to do the following formula: V = B - A. Since B and A have two components, we can translate this into V = (xB-xA, yB-yA)


enter image description here


However, this vector is pretty much useless because the length of it is the length of the line between A and B. If you have something like this


V = mouse.pos - projectile.pos

projectile.pos += V

it will instantly move the projectile to the mouse because - well - you're asking to move the projectile to the mouse..


The idea here is to get a direction from your projectile (point A) to your mouse or whatever object (point B).


enter image description here


So.. how do we get this direction ? Well, it's easy, the formula is u = V/length(V)


This procedure is normalizing where you just get the direction of a vector and completely ignore its length (sometimes called 'magnitude').


Adding this vector u to your position will move your object 1 unit away from its current position.


The point of it ? Moving your projectile towards a point with a fixed 'speed', like this:


projectile.pos += u * 10;


this code will move your projectile 10 units (let's say pixels) towards your mouse.




EDIT: Note that with this method you don't need to use trigonometry to compute the normalized vector; that means that neither you have precision loss (floats, pi, ...) nor confusing conversions with radians. That's a win :)




EDIT2: For consistence and clarity, I used the word vector and position but these "concepts" are the same: a point has an X and Y component as well as a vector.


In the code you would use the same data structure. Unfortunately I don't know java but I can provide you a c++ example:


struct MyData //That would be public class in java I beleive
{
float x;

float y;
}

Here MyData is a structure that allows two components: x and y. This structure is used for points as well as vectors; eg:


MyData point_a;
MyData point_b;
MyData vector_between;

//Instantiate and/or initialize values ...


vector_between = point_b - point_a;

In C++ there is a concept (operator overloading) that makes you able to perform custom operations between data structures (such as point_b - point_a in the above example). This is really useful because the program won't know how to subtract two "MyData" together unless you write your own method.


I don't think Java enables you to do this "hack", so instead of


vector_between = point_b - point_a;

you could write


vector_between.x = point_b.x - point_a.x;
vector_between.y = point_b.y - point_a.y;


this is what I meant where I directly used "pos" without the associated component.


Moreover, when I used length(V) I meant sqrt(V.x*V.x + V.y*V.y), that is the length/distance between two points (Pythagorean theorem, right ?)


NB: Usually the MyData data structure is, by "convention" called a Vector2 in 2D, Vector3 in 3D etc...


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...