Advertisement

Shooting a ball in java

Started by November 26, 2018 05:26 PM
5 comments, last by Vaclav 6 years ago

I want to create a simple 2D Java game , where I should be able to shoot a ball with a character (with a spritesheet). The problem is, I don't know which classes or which methods i would need. I thought of using a physics engine called "dyn4j", but actually i don't know much about it and my researches about it failed. I just found some examples of some games, where I looked for a shoot program, but I couldn't find anything: https://github.com/dyn4j/dyn4j-samples/tree/master/src/main/java/org/dyn4j/samples.

  The shot should be realistic as possible and of course collisions should also be handled.
I also thought to use a mathematical function that resembles a parabola for the shot, but I think there should be an easier way.
Do you think i should use dyn4j or another physics engine? 
Which classes or methods would help?

Have you gone through the documentation and samples?

http://www.dyn4j.org/documentation/getting-started/

http://www.dyn4j.org/documentation/

https://github.com/dyn4j/dyn4j-samples

Then onto advanced: http://www.dyn4j.org/documentation/advanced/

This is more than enough to get started. :) You should also be familiar with Java prior to using this library.

Also... You don't need a "tutorial" to show you how to shoot a bullet in the exact theme you're doing it in. You just need to know how to project an object forward with collision. Then apply that concept to your game...

Programmer and 3D Artist

Advertisement

Yes i did.

But the game should be like "Bowman" - if you know what i mean- so should i just forward the ball into a direction and let it fall by gravitation ? I should be able to manage the speed and the direction by dragging the mouse.

 

What I'm going to suggest you do is just load up the library and play around with it as much as possible. Since this has been posted in the beginners section, I'm not sure what games you've made prior and your experience, but at the end of the day hands on experience and seeing how things actually work will benefit you a lot.

If you want to know what functions to call then you need to read up on the classes in this library:

http://docs.dyn4j.org/v3.3.0/org/dyn4j/dynamics/package-summary.html

http://docs.dyn4j.org/v3.3.0/allclasses-noframe.html

You can use the top search bar to find specific things like how to get gravity as an example:

http://docs.dyn4j.org/v3.3.0/org/dyn4j/dynamics/World.html#getGravity--

It also helps to understand some physics! ;) 

I personally prefer to learn new stuff by reading the official documentation, and looking at the classes and their methods, then toying around. :) 

 

Programmer and 3D Artist

Honestly, for a basic 2d game, there is no need for an engine. You just need to use some critical thinking and some programming skills and you can easily hand code your formula for a bullet trajectory. Basically how a bullet works is that it is fired at max force and min. gravity effect. As the shot line (let's say coordinate x) increases, the the fall line (say coordinate y) increases. The trick is that with each increment, the x velocity slows down more sharply, and the y velocity increases more sharply. Many ways to code this, just get creative and think about what needs to happen at the beginning, end , and each step of the bullet life. The biggest factor is how far you want the bullet to travel at max distance, use that to figure out your algorithm for controlling velocity and bullet drop. 

Regarding 2d games, you didn't say whether it is FPS, top down, side scroller.. But, regardless, you can easily do almost anything you need by simply using the AWT library. Drawn graphics, sprites, etc. 

I think this is the best/easiest way to get started: libGDX with Box2D tutorial

This topic is closed to new replies.

Advertisement