Ball.cpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. // on "init" you need to initialize your instance
  13. bool Ball::init() {
  14. auto ball_draw = DrawNode::create();
  15. ball_draw->drawDot(Vec2(0, 0), BALL_SIZE, Color4F(COLOR_grey));
  16. this->addChild(ball_draw);
  17. // ParticleSun* m_emitter = ParticleSun::create();
  18. // m_emitter->setPosition(Vec2(0, 0));
  19. // this->addChild(m_emitter);
  20. //
  21. auto material = PHYSICSBODY_MATERIAL_DEFAULT;
  22. material.density = 0.0f;
  23. material.restitution = 1.005f;
  24. material.friction = 0.0f; //set friction here
  25. auto physicsBody = PhysicsBody::createCircle(BALL_SIZE, material);
  26. physicsBody->setGravityEnable(false);
  27. physicsBody->setVelocity(Vec2(0,0));
  28. physicsBody->setLinearDamping(0.0);
  29. //physicsBody->setRotationEnable(false);
  30. physicsBody->setMass(1000.0f);
  31. physicsBody->setContactTestBitmask(0xFFFFFFFF);
  32. this->addComponent(physicsBody);
  33. this->setTag(BALL_TAG);
  34. return true;
  35. }
  36. void Ball::throwBall () {
  37. float ang = CC_DEGREES_TO_RADIANS(rand_minus1_1() * THROW_ANG);
  38. float x = sinf(ang) * INITIAL_SPEED;
  39. float y = cosf(ang) * INITIAL_SPEED;
  40. this->getPhysicsBody()->setVelocity(Vec2(x, y));
  41. }