Joint in Box2d with cocos2d - cocos2d-iphone

I'm new in box2d and I tried to create joint between two body.
I wrote a joint like
b2RevoluteJointDef jointDef;
jointDef.bodyA=worm_head;
jointDef.bodyB=worm_body;
jointDef.lowerAngle = -0.25f * b2_pi; // -45 degrees
jointDef.upperAngle = 0.25f * b2_pi; // 45 degrees
jointDef.enableLimit=true;
jointDef.maxMotorTorque = 10.0f;
jointDef.motorSpeed = 10.0f;
jointDef.enableMotor = true;
joint=(b2DistanceJoint*)_world->CreateJoint(&jointDef);
but body is not moving when head is moving.
my tick method is
- (void)tick:(ccTime) dt {
//we update the position of the b2body based on the sprite position
for (b2Body* body = _world->GetBodyList(); body != nil; body = body->GetNext())
{
if (body->GetUserData() != NULL) {
CCSprite *spritedata = (CCSprite *)body->GetUserData();
if(spritedata.tag==1)
{
b2Vec2 b2Position = b2Vec2(SCREEN_TO_WORLD(spritedata.position.x),
SCREEN_TO_WORLD(spritedata.position.y));
float32 b2Angle = -1 * CC_DEGREES_TO_RADIANS(spritedata.rotation);
body->SetTransform(b2Position,b2Angle);
}
else {
spritedata.position = ccp(body->GetPosition().x * PTM_RATIO,
body->GetPosition().y * PTM_RATIO);
spritedata.rotation = -1 * CC_RADIANS_TO_DEGREES(body->GetAngle());
}
}
}
}
Why is not moving ? How should I change my code ?

In b2RevoluteJointDef , one body is static body and another is dynamic body. My problem is using two dynamic problem. Now, it solved.

Related

Box2D - Firing bullet from rotating gun

