Recently I have a new project that uses Box2D as physics engine. And I am having some trouble with the body's position.
I want to draw images on the sprite, so I just use body.GetPosition as image's position. But I found that body.GetPosition returns the same value as body.GetWorldCenter, and I just wondering that I did something wrong.
I use box2d flash 2.1a in my project. And I currently subtract this position with half-width and half-height to walk around. Also, can you tell me if there is some way to solve my problem or draw the image in the correct positon in Box2D.
Thank you.
update
so sorry to post before read manual.
after RTM I got that body has two points,first is origin point and the second is center of mass ,so a regular shape's world center is always the same as it's origin point. sorry to post is.
Have you set the image in the userdata for the body? Your getting the body's position but you need to have a function that runs each world tick that can set the images position to the new location. Box2d is just numbers defining a simulation. Those numbers are constantly updating so the image needs to have position set each time there is a game tick. Hope this helps
Related
I am making a basic app that pushes shapes across the screen and detects collision with Sprite Kit. My first attempt was using moveTo on the nodes. The issue I had was with collision, the objects would rotate around each other instead of bounce.
Therefore I found I need to use applyForce OR applyImpulse.
In this situation I have a circle for example that is position off screen at its start of life. We then determine a target exit point, and want to 'flick'/'push' the node in that direction.
I cannot figure out how to applyImpulse towards the target end position I have as a CGPoint. I need to get this to a CGVector but I am not sure what needs to be done. I had a look around and found some Ray tuts but they just show applyForce or moveTo. I am not sure how to calculate this.
I found a site that explains 2D physics well.
http://www.rodedev.com/tutorials/gamephysics/
With this I worked out what the angle needed to be and have a speed that I can control and it works well.
You can move an object by changing manually the x and y position so you can reach your end point. In the update function you change yourObject.position.x and yourObject.position.y if I have understood correctly your question. If not please be more explicit. Hope that helps.
I have been trying to find, but did not succeed. Is there a way to change physicsworld gravity property in such way, that objects would not be attracted towards the bottom of the screen, but to the centre of it instead?
Use an SKFieldNode for that. Just place it at the center of your map and disable gravity by putting code in that sets the gravity to zero (CGVector(0,0))
I would give you code, but I don't use Objective C, so I don't know the exact syntax for it. I can give it a shot though... [physicsBody setGravity: CGVector(0,0)]; PLEASE NOTE I HAVE NO IDEA IF THAT IS CORRECT SYNTAX
EDIT:
The asker requested an example of SKFieldNode in Swift, so here it goes.
For the question asked, what you would do is create a Radial Gravity Field node at the center. This code goes in GameScene.swift (or .m if you're using Objective C, and make sure you change the syntax to Obj-C).
let gravField = SKFieldNode.radialGravityField(); // Create grav field
gravField.position.x = size.width/2; // Center on X axis
gravField.position.y = size.height/2; // Center on Y axis (Now at center of screen)
addChild(gravField); // Add to world
You are probably looking for a SKFieldNode. There are a couple of different types so you will have to read the docs. The one you are probably looking for is called radialGravityField.
Here is the code for setting the characters physics body.
character.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:character.size];
It works fine, I mean it stays where I want it to but as you can see it displays the character's physics body in the bottom left of the screenshot (which is the grey box).
Is this a bug or can this be fixed?
I had a similar problem which may or may not solve yours.
It looks like you are using some sort of viewport system. Perhaps with a node called world and one called camera? If this is the case then you must be moving your world node every frame in order to center the content correctly? When drawing physics, you need to move the world node at -didEvaluateActions instead of -didSimulatePhysics
I'm pretty new to Box2D and cocos2d. I'm trying to do something which I thought would be pretty simple, but it is turning out to be more difficult than I had expected and I cannot find a working answer anywhere.
Basically, I want to move a b2body and rotate it to a certain point with animation.
I can get it to the correct position and rotation with:
targetBody->SetTransform(b2Vec2(10.0f,1.0f),10);
But I have no idea how to animate it there over time. I tried using cocos2d animation on the sprite used for the body, but that doesn't do anything. Any ideas?
There are a couple of ways you could do this.
You could use SetTransform every time step to update the position of the body gradually over time, in effect performing the animation yourself. The drawback with this method is that the body is 'teleporting' to the new position rather than moving, so it has no momentum, which means you can get odd results if it hits anything along the way. Still, if you know it will not hit anything or does not need to react to a collision this would be ok.
The other way is to SetLinearVelocity and SetAngularVelocity to give the body proper movement. This will keep the results of a collision realistic, and you don't need to keep updating anything every timestep. On the other hand, you will need to detect when the body has arrived at the desired position and then set the velocities back to zero, otherwise it will just keep moving. For dynamic bodies you will also need to counter gravity somehow, either by setting the gravity scale of the body to zero, or by applying an upwards force to balance gravity, or by changing the body type to kinematic during the move.
In general, you use Box2D to simulate the physical behavior of objects in relation to each other. The rules of mechanics implemented by Box2D dictate how your cocos2d CCSprites move if you continuously update the translation and rotation of your sprites according to their corresponding Box2d b2Body. You will have some kind of repeatedly invoked tick: method in which you step your Box2d world along in time, and in which you update your sprite positions according to simulation results of Box2d.
This pattern corresponds to b2Bodys of type b2_dynamicBody. Physical laws dictate the motion of the body in this case, and your sprites will follow these simulation results. This is why setting a conflicting position of a sprite by means of a CCAction or even directly will be undone almost instantaneously with the next tick:.
Solution 1: kinematic body type
However, there do exist other modes for a b2Body, and one of these is b2_kinematicBody in which the translation is no longer governed by the world but by the velocities or angular speeds you dictate through setters. So it would be one solution to work with body type b2_kinematicBody as in:
b2BodyDef bdef;
bdef.type = b2_kinematicBody;
With this you would manipulate the velocity and angular speed of a b2Body explicitly. In this case, Box2d is still responsible for moving bodies around, but according the velocities you dictate, and without any force effects of collision or gravity applied to this particular body. Also with this strategy, your CCSprites will follow b2Bodys. Setting conflicting positions for your sprites directly or by applying a CCAction would not make sense for the same reason as described above.
Solution 2: decouple sprites from Box2d
An alternative way to animating sprites would be to fully decouple those sprites from Box2d. In this case, you would simply not have any b2Body that governs the position of the sprite you are going to animate. Now, in this case, only you will dictate the position and rotation of your CCSprite, i.e. directly either through its position property or by applying a CCAction.
currently I'm developing a simple game which uses physics engine (Farseer for XNA).
I would like to ask how can I make the character so that he can walking on the ground, jumping on platform without rotating itself.
Because I need to update the body position and rotation, because it is a physics object so it will response like the usual which will rotate itself.
Anyone know how can I fix it?
For Farseer 2.1 you can set:
body.MomentOfInertia = float.PositiveInfinity;
Farseer 3.0 appears to have a flag for fixed rotation, which appears to do the same thing internally:
body.FixedRotation = true;
Although I also set rotation to zero after the physics update, just to be sure:
body.Rotation = 0;
Don't be afraid to go in, after your physics update runs, and change any physics values you "don't like". Most games fake and fudge things quite a lot.