File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Game Development and the fly likes Working out destination from start position, power and angle Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Game Development
Bookmark "Working out destination from start position, power and angle" Watch "Working out destination from start position, power and angle" New topic

Working out destination from start position, power and angle

Gaz Johnson

Joined: Apr 27, 2009
Posts: 22
from anyone whos been reading my other thread you'll know what i'm trying to do. For the others : I have a counter on a board. At the minute, I set a dx (the change to make to the x position each time move() is called)and dy value (the change to make to the y position each time move() is called) manually in the code and the counter moves in the given direction.

For the purpose of what i'm trying to work out now think of it in terms of my counter being a football and a net being at the other end of the board

However I have three sliders: 1 to set the initial x,y position, the second sets the angle, the third sets the power then i press a button to shoot

I am working with Vectors (change in y and x) and dont know how to translate the three sliders into an accurate projection of the counter.

Any help, suggestions etc appreciated.
Brian Legg
Ranch Hand

Joined: Nov 07, 2008
Posts: 488
Loop up some typical math functions. You are dealing with distance which is speed(velocity) over time.... time can be grabbed using objects that get the system time, your speed can be calculated based on your initial power, and you can now calculate how far a piece will travel. Make sure to include a friction amount for your board so that your piece will gradually lose speed until it hits a speed of 0. You will want to calculate the distance traveled for a small piece of time, for instance "How far will this piece travel durring the next .01 seconds?". Then you will decrease your speed based on a friction amount and ask the same question again. This will be done in a loop until your distance = 0. Obviously somewhere in this loop you will need to be calling a draw() or update() method to change the screen.

I would get that working before taking it to the next level... that level being the fact that a piece can hit or cause a "collision" with another piece or the side of the board which you will have to detect, cause a change in power/speed, and also cause a change in direction.

When you are ready for that step you may want to read up on "Collision Detection". A simple google search should give you some good examples.

I hope that helped somewhat.

~Currently preparing for SCJP6
Brian Legg
Ranch Hand

Joined: Nov 07, 2008
Posts: 488
One last tip.... triangle math aka trigonometry is a gamers best friend. All projectiles, moving parts, collision detections, etc, etc can all be represented with a basic knowledge of trig. Here's a site that has some simple to complex examples: triangles
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 684
I have done a fair bit of work in collision detection, for some fun projects I have done. keep in mind that your trigonometry formulas will most likely be in double precision floating point, but your java draw commands will be in integers. This can cause some problems if you don't do it right. I have often struggled with "double collisions" where my sprites or whatever will detect a collision, adjust their direction, then detect the collision again before they have had time to separate. So they end up doing some weird kind of wobbly dance.

Another thing is that my motion formulas for spheres typically use the center point, while the draw command will use the top left corner of the square that contains the sphere, so some kind of basic linear transformation is required. No real issues there.

I have found the following javaworld article to be very helpful. It discusses threads and animation applets. The information about threads is a little outdated, it uses some methods that have since been deprecated, and it also uses applets instead of swing, but it really helped me get over the hump.

have fun
I agree. Here's the link:
subject: Working out destination from start position, power and angle
It's not a secret anymore!