1
\$\begingroup\$

I had set angle but it's not correct and I'm not getting exactly what I needed. The ball should bounce when hit to paddle or border and move in opposite direction. I'm writing the code in c++.

I had presently written this code when the ball and paddle intersects and I couldn't get how the code is working.

auto paddleY=paddleSpr->getPositionY();
auto intersectY=ballSpr->getPositionY();

auto paddleheight=65;
auto maxBounceAngle=45;
auto speed =1550;

auto relativeIntersectY=(paddleY+(paddleheight/2))-intersectY;


auto normalizedRelativeIntersectY=(relativeIntersectY/(paddleheight/2));

auto  bounceAngle=normalizedRelativeIntersectY*maxBounceAngle;

float vx = cos(bounceAngle * M_PI / 180) * speed;
float vy = sin(bounceAngle * M_PI / 180) * speed;

pbody11->setVelocity(Vect(vx,vy));
\$\endgroup\$
  • \$\begingroup\$ Please post your code. \$\endgroup\$ – SurvivalMachine Dec 9 '16 at 6:14
  • \$\begingroup\$ I had added my code .plz help me. \$\endgroup\$ – sarada duddela Dec 9 '16 at 6:33
  • 2
    \$\begingroup\$ I really don't like auto for int's, i can't help it. \$\endgroup\$ – Julian Declercq Dec 9 '16 at 9:19
1
\$\begingroup\$

in my opinion , setting speed to negative should fix the problem ( which negative is opposite with positive ) :

auto paddleheight=65;
auto maxBounceAngle=45;
auto speed=-1550;

auto relativeIntersectY=(paddleY+(paddleheight/2))-intersectY;


auto normalizedRelativeIntersectY=(relativeIntersectY/(paddleheight/2));

auto  bounceAngle=normalizedRelativeIntersectY*maxBounceAngle;

float vx = cos(bounceAngle * M_PI / 180) * speed;
float vy = sin(bounceAngle * M_PI / 180) * speed;

pbody11->setVelocity(Vect(vx,vy));

UNTESTED , because it is not possible without full project file .

\$\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.