What is the android equivalent of java.awt.geom.Area? - java

I want to build complex shapes as the intersection of two circles and a rectangle. After researching a bit, java.awt.geom.Area class seems perfect for this task.
I was dismayed, however, when I discovered that the awt package doesn't come with the android SDK. Does anyone know of any alternatives for android that allows me to create complex shapes by defining the union and intersection of simpler shapes?
Note: Using graphics clipping to draw the shape doesn't work because I don't just want to draw the shapes, I also want to store the shapes in memory to do collision detection and other interactions.

Android Alternatives to java.awt.geom.Area
EDIT: #numan pointed out an excellent option using some classes in the Android SDK that I was unaware of at the time of the original answer:
https://developer.android.com/reference/android/graphics/Region.html
https://developer.android.com/reference/android/graphics/Region.Op.html
Region allows you to define geometric areas, and then you can use Regions op() method with Region.Op enum to calculate intersections and more complex shapes.
Some other options
You can use a Canvas to draw custom shapes, particularly using the clip* methods:
http://developer.android.com/reference/android/graphics/Canvas.html
Here are some pages about 2d graphics in Android:
http://developer.android.com/guide/topics/graphics/2d-graphics.html
http://developer.android.com/guide/topics/graphics/2d-graphics.html#shape-drawable
http://developer.android.com/guide/topics/graphics/opengl.html
Some other good options if your graphics remain the same (or roughly the same) are XML-based:
http://developer.android.com/guide/topics/graphics/2d-graphics.html#drawables-from-xml
And one solution I find quite neat, is using 9-patch drawables:
http://developer.android.com/guide/topics/graphics/2d-graphics.html#nine-patch
Collision detection
It might be overkill for your purposes, but there are a number of game physics libraries:
http://www.andengine.org http://code.google.com/p/andengineexamples/
http://bulletphysics.org
http://www.emini.at/
http://www.dremsus.com/index.php/2012/01/box2d-game-demo-in-android/
Android, libgdx and box2d basics
Or you can roll your own solution:
http://cooers.blogspot.com/2012/08/simple-collision-detection-in-2d.html
http://content.gpwiki.org/index.php/Polygon_Collision
http://www.codeproject.com/Questions/244344/Collision-Detection-in-Android
Collision detection for rotated bitmaps on Android
It really depends on the purpose; for games, you'd probably be best to just use a library; but if collision detection is the only feature you need, you'd be better off doing it yourself to save resources.
Extra Credit: Some indexes of Android libraries
http://www.appbrain.com/stats/libraries/dev
http://www.theultimateandroidlibrary.com/
http://www.openintents.org/en/

Android UI tooklit uses Skia for graphics rendering and skia uses the Region abstractions for set operations on shapes (e.g intersection, union, subtract. see Region.Op class) shapes from Paths or Rects.
Region class will also get your far for simple collision detection with Region.quickContains or Region.quickReject methods.

Related

How to prevent polygon overlap in 3D graphics

I have been drawing 3D graphics using the graphics.fillPolygon() method in Java. It has worked well for me so far. I can rotate the graphics by dragging my mouse across the screen, and I can zoom in and out of my graphics.
My one issue though, is finding a way to draw the polygons in the correct order so that the background polygons are not drawn on top of the foreground polygons. I know that the answer to my problem is common knowledge to the 3D graphics programmer; Some people have told me to use OpenGL, but that is too much for me to learn right now; I just want to create basic 3D graphics. I am looking for a mathematical procedure to organize my polygons in the order that they should be drawn, (from back to front).
I have thought about just taking the average distance to all points of each polygon, but that is an unreliable method. I have been using trigonometry for all of my methods, but I am starting to learn some linear algebra concepts; The use of vectors may be helpful in finding which polygons lie in front.
#Raisintoe, In computer science, binary space partitioning (BSP) is a method for recursively subdividing a space into convex sets by hyperplanes. This subdivision gives rise to a representation of objects within the space by means of a tree data structure known as a BSP tree.
Binary space partitioning was developed in the context of 3D computer graphics,1 where the structure of a BSP tree allows spatial information about the objects in a scene that is useful in rendering, such as their ordering from front-to-back with respect to a viewer at a given location, to be accessed rapidly. Other applications include performing geometrical operations with shapes (constructive solid geometry) in CAD,[3] collision detection in robotics and 3-D video games, ray tracing and other computer applications that involve handling of complex spatial scenes.
See the Wikipedia article here
This approach has been used by video games mega tubes such as Quake. You can find more about it in this excellent article by Michael Abrash where he explains how they used BSP tree in Quake to determine Quake's visible surfaces.
I Hope this helps
Yes I do agree, OpenGL is really complex, and especially modern openGL that forces you to use shaders always can get more in your way of getting things done, than actually helping you. But openGl solves this problem for you. It draws each pixel of the polygon with it's depth value. When you draw the second polygon, the pixel is only updated when it's depth value is closer to the camera than the old one. You can do the same, and you will have a pixel perfect result.
side note: Modern games engines even prefer rendering from the front to the back, because then the expensive pixel calculation in the fragment shader can be skipped for pixels that would be overdrawn anyway.
side note 2: actually you have to enable the depth test and explicitly tell, that you want the closest pixels.

