What is the best practice to find the closest body?
Iterating through the box2d world? This seems cpu intensive.
Try to find overlappings with a body that gets bigger until something is found?
Does Box2d offer a direct way to find the closest body?
As far as I know there is no native box2d way to find the closest body. If it were so, box2d also would have to iterate over all bodies internally. It depends on your specific setting if it is more effective to narrow your search on a specific area at first (by a QueryAABB or something) to limit the number of bodies you have to iterate over, but as you did not write something about your intentions and your setting I cannot give you a more specific advice here. Keep in mind that you only need to calculate and compare the square distance of the bodies to your position.
Related
I'm looking for a good method to shift grid points around in a numerical problem that I am solving (gradient descent). I was hoping someone knew of a good resource that discussed simple methods to shift the positions of grid points to make a resulting integration more accurate. I don't think I need a fancy method, just something for one dimension. I'm having more trouble finding a simple explanation than I thought I would.
Something like shifting the points towards larger values of the function I'm integrating would be fine, but I don't want to guess on how to write the cost function for shifting the grid points and want to do something established instead of making a new method.
I'm trying to get my box2d box to bounce when hitting another box2d box. The physical properties to both objects are set thus..
fixtureDef.density=1;
fixtureDef.friction=0;
fixtureDef.restitution=0.9;
And when I apply this line (which is meant to be the correct way of doing things) I get no bounce.
myElementsArray[0].myPhysics.myBody.ApplyImpulse(newVec, myElementsArray[0].myPhysics.myBody.GetWorldCenter());
But when I change the 2nd, part to some random vector, it bounces
myElementsArray[0].myPhysics.myBody.ApplyImpulse(newVec, new b2Vec2(20,20));
Which I don't get, what am I doing wrong?
The "bounce" you are seeing is probably a bit deceptive. I'm not a physics guru, I only know enough be dangerous as my main focus is game programming. Anyway here goes, the first statement uses the center of mass to apply the force to. When you are calculating a "bounce" using restitution, in most physics libraries it would very simply grab the largest restitution between the two collision objects:
restitution = Max( bodyA.restitution, bodyB.restitution )
Then disregarding torque, the new differential final velocities with respect to the restitution is:
–restitution * (bodyA.initialVelocity - bodyB.initialVelocity)
This is also more standardly written as:
–e(V1i - V2i)
Why is this important? Because this is probably the reason you are not seeing a bounce, the two initial velocities (without torque) are zeroing out. Now the second call to ApplyImpulse() uses a coordinate that is not the center of mass of the body. Because of this, Box2D is going to attempt to apply an angular velocity to the body. With that in mind a new series of equations will be used. I'm fairly confident that is where the problem resides, but someone else might correct me or know more about it.
hi guys I'm making a game with maps/levels sizing up from 5000+ x 3000+ px. Maps have movieclips(e.g walls/rooms/stairs) that should be position according to my planned map design.
Should I create it statically on stage? or what should be?
I'm just asking for suggestions on what way should I do this. Thanks!
The most common way to do just that is tile-based approach. The other way is obstacle-based approach, this one uses obstacles as classes with parameters, with metadata stored in the level, and rendered with only visible part of the level in the display list.
Ok, so I'll try to be as descriptive as possible.
I'm working on a project for a client that requires a jibjab-style masking feature of an uploaded image.
I would like to be able to generate a database-storable object that contains anchor/control positions of a bezier shape, so I can pull it out later and re-mask the object. This all is pretty easy to do, except for one catch : I need to create the bezier object from a user-drawn outline.
So far, here's how I imagine the process going:
on mouse down, create a new sprite, beginFill, and moveTo mouse position.
on mouse move, lineTo an XY coordinate.
on mouse up, endFill.
This all works just great. I could just store the info here, but I would be looking at a GIGANTIC object full of tons of pretty useless x/y coordinates, and no way to really make fine-tuning changes outside of putting handles on every pixel. (I may as well give the end user a pencil tool...)
Here's what I'm thinking as far as bezier curve calculation goes :
1: Figure out when I need to start a new curve, and track the xy of the pixel on this interval. I'm imagining this being just a pixel count, maybe just increment a count variable per mouse move and make a new one every x pixels. The issue here is some curves would be inaccurate, and others unnecessary, but I really just need a general area, not an exact representation, so it could work. I'd be happier with something a little smarter though.
2: take each new x/y, store it as an anchor, and figure out where a control would go to make the line curve between this and the last anchor. this is where I get really hung up. I'm sure someone has done this in flash, but no amount of googling can seem to help me out with the way to get this done. I've done a lot of sketching and what little math I can wrap my brain around, but can't seem to figure out a way of converting pixels to beziers.
Is this possible? All I really need is something that will get close to the same shape. I'm thinking about maybe only placing anchors when the angle of the next pixel is beyond 180 degrees in relation to the current line or something, and just grabbing the edge of the arc between these changes, but no matter how hard I try, I can't seem to figure out how to get this working!
Thanks for your help, I'll be sure to post my progress here as I go, I think this could be really useful in many applications, as long as it's actually feasible...
Jesse
It sounds like a lot of work to turn pixels into Bezier curves. You could try using something like the Linear least squares algorithm. http://en.wikipedia.org/wiki/Linear_least_squares
A different tact, could you have your users draw vector graphics instead? That way you can just store the shapes in the database.
Another cool method of converting raster to vector would be something like this iterative program: http://rogeralsing.com/2008/12/07/genetic-programming-evolution-of-mona-lisa/
Good luck
In my answer to this question I discuss using autotrace to convert bitmaps to beziers. I recommend passing your user drawing through this program on the server. Autotrace does a fantastic job of tracing and simplifying so there is no need to try and reinvent the wheel here.
Thanks for the answers, although I guess I probably should be more specific about the application, I'm really only needing an outline for a mask, so converting images to vectors or polygons, despite how cool that is, doesn't really fix my issue. The linear least squares algorithm is mega cool, I think this might be closer to what I'm looking for.
I have a basic workaround going right now, I'm just counting mouse moves, then every X (playing with it to get most desirable curve) moves, I grab the xy position. then, I take every other stored xy, and turn it into an anchor, the remaining xys are turned into controls. This is producing somewhat desirable results, but has some minor issues, in that the speed at which the mask is drawn effects the number of handles, and it's really just getting a general area, not a precise fit.
Interestingly, users seem to draw slower for more precise shapes, so this solution works a lot better than I had imagined, but it's not as nice as it could be. This will work for the client, so although there's no reason to pursue it further, I like learning new things, and will spend some off the clock time looking into linear least equations and seeing if I can drum up a class that will do these computations for me. If anyone runs across some AS3 code for this type of thing, or would like some of mine, let me know, this is an interesting puzzle.
I have a similar problem to this post. I need to display up to 1000 polygons on an embedded Google map. The polygons are in a SQL database, and I can render each one as a single KML file on the fly using a custom HttpHandler (in ASP.NET), like this http://alpha.foresttransparency.org/concession.1.kml .
Even on my (very fast) development machine, it takes a while to load up even a couple dozen shapes. So two questions, really:
What would be a good strategy for rendering these as markers instead of overlays once I'm beyond a certain zoom level?
Is there a publicly available algorithm for simplifying a polygon (reducing the number of points) so that I'm not showing more points than make sense at a certain zoom level?
For your second question: you need the Douglas-Peucker Generalization Algorithm
For your first question, could you calculate the area of a particular polygon, and relate each zoom level to a particular minimum area, so as you zoom in or out polygon's disappear and markers appear depending on the zoom level.
For the second question, I'd use Mark Bessey's suggestion.
I don't know much aobut KML, but I think the usual solution to question #2 involves iterating over the points, and deleting any line segments under a certain size. This will cause some "unfortunate" effects in some cases, but it's relatively fast and easy to do.
I would recommend 2 things:
- Calculate and combine polygons that are touching. This involves a LOT of processing and hard math, but I've done it so I know it's possible.
- Create your own overlay instead of using KML in PNG format, while you combine them in the previous suggestion. You'll have to create a LOT of PNGs but it is blazing fast on the client.
Good luck :)
I needed a solution to your #2 question a little bit ago and after looking at a few of the available line-simplification algorithms, I created my own.
The process is simple and it seems to work well, though it can be a bit slow if you don't implement it correctly:
P[0..n] is your array of points
Let T[n] be defined as the triangle formed by points P[n-1], P[n], P[n+1]
Max is the number of points you are trying to reduce this line to.
Calculate the area of every possible triangle T[1..n-1] in the set.
Choose the triangle T[i] with the smallest area
Remove the point P[i] to essentially flatten the triangle
Recalculate the area of the affected triangles T[n-1], T[n+1]
Go To Step #2 if the number of points > Max