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\$
3
  • \$\begingroup\$ Please post your code. \$\endgroup\$ Commented Dec 9, 2016 at 6:14
  • \$\begingroup\$ I had added my code .plz help me. \$\endgroup\$ Commented Dec 9, 2016 at 6:33
  • 2
    \$\begingroup\$ I really don't like auto for int's, i can't help it. \$\endgroup\$ Commented Dec 9, 2016 at 9:19

1 Answer 1

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

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.