Ball.cpp 1.1 KB

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