• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Working out destination from start position, power and angle

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 488
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Brian Legg
Ranch Hand
Posts: 488
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

http://www.javaworld.com/javaworld/jw-03-1996/jw-03-animation.html

have fun
 
reply
    Bookmark Topic Watch Topic
  • New Topic