0
\$\begingroup\$

I'm writing a game, it a pool game. I've read about cocos2dx + physics library, however the document is really poor to understand.

Material has 3 attributes (http://www.cocos2d-x.org/wiki/Physics#) : - Density (?) - Friction (?) - Restitution (scale from 0 to 1)

Execept the restitution has limitation value from 0 to 1. I don't know what's density and the limitation of friction value?

If i want to create a ball and when it collide to the bound/wall, it bounce back, and the ground has friction to slow down the ball during running, how can i do it?

\$\endgroup\$
1
\$\begingroup\$

First of all you need to make sure your Scene is created with physics enabled. You do this by calling auto scene = Scene::createWithPhysics(); where relevant in your code. If not, none of the physics you'll try will work.

Next, you shouldn't pay too much attention to the material right now, what you should probably focus on is the PhysicsBody class and its methods and variables.

For instance, if I were to make a simple ball physics object I'd do something like this : `

auto sprite = Sprite::create("ball.png");
auto physicsBody = PhysicsBody::createCircle(16.0f); // 16 being the radius of the circle
sprite->setPhysicsBody(physicsBody);
addChild(sprite);
physicsBody->setVelocity(Vec2(100, 20));

Repeat this a few times, changing the setVelocity(Vec2(x, y)); as needed, see if you can get a bunch of balls moving around on the screen. I believe they will bump into each other by default. You could also use physicsBody->applyForce(Point(x, y)); or physicsBody->applyImpulse(Point(x, y));.

More information on both can be find in the class documentation here: http://www.cocos2d-x.org/reference/native-cpp/V3.0alpha0/d7/d7b/classcocos2d_1_1_physics_body.html#a06169bfab34c574afd767ebf1e1f7058

\$\endgroup\$
1
\$\begingroup\$

Old thread but I wanted to put my two cents in since I needed something like this on a project I worked on.

You can create a physics body for a sprite with a specified material. This would look something like. groundSprite->setPhysicsBody(PhysicsBody::createBox(boxSize, PhysicsMaterial(0.1f, 0.5f, 0.7f))); where the last parameter is the friction parameter like OP mentioned. Play around with it until your ball comes to a stop where/when you want it to.

You could continuously apply a force or impulse while your ball moves across the table, but I think using the built in physics would be much less work.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.