@shogun, custom physics bodies are a massive pain unless you use PhysicsEditor.
I wrote a tutorial on how to use PhysicsEditor in case that helps you. Manual polygon creation is a tedious task to say the least.
A fast, easy to use, free, and community supported 2D game engine
@shogun, custom physics bodies are a massive pain unless you use PhysicsEditor.
I wrote a tutorial on how to use PhysicsEditor in case that helps you. Manual polygon creation is a tedious task to say the least.
Hi,
This may be a stupid question but I am trying to set the density of CCBodySprite dynamically but it seems, it's not working.Here is the code for making CCBodySprite:
CCBodySprite *sprite = [CCBodySprite spriteWithFile:@"Disc.png"];
sprite.world = self;
sprite.physicsType = kDynamic;
sprite.collisionType = kBoxCollisionType;
sprite.collidesWithType = kBoxCollisionType | kWallCollisionType;
sprite.position = ccp(location.x, location.y);
sprite.density = 0.0f;
sprite.friction = 0.3f;
sprite.bounce = 0.0f;
[sprite addCircleWithName:@"player" ofRadius:20.0f];
[self addChild:sprite];
playerBox = sprite;
If I am setting the density in the above code it's working fine but if I set the density in the way mentioned below:
playerBox.density = fDensity;
OR
[playerBox setDensity:fDensity forShape:@"player"];
It's not affecting the body.
I am using accelerometer to check the current value of the density and it's displaying the one I am setting dynamically:
- (void)accelerometer:(UIAccelerometer*)accelerometer didAccelerate:(UIAcceleration*)acceleration
{
NSLog(@"player.density: %0.2f",playerBox.density);
static float prevX=0, prevY=0;
float accelX = (float) acceleration.x * kFilterFactor + (1- kFilterFactor)*prevX;
float accelY = (float) acceleration.y * kFilterFactor + (1- kFilterFactor)*prevY;
prevX = accelX;
prevY = accelY;
[playerBox applyForce:CGPointMake(-accelY * pForce.x,accelX * pForce.y)];
}
Please help me with this....
Hi,
This may be a stupid question but I am trying to set the density of CCBodySprite dynamically but it seems, it's not working.Here is the code for making CCBodySprite:
I think this has to do with the following comment in box2D
/// Set the density of this fixture. This will _not_ automatically adjust the mass
/// of the body. You must call b2Body::ResetMassData to update the body's mass.
void SetDensity(float32 density);
Which only happens if _body in the CCBodySprite has been created when you go to change the density with the -(void) setDensity:(float)newDensity function.
Simply change
// if body exists
if (_body)
{
// for each shape in the body
NSArray *shapeNames = [_shapes allKeys];
for (NSString *shapeName in shapeNames)
{
// get the shape
b2Fixture *shape = (b2Fixture *)[[_shapes objectForKey:shapeName] pointerValue];
// set the shape density
shape->SetDensity(_density * PTM_RATIO * PTM_RATIO / GTKG_RATIO);
}
}
to
// if body exists
if (_body)
{
// for each shape in the body
NSArray *shapeNames = [_shapes allKeys];
for (NSString *shapeName in shapeNames)
{
// get the shape
b2Fixture *shape = (b2Fixture *)[[_shapes objectForKey:shapeName] pointerValue];
// set the shape density
shape->SetDensity(_density * PTM_RATIO * PTM_RATIO / GTKG_RATIO);
}
_body->ResetMassData();
}
You should also do this for the -(void) setDensity:(float)newDensity forShape:(NSString *)shapeName function in the same area.
Hope that works.
On a side note I've also made a few changes to the code that you would probably want in your repository. Let me know how it would be easiest to integrate these into your project.
1) First frame rotation fixed.
2) A destruction listener so you can NULL/nil out the revolution/distance joints in the Spring and Motor sprites (it will crash if you don't do this and try and switch levels with a motor or spring sprite)
3) Adding explicit getPosition/getRotation etc to the BodySprite so it will grab the position from the _body object when you request them rather than when that object was updated. (Makes the sprite variables more closely in sync with the box2d object)
4) Added an anchor point for both bodies in the motor sprite
Hi,
I am going through a really tough issue right now.
I am using your wrapper and I want to implement some tricky physics(according to me).
I am using accelerometer to move a body using force but I also want to opposite friction on it.Like in real world if I happen to roll a ball then after some time it stops due friction on it.Box2D friction isn't helping me much.
I am using this formula for calculating friction:
F = μ N
(N = mg)
(m = mass)
(g = 9.8 m/s2)
The issue is I am setting my world's gravity zero and using force to move the body like this:
[playerBox applyForce:CGPointMake(-accelY * pForce.x,accelX * pForce.y)];
But it's not effective.The body continues to move even if I place the device on the flat surface and stops at the corner and I want it to stop as soon as the device is placed on the flat surface.I was thinking of applying opposite force in the opposite direction of the body's movement but I am not getting the point to start with it.
Somebody please help me with this.
Thanks in advance
You can use SetLinearDamping and SetAngularDamping to make a body tend to stop moving or rotating when there is no force or torque on it.
@iforce2d:
Thanks for the reply but I don't want to use damping.I am calculating frictional force and want to apply in the opposite direction of the current movement of the body.Below is my code for calculating friction:
//Considering glass-glass for friction coefficient.
- (void) calculateStaticFriction
{
float frictionCoefficient = 0.9;
float normalForce = 0.0;
float gravity = 9.8;
normalForce = self.fValueMass * gravity;
fValueStaticFriction = frictionCoefficient * normalForce;
}
- (void) calculateDynamicFriction
{
float frictionCoefficient = 0.4;
float normalForce = 0.0;
float gravity = 9.8;
normalForce = self.fValueMass * gravity;
fValueDynamicFriction = frictionCoefficient * normalForce;
}
Here is my code for applying it:
- (void)accelerometer:(UIAccelerometer*)accelerometer didAccelerate:(UIAcceleration*)acceleration
{
static float prevX=0, prevY=0;
float accelX = (float) acceleration.x * kFilterFactor + (1- kFilterFactor)*prevX;
float accelY = (float) acceleration.y * kFilterFactor + (1- kFilterFactor)*prevY;
prevX = accelX;
prevY = accelY;
[playerBox applyForce:CGPointMake(-accelY * pForce.x,accelX * pForce.y)];
NSLog(@"velocity.x: %f and velocity.y: %f", playerBox.body->GetLinearVelocity().x,playerBox.body->GetLinearVelocity().y);
float velX = playerBox.body->GetLinearVelocity().x;
float velY = playerBox.body->GetLinearVelocity().y;
velX = velX * -1;
velY = velY * -1;
NSLog(@"velX: %f and velY: %f", velX,velY);
float divisor = sqrtf((velX * velX) + (velY * velY));
if (divisor != 0.0)
{
velocityX = velX/divisor;
velocityY = velY/divisor;
NSLog(@"divisor: %f", divisor);
NSLog(@"velocityX: %f and velocityY: %f", velocityX,velocityY);
if (velX == 0.0 && velY == 0.0)
{
}
else
{
[playerBox applyForce:CGPointMake((velocityX * fDynamicFriction), (velocityY * fDynamicFriction))];
}
}
}
I am calculating the opposite direction using linear velocity's unit vector and then applying the frictional force in that direction.
But it's not working obviously,body is not able to stop when device held parallel to the surface. once the force is applied the body keeps on moving in that direction until it hits boundary.
I would really appreciate if someone can help me with this.
Thanks
Maybe I'm being stupid, but I haven't been able to get even a simple body added with CCBodySprite without 640 errors happening. I'm getting errors like:
error: expected specifier-qualifier-list before 'b2Body'
I'm able to get it working the "long" way with box2d. I looked at the HelloWorldLayer example and it's not helpful. I think what would be helpful with this project is to have an example that has a bunch of different bodyies. For example, here's a boy with a graphic and a pivot joint, here's a body with gravity etc. They could all be in the same scene. This would especially be helpful to people just getting started with box2d.
@demtriusb
Make sure your .m files are renamed with the .mm extension. This includes anything that touches those classes.
@HoneySharma
Perhaps look at b2Settings.h and change the
inline float32 b2MixFriction(float32 friction1, float32 friction2)
{
return sqrtf(friction1 * friction2);
}
function to something different.
Also check the friction values for all of your bodies. Perhaps try and increase the amount of friction on them and see if it makes a difference, maybe the friction values aren't what you think they are. You could also try and apply the force as an impulse.
You can also change the following to make things sleep (stop moving) faster or slower
/// A body cannot sleep if its linear velocity is above this tolerance.
#define b2_linearSleepTolerance 0.01f
/// A body cannot sleep if its angular velocity is above this tolerance.
#define b2_angularSleepTolerance (2.0f / 180.0f * b2_pi)
Hope that was of some help
.mm was the problem. One of my files was not .mm , that got rid of most of the 640 errors.
Could someone post a simple project on how to use this with a couple of different examples? Joints, bodies etc. I'm missing something because when I try creating bodies using this class I get errors.
Like I said, solid examples of the basics wold save many people alot of headaches.
Add a sprite Like this:
CCBodySprite *sprite = [CCBodySprite spriteWithFile:@"gear256.png"];
sprite.world = self;
sprite.physicsType = kDynamic;
sprite.collisionType = kBoxCollisionType;
sprite.collidesWithType = kBoxCollisionType | kWallCollisionType;
sprite.position = ccp(400, 400);
sprite.density = 1.0f;
sprite.friction = 0.3f;
[self addChild:sprite];
How would you add a motor, spring or a joint to this?
Deleted post ~ silly mistake
You must log in to post.