I have a "simple" shooting game where I have a cannon in the center bottom that can shoot bitmaps to enemies (sprites).
Now I don't know if its the collision detection or the sprite itself but if I "spawn" more than one enemy it starts to lagg, after 15 its completly unplayable.
The only thing the sprite does is moves itself in a direction and changes the picture from a spritesheet and holds some variables, nothing much.
Collision detection is by going through an array of enemies and within that I go through a second array of bullets and for each enemy I check if a bullet has hit it by making a Rect by them both and check for overlaps.
Its a bit of code so I don't know if I should post some of it or not but you can ask me to post it if you need.
Might I suggest that you will save yourself a lot of hassle and boilerplate code if you use a game library such as libGDX? It's free, open-source, and will take care of sprites, collision detection, object pooling (to avoid GC kicking in and dragging your game to a grinding halt), input, and more. Also, if you use it right, you can develop your app on your PC with a rapid redeployment cycle (and I'm not talking about an android emulator) and push it to your phone less frequently. Believe it or not, I'm not affiliated with the creators in any way - I just think it's a pretty awesome library, it's amazing that it's free, and you will otherwise experience a lot of pain building everything from the ground up.
Of course if you really insist on doing it all from scratch, then yeah, you're going to have to provide some code.
Related
as part of my project, I need to plot 2D and 3D functions in android using android studio. I know how to plot 2D functions but I'm struggling with 3D functions.
What is the best way to plot 3D functions? What do I need and where do I start?
I'm not looking for code or external libraries that do all the work, I just need to know what I need to learn to be able to do it myself.
Thanks in advance.
I know how to plot 2D functions but I'm struggling with 3D functions.
What is the best way to plot 3D functions? What do I need and where do I start?
I'm not looking for code or external libraries that do all the work, I just need to know what I need to learn to be able to do it myself.
Since you already understand 2D and want to advance to 3D there's a simple and non-optimal method:
Decide on how much z depth you desire:
EG: Currently your limits for x and y in your drawing functions are 1920 and 1080 (or even 4096x4096), if you want to save memory and have things a bit low resolution use a size of 1920x1080x1000 - that's going to use 1000x more memory and has the potential to increase the drawing time of some calls by 1000 times - but that's the cost of 3D.
A more practical limit is matrices of 8192,8192,16384 but be aware that video games at that resolution need 4-6GB graphic cards to work half decently (more being a bit better) - so you'll be chewing up some main memory starting at that size.
It's easy enough to implement a smaller drawing space and simply increase your allocation and limit variables later, not only does that test that future increases will go smoothly but it allows everything to run faster while you're ironing the bugs out.
Add a 3rd dimension to the functions:
EG: Instead of a function that is simply line_draw(x,y) change it to line_draw(x,y,z), use the global variable z_limit (or whatever you decide to name it) to test that z doesn't exceed the limit.
You'll need to decide if objects at the maximum distance are a dot or not visible. While testing having all z's past the limit changed to the limit value (thus making them a visible dot) is useful. For the finished product once it goes past the limit that you are implementing it's best that it isn't visible.
Start by allocating the drawing buffer and implementing a single function first, there's no point (and possibly great frustration) changing everything and hoping it's just going to work - it should but if it doesn't you'll have a lot on your plate if there's a common fault in every function.
Once you have this 3D buffer filled with an image (start with just a few 3D lines, such as a full screen sized "X" and "+") you draw to your 2D screen X,Y by starting at the largest value of Z first (EG: z=1000). Once you finish that layer decrement z and continue, drawing each layer until you reach zero, the objects closest to you.
That's the simplest (and slowest) way to make certain that closest objects obscure the furthest objects.
Now does it look OK? You don't want distant objects the same size (or larger) than the closest objects, you want to make certain that you scale them.
The reason to choose numbers such as 8192 is because after writing your functions in C (or whichever language you choose) you'll want to optimize them with several versions each, written in assembly language, optimized for specific CPUs and GPU architectures. Without specifically optimized versions everything will be extremely slow.
I understand that you don't want to use a library but looking at several should give you an idea of the work involved and how you might implement your own. No need to copy, improve instead.
There are similar questions and answers that might fill in the blanks:
Reddit - "I want to create a 3D engine from scratch. Where do I start?"
Davrous - "Tutorial series: learning how to write a 3D soft engine from scratch in C#, TypeScript or JavaScript"
GameDev.StackExchange - "How to write my own 3-D graphics library for Windows? [closed]"
I currently need some advice about a "simple" issue.
I'm building a game helper, where users may roll dices (6 faced dice, 20 faced dice, etc). Actually, I displaying only the number to the user.
But, to make that much better to use, i want to display THE dice on the screen (on an alert dialog with custom layout for example).
My problem is here, and I don't really know how achieve that. I can't believe that the only way is to create each face of the dice to display it.
For your information, I need theses dices : 6 faced, 8 faced, 12, 20 and special 6 faced. :) That will be a very long task if I have to create all dices :(
The best i can dream, is to have the dice animation, but i guess is a bit too complex for me at this time.
Anyone as some advice about it ?
Best Quality / Highest Effort:
If you want to show a 3-D tumbling die, you probably want to look into the free 3D modeling/rendering/simulation packages. Blender is the first that comes to mind, although there are probably easier free alternatives. They take some time to learn, but you can model a photo-realistic 3D solid and then animate them in real physical simulations.
Going that route, you'd render short (GIF?) animations for each. Then you just have to look up how to display an animated GIF in a dialog (there's plenty of examples available).
Lower Quality / Less Effort
You could just display a single, 2D face of the die being rolled on your dialog, with a number (or pattern of dots) in the center. Depending upon the type of die, the 2D outline of the face could be a square, triangle, pentagon, etc.
The drawing logic for this would be fairly trivial (again, many examples available online). You could even do a crude animation, by having it cycle through several values before stopping on one. Have the cycling rapid at the start and slow at the end, and add a little 'click' sound for each, and you'd have something users would probably like.
I have an object that is displayed on my android in AR using Kudan markerless, but when I rotate my phone so its off screen and then back again the object is either not there anymore or has scaled to an undesirable level. Quite simply, once placed I want the object to continue to exist as if it's really in the world. Like AR is supposed to do right?
I have just started with Kudan and I'm running the Markerless Unity tutorial but it doesn't detail any further settings that would make the object seem as if its in real world space. Currently it only seems vaguely real if you don't move the camera that much. Even then the object is quite jittery. Any tips? Thanks
After some experimentation there seem to be a number of other issues with Markerless Kudan:
1/ very erratic frame rates going from 0 - 60 fps with just one object, even after I have halved the screen resolution. There seems to be no reason the fps drops or increases.
2/ Occasionally very long freezes of 15 seconds or more.
3/ Markerless objects always seem to come closer to the camera steadily as if gravitating. They will eventually end up inside/on top of the camera.
4/ They never look at all like they are in the real world. Always shaking and moving around.
5/ If I keep the camera very still and wave my hand around in front of it, this actually pushes the object around the screen. Why on earth would I want that to happen as default behavior? Is that a bug? Surely it should only move when the camera moves/rotates? Can someone explain why this is happening?
Am I doing something wrong or is this technology still way off from being usable?
1) Frame rate is dependant on a lot of things.
If you have an old or cheap phone, it could be that your processor simply isn't up to the task.
If you're in an environment that is poorly lit or otherwise difficult to track, then the tracker has to do more work and subsequently there is more load on the processor.
Random frame rate drops in Unity are a problem in many games / apps, just because of the nature of Unity.
2) "Freezes" are essentially just the frame rate dropping to 0. See 1).
3) That simply doesn't happen in any of Kudan's demos, so something else must be going on here, possibly the reasons mentioned in 1).
4) Don't really know what you mean by "shaking", but it isn't something I've seen all that much.
5) Markerless tracking works by tracking the camera image, if you wave your hand in front of the camera, your hand becomes part of the tracked image. If you then move your hand away, the tracker attempts to adjust for the change in its "environment" and move the object in relation to your hand.
so I'm working on a game for mobile using AirForAndroid as3. In the game I need to have a lot of enemies at once on the screen (up to 15 would do I assume). These enemies are atm movieclips and contain various movieclips within, such as shapes that work as hitareas which are vital to the games functionality. The enemy-movieclip also contains a png sequence (a walk loop). Right now this seems to be veeeery unefficient for mobile devices, since 4 or 5 enemies at once already makes the game extremely slow on my galaxyS3.
So the question is, is there a diffrent approach to "render" the enemies, to make the display process less "expensive" ... I can't really get around the png sequence itself, which will span to maybe 200 pics combined, walk, run, fight animations and so on. If anybody has an idea or knows a trick or can put me towards a better solution, that would be awesome.
thanks for reading,
ANB_Seth
The MovieClip is very expensive (memory consumption and CPU/GPU performance) object. Especially on mobile devices. I recommend you to give up using MovieClips at all.
There is standard game-dev technique to make animation: Spritesheet animation.
Some links: Tutorial, Performance comparison: MovieClip vs Spritesheet, Descriptions of the main idea and implementation
I'm trying to put a particle system together in Android, using OpenGL. I want a few thousand particles, most of which will probably be offscreen at any given time. They're fairly simple particles visually, and my world is 2D, but they will be moving, changing colour (not size - they're 2x2), and I need to be able to add and remove then.
I currently have an array which I iterate through, handling velocity changes, managing lifecyling (killing old ones, adding new ones), and plotting them, using glDrawArrays. What OpenGl is pointing at, though, for this call, is a single vertex; I glTranslatex it to the relevant co-ords for each particle I want to plot, one at a time, set the colour with glColor4x then glDrawArrays it. It works, but it's a bit slow and only works for a few hundred particles. I'm handling the clipping myself.
I've written a system to support static particles which I have loaded into a vertex/colourarray and plot using glDrawArrays, but this approach only seems suitable for particles which will never change relative location (ie I move all of them using glTranslate), colour and where I don't need to add/remove particles. A few tests on my phone (HTC Desire) suggest that trying to alter the contents of those arrays (which are ByteBuffers, pointed to by OpenGL) is extremely slow.
Perhaps there's some way of manually writing the screen myself with the CPU. If I'm just plotting 1x1/2x2 dots on the screen, and I'm purely interested in writing and not doing any blending/antialiasing, is this an option? Would it be quicker than whatever OpenGl is doing?
(200 or so particles on a 1ghz machine with megs of ram. This is way slower than I was getting 20 years ago on a 7mhz machine with <500k of ram! I appreciate I'm using Java here, but surely there must be a better solution. Do I have to use the NDK to get the power of C++, or is what I'm after possible)
I've been hoping somebody might answer this definitively, as I'll be needing particles on Android myself. (I'm working in C++, though -- Currently using glDrawArrays(), but haven't pushed particles to the limit yet.)
I found this thread on gamedev.stackexchange.com (not Android-specific), and nobody can agree on the best approach there, but you might want to try a few things out and see for yourself.
I was going to suggest glDrawArrays(GL_POINTS, ...) with glPointSize(), but the guy asking the question there seemed unhappy with it.
Let us know if you find a good solution!