i just cant figure out how this mass thing works .
i have read the box2d docs, (which have many commands that are not in the cocos2d at the moment) and i was trying to understand the mass thing.
i know that if i change the body density it doesnt change a thing :
//add body
b2BodyDef spriteBodyDef;
spriteBodyDef.type =b2_dynamicBody; //b2_staticBody;
spriteBodyDef.position.Set(pilot1.position.x/PTM_RATIO,pilot1.position.y/PTM_RATIO);
spriteBodyDef.userData = pilot1;
pilot1Body = world->CreateBody(&spriteBodyDef);
b2CircleShape spriteShape; //b2polygon-for box shape
spriteShape.m_radius = 15.0/PTM_RATIO;
b2FixtureDef spriteShapeDef;
spriteShapeDef.restitution = 0.4f;
spriteShapeDef.shape = &spriteShape;
//spriteShapeDef.density=10.0f;
b2Fixture *fix=pilot1Body->GetFixtureList();
fix->SetDensity(1.5f);
pilot1Body->ResetMassData();
NSLog(#"mass2: %f", pilot1Body->GetMass());
spriteShapeDef.isSensor = false;
pilot1Body->CreateFixture(&spriteShapeDef);
[self addChild:pilot1];
thanks a lot.
You need to set new density for some or all the fixtures attached to the body using b2Fixture::SetDensity and then call b2Body::ResetMassData, for example:
NSLog(#"mass1: %f", body->GetMass());
b2Fixture *fix = body->GetFixtureList();
while (fix) {
fix->SetDensity(1.5f);
fix = fix->GetNext();
}
body->ResetMassData();
NSLog(#"mass2: %f", body->GetMass());
Related
I need to throw my box2d object till off screen.
For example... Rabbit is moving straight on Jungle Path (Road length around 500 metres). In between it got some power to apply, like Axe. So rabbit need to throw that object forward till the off screen. If any bouncable object (like wall and tree) in midway, thrown object need to come back, else it should go to off screen and hide.
At touch event I called below method to create body and for movement I used setlinear velocity.. but it's not moving straight and smooth; then inbetween if any objects (like wall and tree). How to bounce back (reverse travel)?
[self createbody];
-(void) createbody
{
freeBodySprite = [CCSprite spriteWithFile:#"blocks.png"];//web_ani_6_1
//freeBodySprite.position = ccp(100, 300);
[self addChild:freeBodySprite z:2 tag:6];
CGPoint startPos = CGPointMake(100, 320/1.25);
b2BodyDef bodyDef;
bodyDef.type = b2_staticBody;
bodyDef.position = [self toMeters:startPos];
bodyDef.userData = freeBodySprite;
b2CircleShape shape;
float radiusInMeters = ((freeBodySprite.contentSize.width * freeBodySprite.scale/PTM_RATIO) * 0.5f);
shape.m_radius = radiusInMeters;
b2FixtureDef fixtureDef;
fixtureDef.shape = &shape;
fixtureDef.density = 0.07f;
fixtureDef.friction = 0.1f;
fixtureDef.restitution = 0.1f;
b2Fixture *stoneFixture;
circularObstacleBody = world->CreateBody(&bodyDef);
stoneFixture = circularObstacleBody->CreateFixture(&fixtureDef);
freeBody = circularObstacleBody;
}
-(b2Vec2) toMeters:(CGPoint)point
{
return b2Vec2(point.x / PTM_RATIO, point.y / PTM_RATIO);
}
I am bit confused. How to achieve the above requirement ?
I refer this link:- Box2d object throwing smoother and on same velocity
First, In your Code, u didnt show your movement code(where u used that Linear Velocity)
b2vec2 *move = (30,0);
cart->SetLinearVelocity(move);
above code will move your box2d body with 30 velocity in x axis. when ever any object occurs in middle of road. u need to handle those in below methods
void LevelContactListener::BeginContact(b2Contact* contact)
void LevelContactListener::EndContact(b2Contact* contact)
u will get Fixtures from above method. based on that handle the collisons.(i.e.,) your box2d body(player power) and those road objects collided. set the power to reverse direction (-velocity).
You can set friction to 0 and body will nicely bouncing from walls.
b2BodyDef bodyDef;
bodyDef.type = b2_dynamicBody;
bodyDef.position.Set(100/PTM_RATIO, 100/PTM_RATIO);
b2Body * body = _world->CreateBody(&bodyDef);
b2CircleShape circle;
circle.m_radius = 26.0/PTM_RATIO;
b2FixtureDef ShapeDef;
ShapeDef.shape = &circle;
ShapeDef.density = 1.0f;
ShapeDef.friction = 0.f;
ShapeDef.restitution = 1.0f;
b2Fixture *bodyFixture = body->CreateFixture(&ShapeDef);
To create walls you need create ground body for example:
b2BodyDef groundBodyDef;
groundBodyDef.position.Set(0,0);
_groundBody = _world->CreateBody(&groundBodyDef);
b2EdgeShape groundBox;
b2FixtureDef groundBoxDef;
groundBoxDef.shape = &groundBox;
groundBox.Set(b2Vec2(0,0), b2Vec2(winSize.width/PTM_RATIO, 0));
_bottomFixture = _groundBody->CreateFixture(&groundBoxDef);
groundBox.Set(b2Vec2(0,0), b2Vec2(0, winSize.height/PTM_RATIO));
_groundBody->CreateFixture(&groundBoxDef);
groundBox.Set(b2Vec2(0, winSize.height/PTM_RATIO), b2Vec2(winSize.width/PTM_RATIO,
winSize.height/PTM_RATIO));
_groundBody->CreateFixture(&groundBoxDef);
groundBox.Set(b2Vec2(winSize.width/PTM_RATIO, winSize.height/PTM_RATIO),
b2Vec2(winSize.width/PTM_RATIO, 0));
_groundBody->CreateFixture(&groundBoxDef);
You may add distance joint that will pull object back to the initial position upon its first contact with other collider (see contact listener for details).
I think it is possible to have persistent joint connected your player with throwable object. You just have to handle joint limits (distance) and behaviour (spring and damper).
I have a little issue with my game. In my main game scene I create a Player object from a class, like this:
player = [Player spriteWithFile:#"Icon-Small#2x.png"];
player.position = ccp(100.0f, 180.0f);
[player createBox2dObject:world];
Below is the main chunk of my small Player class that creates the body and the fixture so I can use it in a box2d world.
b2BodyDef playerBodyDef;
playerBodyDef.type = b2_dynamicBody;
playerBodyDef.position.Set(self.position.x/PTM_RATIO, self.position.y/PTM_RATIO);
playerBodyDef.userData = self;
playerBodyDef.fixedRotation = true;
playerBodyDef.linearDamping = 4.0;
body = world->CreateBody(&playerBodyDef);
b2CircleShape circleShape;
circleShape.m_radius = 0.7;
b2FixtureDef fixtureDef;
fixtureDef.shape = &circleShape;
fixtureDef.density = 1.0f;
fixtureDef.friction = 1.0f;
fixtureDef.restitution = 1.0f;
body->CreateFixture(&fixtureDef);
The result of this code is a Box2d object with Icon-Small#2x.png over it. When I move a joystick, a Box2D impulse is applied and the player moves. Simple enough, right?
In non-retina displays, this works fine. However, when I switch to Retina in the simulator, Icon-Small#2x.png is created a little higher and farther to the right, not over the Box2D circle. Then, gravity is applied and they both fall down to the platform. Icon-Small#2x.png falls twice as fast. When I move the joystick, the Box2D circle moves, but Icon-Small#2x.png moves twice as fast and the camera follows it, soon leaving the circle off the screen. I doubt this issue has really anything to do with the code I have here, I feel like its a scaling issue hidden somewhere in my game. Does anyone have suggestions?
Edit:
I move the sprite with:
[player moveRight];
This is moveRight in the player class:
-(void) moveRight {
b2Vec2 impulse = b2Vec2(2.0f, 0.0f);
body->ApplyLinearImpulse(impulse, body->GetWorldCenter());
}
Shouldn't be any issue here, right?
Edit (again):
Here's my update: method-
- (void) update:(ccTime)dt {
int32 velocityIterations = 8;
int32 positionIterations = 1;
world->Step(dt, velocityIterations, positionIterations);
for (b2Body* b = world->GetBodyList(); b; b = b->GetNext()) {
if (b->GetUserData() != NULL) {
CCSprite *myActor = (CCSprite*)b->GetUserData();
myActor.position = CGPointMake( b->GetPosition().x *PTM_RATIO,
b->GetPosition().y * PTM_RATIO);
myActor.rotation = -1 * CC_RADIANS_TO_DEGREES(b->GetAngle());
}
}
b2Vec2 pos = [player body]->GetPosition();
CGPoint newPos = ccp(-1 * pos.x * PTM_RATIO + 50, self.position.y * PTM_RATIO);
[self setPosition:newPos];
}
I have a feeling that the issue is somewhere in here. I've tried changing PTM_RATIO around, but it doesn't affect the speed. Any ideas?
Edit: see comment below, almost have this figured out
You problem probably stems from the fact you are using a #2x image... Read, http://www.cocos2d-iphone.org/wiki/doku.php/prog_guide:how_to_develop_retinadisplay_games_in_cocos2d
There it states:
WARNING: It is NOT recommend to use the ”#2x” suffix. Apple treats those images in a special way which might cause bugs in your cocos2d application.
So to solve your problem read through the information on using png files with the -hd suffix.
For the comment:
Do you have some code that looks something like...
world->Step(dt, 10, 10);
for(b2Body *b = world->GetBodyList(); b; b=b->GetNext()) {
if (b->GetUserData() != NULL) {
CCSprite *sprite = (CCSprite *)b->GetUserData();
sprite.position = ccp(b->GetPosition().x * PTM_RATIO,b->GetPosition().y * PTM_RATIO);
}
}
See how the code loops through all the box2d bodies in the word and sets the position of the sprite that is associated with the box2d body?
i have many sprites in my game,which all have bodies in a b2world. and in order to detect a touch i do the next:
currentPosition = [[CCDirector sharedDirector] convertToGL: currentPosition];
b2Vec2 locationWorld = b2Vec2(currentPosition.x/PTM_RATIO, currentPosition.y/PTM_RATIO);
for (b2Body* b = world->GetBodyList(); b; b = b->GetNext())
{
b2Fixture *bf1 = b->GetFixtureList();
if (bf1->TestPoint(locationWorld) )
{
CCSprite *tempSprite = (CCSprite *) b->GetUserData();
if (tempSprite.tag==2 )
{
now , because my sprite's body is too small,and he is moving, it is very hard to touch it while it moves, so i need to change this code, in order to detect a wide area around this sprite also.
how do i expand the testpoint to be +- more 50pixels ??
thanks a lot.
You could attach a larger fixture to the body and set the sensor flag of the fixture to true. The sensor fixture won't change any of the physics, but you can check if a point falls within its boundaries.
You can create a sensor fixture like this (from SensorTest.h):
b2CircleShape shape;
shape.m_radius = 5.0f;
shape.m_p.Set(0.0f, 10.0f);
b2FixtureDef fd;
fd.shape = &shape;
fd.isSensor = true;
body->CreateFixture(&fd);
See the Box2D manual, section 6.3 (PDF), and SensorTest.h included in the Testbed.
In my game every 2 seconds I create a new body and then I apply an impulse to this Box2d body.
My problem is that impulse is still the same, but the body behaves in two ways:
It slowly goes upwards , or another time it quickly falls down
I want the body to behave in the same way after impulse, is it possible?
Code:
- (void)newBullet
{
CGPoint touchedAt;
touchedAt.x = 184;
touchedAt.y = 1200;
bullet = [CCSprite spriteWithSpriteSheet:spriteSheet rect:CGRectMake(586, 719, 32, 32)];
[spriteSheet addChild: bullet z: 10 tag: 8];
bullet.position = ccp( touchedAt.x , touchedAt.y);
bullet.rotation = 90;
bulletBodyDef.type = b2_dynamicBody;
bulletBodyDef.position.Set(touchedAt.x / PTM_RATIO, touchedAt.y / PTM_RATIO);
bulletBodyDef.userData = bullet;
bulletBodyDef.angle = CC_DEGREES_TO_RADIANS( 90 );
bulletBody = _world->CreateBody(&bulletBodyDef);
b2CircleShape bulletShape;
bulletShape.m_radius = bullet.contentSize.width/PTM_RATIO/2;
b2FixtureDef bulletShapeDef;
bulletShapeDef.shape = &bulletShape;
bulletShapeDef.density = 0.0f;
bulletShapeDef.friction = 0.9f;
bulletShapeDef.restitution = 0.0f;
bulletShapeDef.isSensor = false;
bulletFixture = bulletBody->CreateFixture(&bulletShapeDef);
b2Vec2 force = b2Vec2(6.0f, 4.0f );
bulletBody->ApplyImpulse(force, bulletBody->GetPosition());
}
Video(Sorry for poor quality, but you can see what I mean)
http://vimeo.com/34215327
I don't know how you are calling this method, but I suggest that you have more control over where you call the method and where you do the box2d time step, it might not be related to that, but its worth checking out. What might be happening is that the impulse is not being applied because the solver which determines the speed of the body isn't called until you reset forces (which I assume you are doing), and so the effect is lost.
Hi all
im working on an ipad app using cocos2d objective c and box2d. my question is in relation to box2d and collisions with curves. basically i have circles within a larger circle arena. the smaller circles collide with each other fine but i want to know how to make them collide with the edges of the larger arena circle correctly so that they dont leave the arena and bounce back in. any ideas on how to approach this problem would be great
cheers
ok i got it working but im not sure if its the correct way to do it
int sides = 50;
float x_coordPREV=([Arena contentSize].width/2)*cos(0.0);
float y_coordPREV=([Arena contentSize].width/2)*sin(0.0);
x_coordPREV += Arena.position.x;
y_coordPREV += Arena.position.y;
b2BodyDef arenaBodyDef;
arenaBodyDef.position.Set(0,0);
b2Body *arenaBody = _world->CreateBody(&arenaBodyDef);
b2PolygonShape arenaBox;
b2FixtureDef arenaShapeDef;
arenaShapeDef.shape = &arenaBox;
for(float angle=0.0f;angle<(2*M_PI);angle+=(2*M_PI)/sides){
float x_coord=([Arena contentSize].width/2-10)*cos(angle);
float y_coord=([Arena contentSize].width/2-10)*sin(angle);
x_coord += Arena.position.x;
y_coord += Arena.position.y;
arenaBox.SetAsEdge(b2Vec2(x_coordPREV/PTM_RATIO,y_coordPREV/PTM_RATIO),b2Vec2( x_coord/PTM_RATIO,y_coord/PTM_RATIO ));
arenaBody->CreateFixture(&arenaShapeDef);
x_coordPREV = x_coord;
y_coordPREV = y_coord;
}
this creates a circle out of 50 sides and appears to be doing what i want. if anyone has a better way please let me know
ty
not sur eif this i what you are looking for but i saw this on a forum:
b2BodyDef edgedef;
edgedef.position.Set(0.f,10.f);
b2Body* edge = world->CreateBody(&edgedef);
b2Vec2 vertices[2];
vertices[0].Set(0.0f, 0.0f);
vertices[1].Set(10.0f, 0.0f);
int32 count = 2;
b2PolygonShape polygon;
polygon.Set(vertices, count);
b2FixtureDef edgefixtureDef;
edgefixtureDef.shape = &polygon;
edge->CreateFixture(&edgefixtureDef);
you could see how you could add more points by sending a larger array