Java Android: Bullets

Put your problem here if it does not fit any of the other categories.

Java Android: Bullets

Postby MakyOnCover » Fri Jan 20, 2012 9:42 pm

How can I create alot of bitmaps, like bullets, every five seconds with the method onDraw(Canvas canvas) that haves a certain speed, a certain position and a certain angle ?
MakyOnCover
Junior Developer
Junior Developer
 
Posts: 19
Joined: Sat Dec 31, 2011 7:49 pm

Top

Re: Java Android: Bullets

Postby Phyll » Sat Jan 21, 2012 2:13 pm

Hi MakyOnCover,

That's kind of a nebulous question and I haven't actually written any code for "tracer fire" as it seems you would like to create. But if I were to do that...

I would probably create a Tracer object. It would contain a bitmap, its current x,y location, its m and b from the equation for its tajectory (simple straight line). At least. And maybe a counter so that every tracer was not updated at exactly the same time. And maybe a max count so that individual tracer objects could have their own speed.

I would probably have a tracer object represented by a bitmap if the detail had to be great (tail or something). Otherwise I would draw a simple dot of some kind to represent it. A tail would mean also aligning the bitmap with the trajectory (something that could get pretty ugly).

A clock would drive the screen update and do it whenever the counter in my tracer object reached a preset count. That preset number would determine its speed.

During a screen update, the draw routine would go through an array of such tracer objects updating each one that was ready to be drawn by calculating its next position and putting the bitmap there (maybe in the proper orientation) and updating its counter and location information.

If you wanted a more realistic trajectory, you could use actual balistic data and calculate the trajectory based on time and speed for a particular projectile and take gravity and windage into account. That would produce some kind of arc but may actually take too long calculating and drawing for a high frame rate.

Hope this helps.

Phyll
Phyll
Master Developer
Master Developer
 
Posts: 648
Joined: Fri Oct 14, 2011 11:19 am

Re: Java Android: Bullets

Postby MakyOnCover » Sat Jan 21, 2012 2:44 pm

So I just have to create a kind of a arraylist to hold all the objects?! But does it work on android?
MakyOnCover
Junior Developer
Junior Developer
 
Posts: 19
Joined: Sat Dec 31, 2011 7:49 pm

Re: Java Android: Bullets

Postby Phyll » Sat Jan 21, 2012 5:39 pm

Hi MakyOnCover,

Sure, I wouldn't have spent all that time writing if something like that wouldn't work. However, its not a trivial task to do it and what I have given you there is just the concept. There's a lot of details in the code that will probably take a while to work out.

How well it works depends on how many streams of bullets you need to keep moving at any one time, how well its written and how many shortcuts or tricks we can employ to help it work better, faster.

I've never used them but I think there are better graphics libraries that you might be able to use if you look them up and find them. Might make the whole job easier. But you can roll your own within the Android framework.

There of course are some other things to do if you're making a game. Seems like you'd like to know if your bullets hit anything so you need some fast collision algorithms to determine that. You probably need something to shoot at or shoot from that would require similar movement and really could be handled in about the same way.

Anyway, hope this helps.

Phyll
Phyll
Master Developer
Master Developer
 
Posts: 648
Joined: Fri Oct 14, 2011 11:19 am

Re: Java Android: Bullets

Postby MakyOnCover » Sat Jan 21, 2012 8:07 pm

Thank you!! That helped alot!!
MakyOnCover
Junior Developer
Junior Developer
 
Posts: 19
Joined: Sat Dec 31, 2011 7:49 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 8 guests