Box2D grid based collision - java

First let me quickly tell what I am trying. I am doing tetris-like game using Box2D, I know Box2D is maybe not the best option in this case, but I really need to have normal physics also because the levels have obstacles and if you hit them blocks are supposed to rotate.
Here is image to demonstrate what I mean.
Case 1:
This is how I want it to be.
Case 2:
This was one of my ideas, just to make the block size of falling piece little bit smaller so they fit nicely into the empty space. As expected problem with this was the empty space around the block. Making it look like box doesn't belong there.
Case 3:
Keeping the block size of falling piece same as "wall" block size it never makes it to the empty space, because edges collide and it stays like that.
so how can I make it fit in empty space filling it and without looking weird?
EDIT: If I make size of the falling object smaller it wont work in this case:
And here is image to demonstrate what kind of behavior I want:

1) Use setFixedRotation(true) on your body.
2) Make it little smaller than available space (from both sides). Make the difference so small that effect would be invisible (yet it will be sufficient for box2d).
This should fix your problem.
Edges won't touch, but if necessary you can try removing friction as well.
Note:
Using a physics engine might be an overkill in your use case, but it is not a that bad idea since you won't have to do much if you use it properly.
Good luck.
Update:
1) Don't make objects small. Make them thin. :)
This should solve your first problem.
2) If you want behavior shown in next case, you are expecting free rotation while falling + correct orientation when touching ground. These two things inherently contradict each other.
I would suggest to rethink your strategy. But still if you are determined to do it the same way, there are some weird ways to achieve it (nothing is impossible, right?).
The one I could think of right now is implementing a tendency of all objects to go to nearest right orientation all the time (external moment as a function of current orientation). It should be updated in render. It is effectively a control system, but there is no guarantee that the orientation would be achieved by the time it is going to collide and the physics won't look natural too. Just try to expand your imagination and have fun. I would certainly like to see it if you find out a better way.
Enjoy.

Related

Check if given coordinates are within letter of a text object

Greeting, wise ones!
I am trying to make a generator for pictures like this one. My idea is to make 2 patterns (vertical lines and horizontal lines). After that, I need to make vertical lines only appear "within" the letter but go a bit beyound if they don't intersect a horizontal line. Same for horizontal line, just for being "outside" the letter.
To perform this I need to know, which pixels are "within" letters of the text() object and which are not. This is the only thing, that I can't get my head around. Any ideas on how to implement this?
(If you have a simpler idea of how to make this generator, I'll happily read about them as well, I'm not too sure that mine is the best)
TL;DR: solution
So, initially, I was doing it in Java Processing and was thinking in terms of points and "if" conditions. I couldn't get any meaningful help and abandoned the problem, doing the thing by hand.
However, later I encountered a problem with detecting if a point is within a polygon in Unity and found a solution that includes the concept of raycasting. It's easily implemented in Unity but will require some extra work in something like Java Processing. In any case, this is an excellent answer to my question. I hope it helps anyone who encounters a similar problem.

Implementing a 'Waterfall' simulation in Java

I would like to implement a visualisation of this video in Java as experience to help me understand all of the 'troubles' in creating visualisations. I have some experience in OpenGL, and a good understanding of how to handle the physics involved. However, if anybody knows of any good game engines that may help (or at least do some of the heavy lifting involved in creating a visualisation of the above) I would be grateful.
Also, I noticed that the linked video must use many separate jets in order to operate in the way it does. Is it likely that it was created using something a little lower level such as C? Is it possible to use a higher level language like Java to control such a system?
Honestly, if you want to implement "just that", I think using a game engine is overkill. Just implement a simple particle engine on your own and you are done.
Seriously, that problem is not so difficult, any language can be used for it. The basic principle behind it is the same as behind steam organs or self player pianos. You have an input data that shows what the pattern to play is and you advance it in a given time.
Here is how I would build the basic control system. You take a black and white image. The width is exactly as wide as the number of "emitters" and the length is as long as the pattern needs to be. You read the image and start at the first line. You walk through each pixel in that line and if the pixel is black you emit a drop and if the pixel is white you don't. You then move in a given interval (maybe 25ms) to the next line and set the emitters accordingly.
The cool thing with images is that you can simply paint them in any graphic program. To get the current time to work you render the time into a image buffer in memory, then pass that into the above code. (You even get fonts if you like...)
You can use jMonkeyEngine.
JAVA OPEN GL GAME ENGINE

Feature/blob correlation and histogram analysis

