I am currently working on a 3D engine in java and have run across a few problems. Such as, a reliable way to create 3D (or 2D or 1D) geometry in a reliable, simplistic way. Of course, there is no real, straight-forward answer for this, but I'm mainly anticipating this to be a discussion-type-post.. But, nonetheless, I'm looking for a simple, yet reliable way to instantiate geometry in my 3D engine.
(As an example of what exactly it is that I mean, I currently have everything being composed of 3D points (as it is in reality), but telling the engine what the points compose is a difficult task, making it hard to get reliable geometry. I need a more reliable system rather than just using many many points.)
Yay discussion-type question -- first off, just a warning, discussion-type questions tend to be frowned upon at stackoverflow, but I'll pitch in nonetheless:
The first thing that popped into my mind was the Half-edge Mesh Datastructure -- a simple, and powerful-enough-for-everything-I've-needed-so-far structure -- with almost no space-wise superfluous data. This would cover basically all of your mesh structure needs.
Another solution which you'll find in many 3D game engines is the object-tree system (I'm not sure whether that is the correct name), which you;ll find in action in the python panda3D game engine, Blender (kinda, not that familiar with blender), and so on and so forth. The idea is to have a simple Object3D class, which can contain other Object3D objects at relative positions within. This nested hierarchy works very well with complex systems, where parts come together to form more complex parts of other things. This nested hierarchy also lends itself very well to recursive O(log n) collision algorithms, and recursive frustum culling (for graphics). This is also very easy to code -- I wrote a basic 3D engine using object trees in 11th grade, and let's just say I wasn't exactly a MENSA member back in the day.
In any engine, we'd need some way to compute intersections and so on, so we can do frustum culling, physics collisions, and so on and so forth. The trick I used (very efficient, and just intelligent enough), was the AABB tree -- if we already have trees of objects, we simply let every object store & compute its AABB, and hey presto! AABB tree.
Anyways, you say, since I've some experience writing some code like this I must be able to provide samples, right? Yes:
public class Object3D {
public LinkedList<Object3D> children;
public AABB aabb; // the AABB of the object -- constructed
// by union of children's AABBs
public void doesIntersect(Object3D obj){
// intersection using AABB-AABB intersection
// sadly web resources are sorely lacking, so I shall handle this one below
}
}
On AAABB-AABB collisions (if you need to know)
We have two boxes. Each box is aligned with the axes, and each box has a width, height, and depth. To test if they collide, we see whether they overlap when looking along all three axes -- after all, if they don't overlap when we look along even one axis, they can't possibly be colliding. Some fancy math later, we note that:
"Box A and Box B collide" iff "Box A and B have overlapping x coordinates" and
"same for y coordinates" and "same for z coordinates" and
"same for any other dimensions of space you decide to implement".
As it turns out, this is a trivial check, both to code and to compute, which is why AABBs make good bounding volumes for object trees.
Well, for a discussion this answer has gotten rather one-sided. Anyways, comments would be lovely, I'm sure there are some useful things I've missed (verlet integration, raytracing, but all of those are easy on a base as described above)
Related
How would I go about modeling a turgid bag of fluid in Box 2D, i.e. what physics equations would be useful in modeling this? The bag of water could move when touched, but that is the only interaction. Any places equations or models would be much appreciated!
You could use a library for this. Google's LiquidFun is quite good http://google.github.io/liquidfun/
User99345's answer is the way I'd go to model a bag of liquid, but if you're wanting to use an unmodified Box2D library you can model it using instances of b2EdgeShape, b2RevoluteJoint, and b2CircleShape. Whether that's a good enough way to model it, you'll have to decide.
Additional basis/insight for this...
After seeing your question I put together a model for a bag of liquid as a Testbed demo in my dev branch of my fork of Box2D. The demo is called "Bag of Disks" because that's basically what the demo models and it's in the file BagOfDisks.hpp. The code uses a series of edge shapes connected by revolute joints to model a deformable container and fills it with circle shapes (called DiskShape in my fork) to model a liquid. If you build the library and Testbed of my fork you can see for yourself how the model looks.
As a model of a bag of liquid, I'm of the opinion that the code I whipped together has shortcomings like the following:
It must be computationally less efficient than using particle simulation like Google's LiquidFun does.
I don't believe joints as currently implemented in Box2D (or my fork of it), can always ensure containment. That's because I don't believe joints are able to 100% prevent alternative movement of the connected shapes.
The "bag" in the demo doesn't level evenly at it's top like I'd expect a normal bag of liquid to do.
The "bag" doesn't model a buoyancy force that I'd expect the top of a bag to do.
I imagine things can be done from a user level to improve the model's behavior but I suspect less can be done from a user level to improve the model's speed. By user level, I mean as a user of the Box2D library.
While my fork has made many changes to the Box2D library (especially in naming), I don't believe what I've done in the demo itself can't be reproduced pretty closely using the original Box2D library and syntax as I'd said in my first paragraph using instances of b2EdgeShape, b2RevoluteJoint, and b2CircleShape.
As to what physics equations would be useful in modeling this beyond the equations that Box2D already uses, I'm sorry to say that I have no idea at the moment. I am interested in that however and am looking into it as well. Physics equations for this are available of course but the closest related work that I'm aware of from a Box2D user level is what iforce2d put together in his Buoyancy web page.
Hope this answer contributes helpfully to what's already been said.
I am working on a game scene with multiple objects that need multiple materials. I extensively searched online, but I could not find any satisfactory solution.
My scene will have like a river flowing by and the material there will require a separate shader anyway (it will combine many specular and normal maps into what would look like a river) then there is a terrain that would mix two (grass and sand textures) requiring another shader. There is also a player with hands and amour and all.
EDIT: Essentially I wish to find out the most efficient way of making the most flexible multiple material/shader implementation.
Briefly, there is a lot of complex objects around requiring varied shaders. They are not many in number, but there is a lot of complexity.
So using glUseProgram() a lot of times dosn't seem like the brightest idea. Also Much of the shader code could be made univeral like point light calculation. Making a generic shader and using if's and state uniforms could possibly work, still requiring different shaders for the river and likewise diverging materials.
I basically don't understand the organization and implementation of such a generic system. I have used engines like Unreal or possibly Blender which use Node based materials and allowing the customization of every single material without much lag. How would such a system translate into base GPU code?
If you really face timing problems because of too many glUseProgram() calls, you might want to have a look at shader subroutines and use less but bigger programs. Before that, sort your data to change states only when needed (sort per shader then per material for example). I guess this is always a good practice anyway.
Honestly, I do not think your timing problems come from the use of too many programs. You might for example want to use frustum culling (to avoid sending geometry to the GPU that will be culled) and early z-culling (to avoid complex lighting computations for fragments that will be overriden). You can also use level of detail for complex geometries that are far away, thus do not need as much details.
I'm trying to find if a scanned pdf form contains a signature (like making sure a check is signed).
The problem domain:
I will be receiving document packages (multi page pdf's with multiple forms). I have already put together document package classifiers that will check the package for all documents and scale the images to a common size. After that I know where the signatures should be and can scan the area of the document specifically. What I'm looking for is the best approach to making sure there is a signature present. I've considered just checking for a base threshold of dark pixels but that seems so clumsy. The trouble with signatures is that they are not really writing, more of a personal mark.
The only thing I can come up with is a machine learning method to look for loopyness? But I'm not all the familiar with machine learning and don't even know where to start with something like that. Anyone with some suggestions for practical approaches would very appreciated.
I'm coding this in Java if that's helpful at all
What you asked was very broad so there isn't a lot of information that we can give you. However, I can point you to some helpful links:
http://java-ml.sourceforge.net/ --This is a library that you can download that has lots of useful algorithms and other code to include in your program
https://www.youtube.com/playlist?list=PLiaHhY2iBX9hdHaRr6b7XevZtgZRa1PoU --this is a series that explains neural networks (something you might want to look into for your machine learning)
So a big tip I have for your algorithm is to instead of looking for how long exactly all of the loops and things are, look at all of their relative distances
"Relative distances from what?" you say. Well this is where the next tip comes in handy: instead of keeping track of the lines, keep track of the tips of the loops and the order of these points. If you then take the distance between all of them (relatively of course which means to set one of the lengths to zero). Along to keeping track of the distances, you should also keep track of the angles. You would calculate the angle ABC by taking the distance between (A,B), (B,C), and (A,C) (A,B, and C being coordinates on the xy plane) which creates a triangle between the points which allows you to use trigonometry to calculate the angle.
(I am assuming that for all of these you are also trying to detect who's signature it is of course because it actually doesn't really complicate things much at all) When trying to match up the signature detected to the stored signatures to see if they are the "same," don't make it to where the distances and angles have to be exact. Give a margin of error (like use a % range above and below). Here is a tip: Make the margin of error rather large. That way if it is written poorly, it will still be detected. This raises the chances of more than one signature being picked up. Luckily, there is a simply solution to this. Just have it run the algorithm again on the signatures that were found but with the margin of error smaller (you of course don't do this manually, the program does it). Continue decreasing the margin of error until you get only one signature remaining.
I am hoping you have ideas already for detecting where the actual signature is but check for the difference in darkness of the pixels of course. Make sure it is pretty continuous. Also take note of the fact that signatures are commonly signed in both black or blue or sometimes red and other fancy colors.
I'm after an efficient 2D mapping algorithm, and I've tried a number of implementations, but they all seem lacking. I'm hoping the stackoverflow world can help out with some pointers to existing, tried-n-tested algorithms I could learn from.
My goal is to display articles based on the genre of writing; for the prototype, I am using Philosophy, Programming, Politics and Poetry, since those are the only four styles of writing I have.
Each article is weighted based on each category, and the home view will have each category as a header in each corner. The articles are then laid out in word-cloud-like format, with "artificial gravity" placing each item as-near-as-possible to its main category (or between its main categories), without overlapping.
Currently, I am using an inefficient algorithm which stores arrays of rectangles to perform hit-test-and-search every time an article is added to the view, (with A* search patterns to find empty space to fill). By approximating a single destination for all articles of the same weight, and by using a round-robin queue to pick off articles from each pool, I can achieve fresh results (arrays are sorted by weight, then timestamp), with positioning-by-relevance ("artificial gravity").
However, using A* to blindly search seems really wasteful, even with heuristics to make each article check closest to it's target marks first. I need a more efficient way to iterate over a 2D space.
I'm wondering if a Linked-List approach might work better; rather than go searching blindly in all directions for empty space, I can just iterate through connected nodes to ask each one if it has either a) nearby free space, or b) other connected nodes to ask (and always ask the closest node first).
If there are any better algorithms available, or critiques on my methods, any and all help would surely be appreciated.
I am using gwt elemental + java in this gui, but any 2D mapping algorithm in any language will surely help.
[EDIT (request for more details)] : The main problem here is the amount of work each new addition performs; it produces noticable glitches in the ui thread, especially when there is almost no space left, as I am searching many points in a given radius for enough free space to fit the article.
If I cut the algorithm off too soon, I get blank spots that could have been filled. If I let it run too long, the ui glitches pretty bad, and I'm sure users will hate it.
What is the fastest / most efficient way to store and modify collections of 2D space?
You haven't provided enough information to say what would make an algorithm "better." Faster? Produces layouts that are "nicer" by some metric for quality? Able to handler bigger data sources?
There is certainly nothing wrong with arrays, nor with A*. If they are giving acceptable results with the size of problem you are trying to solve, how can they be "wasteful?" Linked data structures are worthwhile only if they reduce cost of frequently needed operations.
If you sharpen the problem, you're more likely to get a useful answer.
At any rate, there is an enormous literature on "graph layout" and "graph drawing." Try searching on these terms. If you can represent your desired layout as a collection of nodes and edges, these might apply. Many are based on simulated spring systems, which seems akin to what you are doing.
I'm creating an organism simulator for Android, so I guess the algorithm would ideally be in Java. I realize that there is a whole Stanford course on Machine Learning available on youtube, but I simply don't have the time to sit through the whole thing, and I think for my purposes the solution could be very simple.
The organism will be interacted with by the touchscreen primarily, or even if it's interacted with through the mic or accelerometer the inputs in the algorithm will mostly amount to coordinate positions for the different limbs. I think it will be inelegant to have a 'scolding' or 'rewarding' mechanism for random behaviors, so I would like to avoid that. So tracking general directions or patterns in movements and being able to repeat them when they have a high enough frequency would be the goal.
To be honest I'm not really sure how hard this is to accomplish, but I'd like to hear any feedback to know how much more I have to research before I can implement it.
EDIT: Is this a genetic algorithm? The problem is I have no idea how to measure a successful or non successful evolution.
EDIT 2: Okay, I'll try to add as much detail as possible. The application is still in concept stage at the moment, but I just wanted to know how difficult the algorithm would be to put it. So I'm building it in Processing, which is really just Java. The organism would be comprised of limbs that have a fixed distance between them, but are allowed to move independently from the center piece. The limbs move around freely and would find random points periodically to ease to. The organism would have a center appendage that has x and y coordinates as well, and each of the outer limbs would move in relation to that. The user could interact with the organism by manually moving the appendages or the center piece with drags on the touch screen. When the organism is being interacted with is where the algorithm would be used, because there's no point in learning from just random numbers. So I guess the algorithm would take the x and y coordinates of the center piece into consideration, and each appendage would have its own version of the algorithm that learns independently from the others. For instance, if the user continually dragged the organism to the right side of the touch screen, it might be more attracted to that place when it isn't being interacted with. I hope that clarifies a little bit.
I think that for your case, you should try to sit down and write down what are the variables that you can observe and what are the variables that you want to predict
Observable variables: position of the appendage, how many times a specific one is interacted with, for how long, ...
Variables you want to predict: which appendage will be interacted with next time, ...
Once you have the input variables and output variables, you can try to go through the list of standard machine learning algorithms. There are Weka(Java), Rapidminer, KNIME ... which are both libraries and standalone tools. Try to throw your problem at the available tools and see if you are doing better than chance.
If you are, tune its parameters. If you are not performing better than chance, you should ask your Data Mining/Machine Learning friends. They will know best what will work for your problem.
Other things that might affect your choice of algorithms:
Are there hidden states?
Are the variables independence?
The way I see it, all you'd need to do is have an array of, for example, the appendage coordinates, then just average them out and have it move towards that point on the screen