Apply force error with Box2D and Cocos2d-x - cocos2d-x

I develop a game using Box2D in Cocos2d-x with Xcode and iPhone simulator. I apply a upper force to a body in a b2World(gravity 0.0f, -10.0f), and I find that the body fly up to different height each time.
When b2World has many bodies, the problem more likely to occur.
And, I also want to ask how does following function set with velocityIterations and positionIterations.
world->Step(dt, velocityIterations, positionIterations);
Thank you.

Maybe you apply force to different point of body. So part of force goes to rotation and part of force goes to moving up.
velocityIterations, positionIterations just increase accuracy, I think you can use
world->Step(dt, 1, 1);

Related

Sidescrolling Box2D Game

When creating Box2D sidescroller game, is it performance friendly to scroll through every object and move it (for example by x axis) as it does in the following code snap:
for(Body b: bodies) {
b.setTransform(-1, b.getPosition().y, b.getAngle());
}
I was also wondering about the rendering, if I render every texture on every body, I would be rendering the whole level at once and it would cost a lot of my precious FPS.
In contrast to the comments saying that this is a good way to do it, I don't think so.
It is not intuitive to move every entity in your world. When you run around through a forest in real life, the trees also don't move around you, but only you move.
Use an OrthographicCamera and change its position. When rendering via a SpriteBatch, draw all bodies exactly on the position where they are (probably with some METER_TO_PIXEL scaling factor) and use spriteBatch.setProjectionMatrix(camera.combined) on your SpriteBatch. This way only the visible bodies will be drawn automatically, since the camera transformation will get rid of all non-visible bodies.
And another hint: Try to avoid Body.setTransform(). Instead try setting linear velocity or apply forces/torque on the bodies. Using setTransform causes non-physical behaviour since it's basically like teleporting and might lead to weird effects and in my case sometimes even bugs.

libgdx box2d flip compound body

Hi I have a box2d compound body which is intended to be a vehicle seen in profile. when it goes backwards i'd like it to face the other way ie flip/reflect on the x axis. is there an easy way to so this? I find nothing in google though much false hits due to other uses of word reflection.
I could recreate the body but I'd have to do the whole out-of-time delete/add thing? and it seems a bit cumbersome. i tried body.getFixtureList and setting the vertices but tyhe method is flagged don't change the list. does that mean don't add remove? or don't edit? Anyways nothing changes on screen. Off to double-check my code...
Am I missing something?
Changing the vertices won't work. Only possibility to do that is to recreate the body as you said. And don't think that it will be slow just because you will have to delete it and then create it again. Box2d could probably create 100 of your vehicles in one frame without having too much of a struggle.
And for the whole flipping process you would scale your vertices x component by -1. And if your vertices origin wasn't in center of shape you would have to adjust the x coordinate of the whole vehicle (or just have the origin properly placed in the first place).
Also when recreating the body you would probably want to save the linear and angular velocity of your original body. And then once you create the new one apply those velocities to the new body. If you don't do that you would make your vehicle freeze once you do the flip.

Making a Continuous Floor (Actionscript 3)

I'm making a game similar to 'Run' (http://www.albinoblacksheep.com/games/run) but I can't quite figure out the floor part. I'm not wanting the sides and a ceiling like in 'Run', just the main floor.
I'm new to Flash game development (AS3) but I know the basics. I realise the character isn't moving and is just turning (the level itself moves). How do a make a continuous floor, do I have to make a very long .fla file, or do I have to do this with code?
All ideas and help appreciated, Thanks. =)
You should use Tiles. Basically, a tile game consists of a lot of squared tiles, which are inserted when appear in screen and removed when goes out.
In your case, maybe you could use a big tile, with screen's size.
You can visualize a good example here, also a very good resource for gamedev on Flash.
You need to generate sprites of the edge of the screen and either recycle them when their x < some value or dispose and create new ones.
I think you can just use Bitmap.draw() to draw different pixels on a Bitmap that you've applied a 3D transformation to. You can see the basic concept illustrated here.

HTML5 Canvas - Animating color, scale, and rotation

I'm new to canvas, so pardon the beginner question. I'm trying to animate a square on a canvas that changes colors, rotates, and scales up and down (this is just for practice). I'm just trying to grasp all of the concepts and create an example that does all of them at once.
What I want it to do is scale up to a certain amount, then once it reaches that amount, it scales back down to the beginning amount (and then repeats). The same goes for the color (continue animating, then go backwards through the colors). How can I accomplish this?
Here's an example of my code that I wrote:
http://jsfiddle.net/ggsFJ/1/
You'll notice a couple bugs:
Once the color gets to yellow, it stops animating.
The scaling obviously doesn't work.
The rotation isn't either clearing the canvas fast enough or something, because it's showing a trail of positions.
Where can I find some resources on accomplishing this? Any help is appreciated.
There's one small problem that's causing all the other problems (well, apart from the yellow - I'm not experiencing that particular one):
ctx.restore;
That line does nothing. You need to call ctx.restore using parentheses. Once you do that, the scaling works, and clearRect() will clear a non-transformed rectangle:
ctx.restore();
And here's the updated demo.
The problem I can see is with your restore method call.
You are just saying ctx.restore (possibly by mistake). It should be ctx.restore();

Any known solutions for Image Differencing in Actionscript?

I'm working with a few programming buddies to create an AS interface to the kinect and one problem we're running into is image differencing. We need to be able to throw out image data that doesn't change from image to image so we can pin-point only things that are moving(i.e. people).
Anyone have any experience with this or a direction we can go?
I would consider creating a pixel bender shader to find the difference and also do any other math or tracking. Pixel bender gets its own thread outside of the normal flash player so you can get some more horse power for your setup. Pixel Bender shaders can be applied to bitmaps, vectors, or video so I think it is perfect for this project. Good Luck!
http://www.adobe.com/devnet/flash/articles/pixel_bender_basics.html
And is is a full collection of shaders including difference
Take a look at the threshold method on BitmapData.
It'll allow you to do this stuff. Their docs have a simple example so check that out.
It might be a long shot, and this is just me rambling, but in sound theory (strange how I'm relating it to image cancellation, but here goes...) the concept of cancellation is when you take a wave sample and add its inverse. It's how you make acapellas from instrumentals + originals or instrumentals from acapellas + originals.
Perhaps you can invert the new image and "normalize" the two to get your offsets? I.e. the first image is 'black on white' and the second image is 'white on black', and then detect the differences for the bitmap data. I know I did a similar method of finding collisions with AS3 a few years back. This would, in theory, cancel out any 'repeating' pixels and leave you with just the changes from the last frame.
With BitmapData your values are going to be from 0 to 255, so if you can implement a cancellation (because a lot of parts of the image are going to stay the same from frame t frame) then you can easily find the changes from the previous frame.
Just a thought! Whatever your solution is it's going to have to be quick in order to beat the flash runtimes' slow speeds. Your Kinect read FPS rate will be greatly hindered with bad code.
Here is some frame differencing code I wrote awhile back. It uses bitmapData: http://actionsnippet.com/?p=2820
I also used this to capture moving colors in a video feed: http://actionsnippet.com/?p=2736