Public Group

# Math / Programming Question

This topic is 3672 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey guys, I have (what should be) a very easy question. I am new to game design, but have been programming in JAVA for about 6 years now. I am createing a JAVA 3D asteroids game, using JME. I have a sphere set in space which represents Earth. My idea is to have "asteroids" created in various locations (3fVector), and then have the asteroids move toward earth. The player has to shoot the asteroids before they hit Earth. Simple enough, but I am having problems with the math involved in determining the "direction" these asteroids need to move in order to eventually hit earth. I am throwing spheres out in space in random places with the method asteroid.setLocalTranslation(x,x,x); So, how would I then determine the relative position to move to eventually collide with earth? Hope that makes sense, I am sure there is just some mathematic formula where you can take the 3fVector location of the asteroid, and the 3fVector location of the earth, and come up with a directional vector that the asteroid needs to be moved, but I am very lost on what that formula might be. Thanks guys!

##### Share on other sites
Getting a direction vector is simple vector addition/substraction. Substract the asteroids position from the earths position to get a vector that points from the asteroid to the earth. Then, change the length of this vector to the speed you want the asteroid to have and you've got your asteroids velocity.

##### Share on other sites
Well, how I represent projectiles (and essentially we can treat your asteroids as arrows for that matter) is a little silly, but it works.

Essentially, I model a triangle and find the angle the triangle makes. Then I represent a growing circle and plot the projectiles position on that circle using the formula of:

To find the initial angle, I used:

To update the projectile (or asteroid), I calculate the time difference between last update call and the current time, then multiple that by the speed per second, and dividing by 1000 (since GetTickCount() Win32 API in windows returns milliseconds). I then treat that as my radius and add the values to the x and y of the projectile. Keep in mind that you can keep track of a growing radius, and just do something like this:

totalMove += nextMoveAmount;
x = cos(angle) * totalMove;
y = sin(angle) * totalMove;

1. 1
2. 2
3. 3
Rutin
20
4. 4
khawk
14
5. 5

• 9
• 11
• 11
• 23
• 12
• ### Forum Statistics

• Total Topics
633655
• Total Posts
3013186
×