Ball.cpp 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. //
  2. // Ball.cpp
  3. // RedCore
  4. //
  5. #include "Ball.h"
  6. #include "params.h"
  7. #include "SimpleAudioEngine.h"
  8. USING_NS_CC;
  9. bool Ball::init() {
  10. auto ball_draw = DrawNode::create();
  11. ball_draw->drawDot(Vec2(0, 0), BALL_SIZE, Color4F(COLOR_grey));
  12. this->addChild(ball_draw);
  13. auto material = PHYSICSBODY_MATERIAL_DEFAULT;
  14. material.density = 0.0f;
  15. material.restitution = 1.005f;
  16. material.friction = 0.0f; //set friction here
  17. auto physicsBody = PhysicsBody::createCircle(BALL_SIZE, material);
  18. physicsBody->setGravityEnable(false);
  19. physicsBody->setVelocity(Vec2(0,0));
  20. physicsBody->setLinearDamping(0.0);
  21. physicsBody->setMass(1000.0f);
  22. physicsBody->setContactTestBitmask(0xFFFFFFFF);
  23. physicsBody->setGroup(-5);
  24. this->addComponent(physicsBody);
  25. this->setTag(BALL_TAG);
  26. return true;
  27. }
  28. void Ball::throwBall () {
  29. float ang = CC_DEGREES_TO_RADIANS(rand_minus1_1() * THROW_ANG);
  30. float x = sinf(ang) * INITIAL_SPEED;
  31. float y = cosf(ang) * INITIAL_SPEED;
  32. this->getPhysicsBody()->setVelocity(Vec2(x, y));
  33. }