Animate a cube turning via mathematics (Java)

For a school project i have made a program that can solve a rubiks cube (you know, that cube with all the colors). But now my teachers asked if i could do some research and try out 3d animation for one of the sides. But they want to see the mathematical way to do it. I have found a way to move the corners with the use of polar coordinates. But i do not know how i could render a cube in 3d and be able to animate it.
So my question is: how can i render things like a cube in 3d (or are there any good libraries for it) and how could i use these polar coordinates to animate it?
And is there a good tutorial out there for java 3d rendering?
i must say that i have absolutely no experience with 3d rendering, so it might be a bit difficult. But i really would like to try it out.
thanks in advance
Depending on whether you want to take an existing implementation or if you want to build code for 3D animations from scratch, you might try this tutorial. Graphics programming in this area can be quite involved; a full answer of the question is beyond the scope of this site. However, there are a few main areas.
Usage of vector math for transformation of objects (translation, rotation). This can be done directly or via projective geometry using 4-by-4 matrices. The latter is easier for concatenation of transformations.
Backface culling to remove faces of the object which cannot be seen by the camera.
Using a projection and a camera model to transform 3D coordinates to 2D coordinates.
Using a rasterizer to render the 3D vector information to the screen. Here Bresenham's algorithm might be a good start.
I would suggest you look at one of the 3D libraries. One that I've used a lot and found to be excellent is JMonkeyEngine (JME) which is designed for games but would work well for your needs. It also has an excellent tutorial that takes you from basic to very complex. In fact the first step in the tutorial is a revolving coloured cube!
JME takes a huge amount of the work out of 3D modelling. You build a scene in code with materials, lights etc. and JME does all the work to render it. You can even build your models in a tool such as Blender so you don't even need to do the modelling in code. But I will warn you that using modelling tools is definitely not for the faint hearted.

Collision detection on a rotated Image

How would you go about detecting collision on a rotated Image in a game? I am making an asteroids game an I cannot figure out how to make the asteroids properly collide with the rotated spaceship.
If the rotated object is one that implements the Shape interface, it may have a useful implementation of the contains() method.
In paint(), as you prepare to draw the in-motion image, check the pixel colors of the destination points and look for the target object's color(s). The in-motion image and the target object must be, of course, different colors.
Pixel-perfect collision detection, ie. collision detection between images, is generally really hard to do efficiently. For that reason, it is generally a good idea to use an existing, optimized library built for that purpose.
Since you also need support for rotated images, I recommend PoxelColl. It supports pixel-perfect collision detection as well as basic transformations such as scaling and rotation. It provides a Scala port, which is compatible with Java.
What exactly are you using for collision boundaries of your asteroid?
Simplest might be that you can just use circles for everything and implement a circle-circle collision detection (just google it). This may not visually pleasing if your images are not very circle like.
Otherwise, if you have rotating rectangles colliding with other rotating rectangles then you'll have to implement an algorithm using Separating Axis Theorem for 2D Rotated Rectangle Collision.
Another option might be pixel perfect collision detection which is what Chuck was talking about. A quick search turned up this forum post. Proceed with caution though, this method's performance degrades with the size of your images.

Motion Capture with OpenGL on Atom Board

I am looking to make an arm where each joint can be controlled given an input. For example, I would receive a location each millisecond and I would need each joint to move to that new location. This is going to be used to simulate motion capture. Are there any good tutorials about how to do this. I am programming this on an atom board with limited support for openGL. I tried using C++ G3D but it seems to be too complicated for the graphics card. There were some glitches when rendering. Would a more basic OpenGL only approach be better? Are there models for arms that have pivot points built into them?
G3D itself uses OpenGL as a rasterizer backend, so I doubt that would make any differency. After all OpenGL is just a drawing API and doesn't maintain a scene. You send it commands to draw points, lines and triangles and it executes them. It's really nothing more. You need to implement scene management to do anything usefull with OpenGL, and controlling a figure is already a quite complex task.

