Ball.cpp 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. this->addComponent(physicsBody);
  27. this->setTag(BALL_TAG);
  28. return true;
  29. }
  30. void Ball::throwBall () {
  31. float ang = CC_DEGREES_TO_RADIANS(rand_minus1_1() * THROW_ANG);
  32. float x = sinf(ang) * INITIAL_SPEED;
  33. float y = cosf(ang) * INITIAL_SPEED;
  34. this->getPhysicsBody()->setVelocity(Vec2(x, y));
  35. }