I'm working on a sketch search engine that correlates whatever someone's sketching with a picture in the database (the db is just about 40 pictures now). I'm doing this mostly for fun so I'm not that well-versed in computer imaging techniques.
First of all, are there any rules of thumb on how one should create histograms (bin sizes, ranges, etc)? I'm using some histogram code found at http://www.scribd.com/doc/6194304/Histograms (but ported to JavaCV). Sometimes I get good results, sometimes I get bad results, most of the time I get "meh" results. I've been experimenting a TON with bin sizes and ranges and I'm wondering if comparing higher dimensional histograms may be the answer here.
Second of all, it seems that black makes a very strong presence in my current histogram setup (even a black dot shifts the entire result set). Should this be expected? Or did I screw something up? Example:
And after the dot:
Note how I'm already getting pictures of "earthrise" as "close" matches.
I'm also wondering what methods I should use for blob or feature analysis. I think that stuff like SURF may be overkill because I only want to broadly compare blobs, not accurately map templates. Is there any way I can compare the edges after being passed through a Canny filter? (Low complexity if possible):
For example, here, I want the two smiley faces to be at the top because the needle smiley "blob" is more closely related to the smily face shape than to a bunch of passion fruit or a galaxy.
Phew long question. If you want to try out the engine for yourself, go to http://skrch.dvt.name/ (shameless plug, I know, I know -- only works in FF/Chrome/Safari). Maybe more experienced computer vision people can make suggestions based on results. Oh, I'm using the CV_COMP_BHATTACHARYYA distance when comparing histograms (it seemed that it gave the best results although chi-square isn't bad either).
Is there a background ?
IS it significant ?
Maybe you need to look at whether there is a user-supplied background or not.
then you "just" need to have 2 histogram per db entry, one with bg, one without.
That'll stop earthrise looking like an apple with a dot.
for basic bg separation, try a canny, then taking "outside" and removing it from a copy of the original.

How do those java sand games keep track of so many particles?

Can anyone shed any light on how a program like that might be structured?
What java classes would they employ to keep track of so many particles and then check against them for things like collision detection? Particles need to know what particles they are next to, or that they're not next to anything so they can fall etc.
Here's an example, incase you're not sure what a sand game is.
Arrays, mainly.
A one-dimensional array of actively moving grains, represented by two coordinates (and possible a velocity if you want gravitational acceleration).
A two-dimensional array of bools (or colours), representing the fixed parts of the world.
The simplest physics model is to remove a grain once it is at rest (e.g. when the world positions below, below to the left and below to the right are filled): instead, the corresponding world coordinate is filled in. This keeps the calculations manageable. Allow a grain to shift down if either the left or right below world coordinate is free. Grain collisions between moving grains can be ignored without loss of much verisimilitude.
(I can't quite get over how much CPU power we've got to spare these days!)
The simple version can be implemented without much trouble on a loose friday night (like I did just now). Simply make a program with a 2D array (of bytes, ints, whatever) representing your field. In each tick, iterate over all the elements and do a check:
If the field below (array[x][y+1]) is empty, move 1 spot down (= set [x][y] to empty, [x][y+1] to occupied)
Else, if [x-1][y+1] is empty, go there.
Else, if [x+1][y+1] is empty, go there.
That's the basics. Next you have to add checks like not 'repeating' the calculation for a grain (set a flag at the new position that's checked at the following iterations).
I followed this tutorial, it's quite good, not too long but still points out common pitfalls and things like that.

Scraping data from Flash (Games)

I saw this video, and I am really curious how it was performed. Does anyone have any ideas? My intuition is that he scraped pixels from the screen (one per 'box'), and then fed that into some program to determine the next move.
Is scraping pixel-by-pixel the way to do this, or is there a better way? I am looking to do something similar with either Java or Python.
Thanks
Probably that's the most reliable way. There are ways to inspect what is happening inside a process - looking directly at its internal state and memory - but they are platform-specific and very prone to misbehaving because your dealing with a slightly different version of something - that includes a different flash version as well as a different version of the app. Those methods are more often used for "trainers" for exe games, where there's typically only one or two versions of the executable to worry about.
Lots of screen shots, comparing, figuring out reliable indicator pixels seems the way to go - plus keeping track of what you expect to happen, of course. When the app is running, it should work from a screenshot at a time (hopefully ensuring a consistent picture, with no half-updated views) and then test the minimum number of pixels needed using (perhaps) a decision tree.
There are ways to automate construction of efficient decision trees, but it's probably easier to do it manually based on comparing screen shots. In this case, since Tetris normally creates all new pieces at the same position, with a 1:1 relationship between colour and shape, you can probably determine the shape and position of a new piece from a single pixel colour - so "decision tree" is probably the wrong term, really, in this case - though there are other things the bot needs to read from the screen.
What's more interesting is the logic to actually make gameplay decisions, since that bot clearly isn't just slotting every piece into the most immediately obvious position, but deliberately aiming to create opportunities to clear 3 or 4 rows at a time.
Yes, i think he scanned the pixels. Actually it should be very simple because you only need to scan the new shape for each move. With that information you can locally calculate the grid and further use it for your AI calculations.

Categories