What should I use to display game graphics?

I have a system in place for a game yet I don't know what I should use to display it. I am making a vertical shooter game and I have written the methods for all the classes controlling enemies and players, but I have not idea how to efficiently display the game. I was thinking a Canvas, that would repaint every frame, but is that really the most efficient method?
Important details:
ideal framerate: 25fps
It is a 2d game
There are anywhere between 25-100 objects on the screen at any one time, all of which are moving
All objects being displayed are images, all in PNG format
The window is 640px by 480px
Right now all the images are loaded as BufferedImage, although I could easily change this
7. I need a coordinate plane. This is the only fundamental part that cannot be changed without completely restructuring my code.
Most importantly the way I have everything set up, every frame all of the objects move and interact in a coordinate plane I devised (deals with collision detection and movement, no graphical component), then everything should get painted to the screen, simply by going through the ArrayLists which keep track of all moving objects and painting them one by one.
If Swing is acceptable, JPanel is double-buffered by default, and a javax.swing.Timer with a period of 40 ms will give you ~25 Hz updates. This example shows the basic approach, while this example shows a number of images in motion.
I need a coordinate plane.
It's not unusual to have the the model and view use different coordinates; all that's needed are functions to map one system to the other. In this game, the view relies on four methods that map tiles to pixels and vice-versa. The same approach is outlined here for map tiles.
You have a number of options available to you:
Firstly, you could use one of the existing Java game frameworks:
JMonkeyEngine (http://jmonkeyengine.com/)
Slick (http://slick.cokeandcode.com/index.php)
(Slick is aimed at 2D graphics, while JMonkey is aimed at 3D and uses OpenGL - While I have looked into their use, I've not actually used them myself)
Alternatively you can code everything yourself. From the sounds of things this is your first (graphical) game, so you may want to read up on a technique known as double buffering, whereby you write each frame off-screen and the just paint the whole thing to screen, as this can lead to smoother animation.
To get you into games development a bit more, I would highly recommend reading this site, Killer Game Programming in Java by Dr Andrew Davison, as he gives some good pointers, and also provides a good progressive learning path for new game developers, and moving them into 2D and then 3D development.
HTH
The answer really depends on whether this game is 2D or 3D.
If your game is 2D, an easy way to do what you want is to use Java's own 2D Graphics API. A good tutorial to start you off (at least in my opinion) can be found at The Java Tutorials. In my experience I have found that Java's graphics API is easy to learn, and is more efficient than one might expect. The basic technique is to have your game code keep track of the positions of all your objects, then translate those coordinates into screen coordinates and display appropriate images at those locations. I made a (very, very simple) game in Java once, and this is the method I used.
If your game is in 3D, OpenGL is definetly the way to go. I have only limited experience with OpenGL, so I'm not sure how easy the Java bindings are to work with. In general, 3D programming is a massive topic, so if this is a first-time project or you aren't prepared for a major time investment, I would attempt to find a good game framework to use or make the game 2D. If you are interested in OpenGL, or 3D programming in general, a quick Google turned up the JOGL project. I would recommend investigating JOGL as a way to access the OpenGL API from within Java code, but for actually learning OpenGL I recommend The OpenGL SuperBible(5th edition), commonly known as "The Blue Book".
The code examples are all in C++, but for the OpenGL functions it could possibly be just a matter of using a wrapper library. For example:
glDrawElements(...);
May become:
JavaGLWrapperObject.glDrawElements(...);
Unfortunately I can't give concrete examples because I haven't used OpenGL with a Java program, but the above example very coarsely approximates how OpenGL ES is used on the Android platform.
As far as performance... Java's API comes with a non-trivial ammount of overhead, but I could see it doing alright for your purposes. You may have to put a little more effort into making your algorithms efficient, and your game may not run as well on less-capable hardware. If you do decide to go the OpenGL route, it will almost certainly be much faster (again, depending on how efficient your algorithms are), but is correspondingly much harder to learn and get started with. Certainly doable, but it will be a challenge.
Canvas will quickly be too slow for 25+ fps targeted.
the number of object is irrelevant, what is is how complex they are.
if it is 100 images will take nothing compared to 1 3D model of Avatar for example.
in Java you can use either Opengl or Java3D
I would tend to go with Opengl as a personel choise

Categories