#### Archived

This topic is now archived and is closed to further replies.

# How to implement a projectile-motion shooting game (ala-"Worms") in Java/J2ME??

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

## Recommended Posts

Im conceptualizing this game that has mechanics which are similar to: Scorched Earth, the Worms series and Gunbound (if you havent heard of the games, feel free to google them ) Anyway, I was wondering if anyone can suggest a good way of implementing the use of projectile motion in java considering these factors: - the player would have to adjust the angle of the shooter and determine the power at which the projectile would be shot - the projectile will then follow a path determined by the angle and strength it was shot - basic physic laws apply - (i dont know if i will implement a "wind drag" feature yet to the game) - J2ME Specific: J2ME does not use the data types: float and double Ive been considering lots of physics related stuff such as: the path of the projectile (how to compute for it and how to draw it), and the speed of the projectile <<< these are my biggest problems for now. I have been considering how to do this for a LONG time now. any type of help will be GREATLY appreciated, thanks!!!

##### Share on other sites

x speed = sin(angle) * power
y speed = cos(angle) * power

This is the original speed of the projectile.

Now just keep adding x and y speeds to projectiles x & y.
You obviously need gravity, so add some gravity constant to y speed.

And just add a wind force or air resistance to x speed.

That's it.

And last time I used java it had floats...

[edited by - bouncer on March 19, 2004 12:14:20 PM]

##### Share on other sites
thanks Bouncer. That''s quite a bit of help =)

J2ME, which is java for mobile technology (cellphones, PDAs) doesn''t support doubles nor floats... and using this development platform, complexities arise because of the trigonometry involved (but someone told me to use MathFP - fixed point math, to maybe get around the problem..)

I''ll try your suggestion on how to implement the projectile motion, although, as simple as it sounds, i think there''s some things that are probably lacking/ have to be taken into consideration. No offense meant.

Any input is highly appreciated. Please! =) thanks!

##### Share on other sites
You can use the estimated integer value (eg. g = 10 instead of 8) and X and Y have to be integer anyway, so it should not be a problem that float and double is not available.

I'm not quite sure about the Math class and its methods is also available in J2ME or not, then just try it .

^^'
Edit: change decimal -> integer

[edited by - mr_tawan on March 19, 2004 12:41:26 PM]

##### Share on other sites
And also, if your still need the decimal , you might have to implement the fixed-point decimal by yourself.
^^''

##### Share on other sites
tnx mr_tawan. I kindda figured that since drawing into canvas would require decimals, i might not actually need floats and doubles. But the trigonometrical computations might require them.

Anyway, just so it may be clear, the biggest problems that i''m facing about this is the determination of the actual path and speed that the projectile is traveling.

SPEED: For example, a projectile is fired at an upward angle, the projectile must be shown decelerating upwards, stopping at the peak, and then accelerating downwards.

PATH: For example - i''m just assuming the coordinates here (but i hope you guys catch my drift) - if a projectile was fired at 45 degrees NorthEast starting at position (0,0), it must be determined that the path would be: {(0,0), (1,1), (2,2), (3,3)...(x,y)} (and the power of the shot determines how high it gets before going downwards)

I hope this isnt too much help to ask for you guys. I think i can do basic physics, but all the things that have to be considered is overwhelming me so i need some help. Thanks a lot!

##### Share on other sites
The acceleration and decceleration will take care of itself
because of gravity. As for the path, once you have your initial velocity figured out, you can use the equation for projectile motion to find the coordinates. I don't think you'd need to know the path though because you can just check collisions with a circle or bounding box.

// Reverse cos() and sin() depending on screen orientation.// If (0,0) is top-left, then keep it the way it is.// If (0,0) is bottom-left, then switch functions.float speedX = power*cos(angle); float speedY = power*sin(angle);float wind = 0;float gravity = 9.8;float x = speedX*time + 0.5*wind*(t*t);float y = speedY*time + 0.5*gravity*(t*t);

if you want to use fixed point math, check out how Allegro implements them. It's in C but should be easy enough to decipher, or google for the subject.

[edited by - kyc on March 19, 2004 1:17:15 PM]

##### Share on other sites
These are some formulas that don''t take cares about wind:

V0_x = V0 * cos(a)
V0_y = V0 * sin(a)

g = gravity acceleration = 9.867

//trajectory

{ x = V0_x*t
{
{ y = V0_y*t - 1/5 g * t*t

that becomes

y = (V0_y/V0_x)*x-0.5*(g/(V0_x*V0_x))*x*x

//range
x_max = V0*sen(2*a)/g

in the case you have the wind, the x parallel motion, becomes accelerated, than you have to use the standard formula
s = s0 + v0*t + 1/2 a * t**2

{ x = V0_x*t+0.5*wind_acc*t*t
{
{ y = V0_y*t - 1/5 g * t*t

##### Share on other sites
I been looking for a moment to show of my part finished GDI pong clone see my sig (Stay tuned for the multiplayer OpenGL version)

well in your game loop you will want a delta time for each frame
so it runs at the same speed on everyones computer.

Keeping things simple:

float speedX = power*cos(angle);
float speedY = power*sin(angle);

Those be the horizontial (x) and vertical (y) components of the motion of the ball.

If you want to have AI you can calculate the range of the projectile and then add a random error to it, pretty simple.

Every frame you are going to either
float x = speedX*t + 0.5*wind*(t*t);
float y = speedY*t + 0.5*gravity*(t*t);
where t is the delta time since the ball started to move
or
float x += speedX*t + 0.5*wind*(t*t);
float y += speedY*t + 0.5*gravity*(t*t);
where t is the delta time since the last frame.

The previous method is more accurate, but only differ by a few miliseconds depending in the accuracy of the timer.

http://science.palomar.edu/physics/Physics230/Tutor/Tutor06.html

pongv0.1.8.6

[edited by - try_catch_this on March 19, 2004 1:39:29 PM]

[edited by - try_catch_this on March 19, 2004 1:41:14 PM]

##### Share on other sites
Nothing more needs to be taken into consideration, except for what I and others posted... no offence taken.

1. 1
2. 2
Rutin
21
3. 3
JoeJ
18
4. 4
5. 5

• 14
• 40
• 23
• 13
• 13
• ### Forum Statistics

• Total Topics
631719
• Total Posts
3001886
×