开发者

Pong game in c++

开发者 https://www.devze.com 2023-03-29 09:24 出处:网络
Now I\'m making a pong game in c++, and by now I\'ve made a pad开发者_如何学运维dle and ball which both inherits from entity that holds the coordinates and the bounding box(can be circle or polygon) o

Now I'm making a pong game in c++, and by now I've made a pad开发者_如何学运维dle and ball which both inherits from entity that holds the coordinates and the bounding box(can be circle or polygon) of the entity, also I've implement a function entity.isColliding(entity) which easily telling if the entities are colliding for any two entities.

Now I want to make the Ball move and when it will collide with an entity it will reverse the right speed coordinate(x or y). I thought of making a function canMove() which will move and check collision and if there is move back, but I have no idea how to check if I need to reverse the x or the y speed coordinates all I know is that there was or will be a collision.

Any suggestion?


Hm, if I understood your problem correctly... Reversing the right one of the speed coordinates makes your ball resume it's travels the way it should, while reversing the other one makes it just resume to go into the wall (but from the different angle).

I don't know how fast your canMove() function is, but if it's fast enough maybe you could try it this way:
- reverse the x coordinate, if it works, that's the one
- if it doesn't work, then reverse the y coordinate
- if both of these don't work then you're in a special case situation (e.g. corner) -- reverse both :D


What you have to do if you have a complex shape and a collision, you have to find out the tangent at the point of the collision. This helps you calculate the angle of reflection. E.g. if you have rectangles your tangent is vertical or horizontal, and the angle of reflection is easy to calculate: you negate one of your speed coordinates as you already stated. If you have circles, it gets complicated :) I.e. you need some sin and cos.. do you really need that?


If it's a Pong clone, you always reverse the horizontal speed when you collide with the paddle, and always reverse the vertical when you collide with a wall.
To find out whether to reverse the vertical speed when ball and paddle collide, look at what half of the paddle the ball hits.
You're going to need a way to find out the point of collision.

I would suggest that you start off with a square ball and rectangular paddles, like the original, since it makes the collison calculations much easier.

I would also suggest that you get something you can actually play running properly before you start bogging yourself down with a more realistic collision model.
Improving something that works is way more fun than possibly improving something that might work some time in the future.


first work out the velocity of the ball when it hits the paddle or whatever your hitting. Then using trigonometry, the direction of the ball and its x and y coordinates (yay trig!) work out the angle the ball will rebound at. Rejig the balls velocity based on that angle (hint: serious amount of trig... its possible). Its worth noting its all trigonometry and that is works with velocies aswell (not speed).

Im guessing your trying to increase or decrease the speed of the ball like in other pong games? Do the above first then worry about changing the speed based on the angle of collision.

There might be an easier way if you tell me what graphics library your using?

Also with a polygon its going to be harder. best to start simple with a circle or box then refactor your code. When looking for collision detection with different shapes there are alot of theories and ways out there. You need to be a little more specific at what your "polygon" means.

Hope this helps

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号