i have a little trouble as title said: i can't figure out how to shoot the bullet toward the direction the gun is pointing at.
Here's short version code, for my bullet firing:
b2BodyDef bd;
bd.type = b2_dynamicBody;
bd.bullet = true;
bd.position = gun->GetPosition();//bullet start at the middle of the gun
m_bullet = m_world->CreateBody(&bd);
m_bullet->CreateFixture(&fd);
m_bullet->ApplyLinearImpulseToCenter( ??? ,true);
At first i thought first param is the direction you want the body go toward so i put in gun->GetWorldPoint(b2Vec2(0.0f,-5.0f)) (middle of the gun's muzzle). Big miss! After a while i thought i should try put in the vector of current gun's rotation degrees b2Vec2 vect = b2Vec2(cos(angle * PI/180), sin(angle * PI/180)); but then the bullet won't fly at all. Now i am all out of ideas. Please, some light.
Full version of code:
public:
b2Body* m_bullet = NULL;
b2Body* gun;
b2RevoluteJoint* joint1;
b2FixtureDef fd;
TestingStuff()
{
{
//body
b2CircleShape circle1;
circle1.m_radius = 1.6f;
fd.shape = &circle1;
fd.density = 1.0f;
fd.filter.groupIndex = -1;
b2BodyDef bd1;
bd1.type = b2_staticBody;
bd1.position.Set(-5.0f, 9.0f);
b2Body* body1 = m_world->CreateBody(&bd1);
body1->CreateFixture(&fd);
//gun
b2PolygonShape box;
box.SetAsBox(0.5f, 5.0f);
fd.shape = &box;
fd.density = 1.0f;
fd.filter.groupIndex = -1;
b2BodyDef bd2;
bd2.type = b2_dynamicBody;
bd2.position.Set(-5.0f, 8.0f);
gun = m_world->CreateBody(&bd2);
gun->CreateFixture(&fd);
//joint
b2RevoluteJointDef jd1;
jd1.Initialize(gun, body1, bd1.position);
jd1.enableMotor = true;
jd1.maxMotorTorque = 90;
jd1.motorSpeed = 180 * DEGTORAD;//DEGTORAD=0.0174532925199432957f
joint1 = (b2RevoluteJoint*) m_world->CreateJoint(&jd1);
}
}
void Keyboard(int key)
{
switch (key)
{
case GLFW_KEY_COMMA:
if (m_bullet != NULL)
{
m_world->DestroyBody(m_bullet);
m_bullet = NULL;
}
{
//bullet
b2CircleShape shape;
shape.m_radius = 0.25f;
fd.shape = &shape;
fd.density = 1;
fd.restitution = 0.05f;
fd.filter.groupIndex = -1;
b2BodyDef bd;
bd.type = b2_dynamicBody;
bd.bullet = true;
bd.position = gun->GetPosition();
m_bullet = m_world->CreateBody(&bd);
m_bullet->CreateFixture(&fd);
m_bullet->ApplyLinearImpulseToCenter( ??? ,true);
}
break;
}
}
After a good sleep i found the solution after few more trial and error.
//bullet
float degAngle = joint1->GetJointAngle() * RADTODEG + 180;
b2Vec2 vect = b2Vec2(sin(degAngle* (b2_pi / 180)) * 10, cos(degAngle* (b2_pi / 180)) * 10);
m_bullet->ApplyLinearImpulseToCenter(vect ,true);
The * 10 is to increase the impulse, make the bullet fly faster and farther, for the sake of testing i just make it 10. Notice that this code is just for testing, if you want to make it more realistic, increase the impulse as well as make the bullet go toward the muzzle instead of go toward the vector it had been fired at.

Cocos2d - Moving the Layer/view based on ball movement

My game world space is a rectangle of 4 screen sizes (2 rows x 2 columns )
I am moving the camera based on the movement of a ball. The ball position is set according to the physics engine.
I notice that, sometimes the ball changes direction slightly as the camera is moving to follow the ball.
Initially I thought it was sort of an illusion based on the movement of the camera, but when I draw a path, the line is of crooked at times.
I used some of the code behind the CCFollow implementation code (not CCFollow itself, but the code behind it), for following the ball
(I had issues with CCfollow)
Below is my Tick() function:
————————————————–
- (void)tick:(ccTime) dt {
bool ballMov = false;
CGPoint pos;
CCSprite *ballData;
dt = 1.0 / 60.0f;
world->Step(dt,8, 3 );
for(b2Body *b = world->GetBodyList(); b; b=b->GetNext()) {
if (b->GetUserData() != NULL) {
CCSprite *tempData = (CCSprite *)b->GetUserData();
if (tempData == ball){
ballData = tempData;
ballMov = true;
ballData.position = ccp(b->GetPosition().x * PTM_RATIO, b->GetPosition().y * PTM_RATIO);
}
}
}
//
// Move the layer so we can keep looking at the travelling ball
// Code is similar to CCFollow code with some adjustments
pos = ballPosition;
if(ballMov){
#define CLAMP(x,y,z) MIN(MAX(x,y),z)
pos = ccp(CLAMP(pos.x,leftBoundary,rightBoundary), CLAMP(pos.y,bottomBoundary,topBoundary));
#undef CLAMP
pos = ccpSub(pos, halfScreenSize );
pos.x = - pos.x ;
pos.y = - pos.y ;
//calculate delta move towards the new position
if(gunShot){
CGPoint moveVect;
CGPoint oldPos = [self position];
double dist = ccpDistance(pos, oldPos);
if (dist > 1){
moveVect = ccpMult(ccpSub(pos,oldPos),0.4); //0.05 is the smooth constant.
oldPos = ccpAdd(oldPos, moveVect);
pos = oldPos;
}
}
}
//move towards the ball
[self setPosition:(pos)];
lastPosition = pos;
ballPosition = CGPointMake([ball body]->GetPosition().x * PTM_RATIO, [ball body]->GetPosition().y * PTM_RATIO );
//if ball is out of gameworld space then reset
if (!CGRectContainsPoint ( outerBoundaryRect, ballPosition)){
ball.visible = false ;
[self recreateBody]; //Ball is out of Gameworld, recreate it at the default position
}
}

Getting EXC_BAD_ACCESS signal received when i tried to get collision detection

I stuck at this position and don't know what went wrong in this,
I have enabled ARC in my project. And i made softBody as follows
Ball.h
B2Body *body[NUM_SEGMENT];
CCSprite *ball;
Ball.mm
ball = [CCSprite spriteWithFile:#"Ball1.2.png"];
ball.tag = 1;
for(int i=0;i<NUM_SEGMENT;i++){
float theta = deltaAngle*i;
float x = radius * cosf(theta);
float y = radius * sinf(theta);
b2Vec2 circlePosition = b2Vec2(x/PTM_RATIO,y/PTM_RATIO);
b2BodyDef bodyDef;
bodyDef.type = b2_dynamicBody;
bodyDef.position = (center + circlePosition);
bodyDef.userData = &ball;
body[i] = world->CreateBody(&bodyDef);
outerBodyFixture[i]=body[i]->CreateFixture(&fixtureDef);
[bodies addObject:[NSValue valueWithPointer:body[i]]];
}
And I have given physics to the tiles as follows,
Tile.h
CCSprite *tile;
Tile.mm
tile = [layer1 tileAt:ccp(i, j)];
tile.tag = 0;
b2BodyDef tileDef;
tileDef.type = b2_staticBody;
tileDef.position.Set((tile.position.x+(tile.contentSize.width/2))/(PTM_RATIO), (tile.position.y + (tile.contentSize.height/2))/PTM_RATIO);
tileDef.userData = &tile;
tileBody = world->CreateBody(&tileDef);
Now i tried to catch collision detection and I have made code which will print the tag number of colliding bodies. The code is as follows,
std::vector<MyContact>::iterator pos;
for (pos=_contactListener->_contacts.begin();
pos != _contactListener->_contacts.end(); ++pos) {
MyContact contact = *pos;
b2Body *bodyA = contact.fixtureA->GetBody();
b2Body *bodyB = contact.fixtureB->GetBody();
if (bodyA->GetUserData() != NULL && bodyB->GetUserData() != NULL) {
At this point Getting ERROR: EXC_BAD_ACCESS
CCSprite *spriteA = (__bridge CCSprite *) bodyA->GetUserData();
At this point Getting ERROR: EXC_BAD_ACCESS
CCSprite *spriteB = (__bridge CCSprite *) bodyB->GetUserData();
printf("contact :%d \n",spriteB.tag);
}
}
Don't Know whats wrong with this code,,Give me some solution for this
Your problem is that you store in userData a pointer to a pointer rather than the pointer itself.
tile is already pointing to a CCSprite instance and &tile points to a pointer to a CCSprite instance making your casting incorrect.
So change it to :
tileDef.userData = tile;

Cocos2D - adding support for normalized position

I want cocos2d to use the nodes' position as a normalized factor when coordinates are less than 1, and otherwise when coordinates are greater than 1, I want cocos2d to treat coordinates as it would normaly do
For instace position (1,1) would locate the node at the top-right corner of the screen, while (.5,.5) would locate the node on the center of the screen.
I was thinking on rewriting the code just where cocos2d pass on the coordinates to the vertex buffer like the following pseudo code
if (Position.x <= 1)
BufferPosition.x = Screen.Width * Position.x;
else
BufferPosition.x = Position.x;
if (Position.y <= 1)
BufferPosition.y = Screen.Height * Position.y;
else
BufferPosition.y = Position.y;
So my question is where would you put this code so that cocos2d works that way with no problem, or what approach would you take istead if it's different from the shown above
Update
I changed every single call/assignment of positionInPixels_ in the entire cocos2d library for positionInPixels
This means that each time, the corresponding getter and setter is caled instead of the CCNode's positionInPixels_ member variable.
Then I changed setPosition, and setPositionInPixels in CCNode.m like this
setPosition
-(void) setPosition: (CGPoint)newPosition
{
CGSize winSize = [[CCDirector sharedDirector] winSize];
position_ = newPosition;
if (position_.x <= 1.0) {
positionInPixels_.x = position_.x * winSize.width;
}
else
{
if( CC_CONTENT_SCALE_FACTOR() == 1 )
positionInPixels_.x = position_.x;
else
positionInPixels_.x = newPosition.x * CC_CONTENT_SCALE_FACTOR();
}
if (position_.y <= 1.0) {
positionInPixels_.y = position_.y * winSize.height;
}
else
{
if( CC_CONTENT_SCALE_FACTOR() == 1 )
positionInPixels_.y = position_.y;
else
positionInPixels_.y = newPosition.y * CC_CONTENT_SCALE_FACTOR();
}
isTransformDirty_ = isInverseDirty_ = YES;
#if CC_NODE_TRANSFORM_USING_AFFINE_MATRIX
isTransformGLDirty_ = YES;
#endif
}
setPositionInPixels
-(void) setPositionInPixels:(CGPoint)newPosition
{
CGSize winSize = [[CCDirector sharedDirector] winSize];
positionInPixels_ = newPosition;
if (position_.x <= 1.0) {
position_.x = positionInPixels_.x / winSize.width;
}
else
{
if( CC_CONTENT_SCALE_FACTOR() == 1 )
position_.x = positionInPixels_.x;
else
position_.x = newPosition.x * 1/CC_CONTENT_SCALE_FACTOR() / winSize.width;
}
if (position_.y <= 1.0) {
position_.y = positionInPixels_.y / winSize.height;
}
else
{
if( CC_CONTENT_SCALE_FACTOR() == 1 )
position_.y = positionInPixels_.y;
else
position_.y = newPosition.y * 1/CC_CONTENT_SCALE_FACTOR() / winSize.height;
}
isTransformDirty_ = isInverseDirty_ = YES;
#if CC_NODE_TRANSFORM_USING_AFFINE_MATRIX
isTransformGLDirty_ = YES;
#endif
}
This is working so far, even when using normalized coordinates with CCActionInterval
So you really think that this variant is the right one? To change the whole framework instead of just adding one method to the CCNode's extension? I think you will see a lot of problem with positioning very soon.
Also it is not good, that (1.f, 1.f) is always have coordinates (screenWidth, screenHeight). You will have a lot of troubles with small nodes. IMHO if you need such positioning, it is much more appropriate to connect this "factor" coordinates to the node's parent size.
Just do restore cocos2d framework and add your own extension to your project. For example,
#interface CCNode(FactorPositioning)
- (void) setPositionFactor:(CGFloat) factor;
#end
#implementation CCNode(FactorPositioning)
- (void) setPositionFactor:(CGFloat) factor
{
assert(self.parent != nil);
// do your calculations relatieve to the parent's content size
}
#end

How do I resize a b2CircleShape on cocos2d iPhone with Box2D

I have a 2D physics sandbox with a bunch of circles which resize on contact (the bigger one gets bigger, the smaller one gets smaller). I can resize the sprite fine, and I understand that you can't scale a B2Body - you need to destroy it and recreate it, but I'm not familiar enough with Box2D to do it yet.
Here's what I'm doing to resize the sprites:
std::vector<MyContact>::iterator pos;
for(pos = _contactListener->_contacts.begin();
pos != _contactListener->_contacts.end(); ++pos) {
MyContact contact = *pos;
b2Body *bodyA = contact.fixtureA->GetBody();
b2Body *bodyB = contact.fixtureB->GetBody();
if (bodyA->GetUserData() != NULL && bodyB->GetUserData() != NULL) {
PaintBlob *spriteA = (PaintBlob *) bodyA->GetUserData();
PaintBlob *spriteB = (PaintBlob *) bodyB->GetUserData();
NSLog(#"spriteA: %# is touching spriteB: %#", spriteA, spriteB);
if((spriteA.scale > spriteB.scale) && (spriteB.scale > 0)){
spriteA.scale = spriteA.scale + kSCALE_INCREMENT;
spriteB.scale = spriteB.scale - kSCALE_INCREMENT;
}else if (spriteA.scale >0) {
spriteB.scale = spriteB.scale + kSCALE_INCREMENT;
spriteA.scale = spriteA.scale - kSCALE_INCREMENT;
}
}
}
How do I resize (destroy/recreate) the Box2D body (a b2CircleShape?).
I think this is how you do it in C - from emanueleferonato.com (I am not enlightened enough to understand C):
// if I selected a body...
if (body) {
// I know it's a circle, so I am creating a b2CircleShape variable
var circle:b2CircleShape=body.GetShapeList() as b2CircleShape;
// getting the radius..
var r=circle.GetRadius();
// removing the circle shape from the body
body.DestroyShape(circle);
// creating a new circle shape
var circleDef:b2CircleDef;
circleDef = new b2CircleDef();
// calculating new radius
circleDef.radius=r*0.9;
circleDef.density=1.0;
circleDef.friction=0.5;
circleDef.restitution=0.2;
// attach the shape to the body
body.CreateShape(circleDef);
// determine new body mass
body.SetMassFromShapes();
}
return body;
Hi there you handsome devil.
Here's how:
//Radius is worked out by scale * kBLOBDIAMETER /2
contact.fixtureA->GetShape()->m_radius = (spriteA.scale * kBLOBLDIAMETER / 2) /PTM_RATIO;
contact.fixtureB->GetShape()->m_radius = (spriteB.scale * kBLOBLDIAMETER / 2) /PTM_RATIO;
bodyA->ResetMassData();
bodyB->ResetMassData();