Hey,Guys:
I just want to make balls bounce back into the screen when balls reach the boundary of screen.So how to simulate the behavior in cocos2d?
How to simulate the ball bounce back
(7 posts) (4 voices)-
Posted 2 years ago #
-
You're using any physic engine for collision detection or planning on doing your own?
Posted 2 years ago # -
if you are doing it by yourself, the simplest way would be to check if the sprite is colliding with the edge (or if the position is greater than the width/height of the screen) and invert the current movement by multiplying the speedvector.x or speedvector.y by -1
Posted 2 years ago # -
I think it's just simple bounce back,no phsysic engine needed,because I don't need the mass and gravity and other things,just simple bounce back.
And solution?Posted 2 years ago # -
how to get the speedvector? is cocos2d support speed vector concept?
Thx for your reply!Posted 2 years ago # -
no, what you would have to do is:
suppose you have an sprite, and each step/update/tick (whatever you call the function that updates de game) you are doing:ball.sprite.position.x += speed.x * dt; (this would make the ball go right)
speed would be a CGPoint, you would make it an instance variable of the class Ball
then when the ball collides, you can do:
speed.x *= -1; to have the ball go left instead
Posted 2 years ago # -
Hi,
How to move the ball into the specific direction? ie, the ball should move through the touch position!
Thanks!
Posted 12 months ago #
Reply
You must log in to post.