Public Group

# Okay, I'm finally defeated (help!?)

This topic is 4893 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

##### Share on other sites
nevermind, got it.

Quote:

Fixed.

##### Share on other sites
At the moment you seem to be checking for an exact positional match with the paddle, which surely is unlikely to occur since there are thousands of possible positions for both ball and paddle. I think you will need a more complex check that takes into account the size of the paddle (and maybe the size of the ball).

##### Share on other sites
Ok I'm not gonna change your code and show it to you. Thats for your benefit. I'm going to make recomendation that mean you will potentially have to rewrite a signifigant portion of your code. Your problem lie in how your trying to represent you ball and its movement.

If you dont know what the term vector means(a geometrical vector not the std:: kind) take a few minutes to look up 2d vector math. you might want to read it anyways. If you dont get it, or cant find good info. I'd be glad to help you get started with atleast anough to make you pong game. Once you've read and understand it...

[edit: you might want to check out this thread in regards to vectors: http://www.gamedev.net/community/forums/topic.asp?topic_id=318048 ]

You need to represent your ball differently. something like this:

vector2f.h is a 2d vector class I wrote for doing stuff just like your doing. I'm not the best programmer and Im alot better now than I was when I wrote that.
No guaruntees and you really should write your own.
should be fine for pong though. you can download it here: vector2f.h
#include "vector2f.h"struct pong_ball{    vector2f position;    vector2f direction;    float speed;}

Now you have a ball, To make the ball move you need to give it a direction and speed. So lets set the init direction to up-right at a 45 degree angle. do that by setting the x direction to one and the y direction to one.

pong_ball pb;//set initial position speed and direction.pb.position.x = SCREEN_WIDTH /2;pb.position.y = SCREEN_HEIGHT /2; //set these to the middle of screen or wherever you want.pb.direction.x = 1.0f; // these should be numbers between -1 and 1pb.direction.y = 1.0f;// Now we need to give it a speed. I wont go into making things go the same speed on all computers but once you get this working that will be the next step.pb.speed = 0.5; //start low and find a value that wil work for now.//Noweach frame in you main loop you want to do this:pb.position = pb.direction * pb.speed;// then draw you pong ball at position.x, position.y you compiler will probably give you a warning about conversion from float to int or long, but this is ok.//Now when you collide instead of changing speed like you did before, change //direction. to bounce off the top and bottom for example:if(pb.position.y <= 0.0f)  pb.direction.y = fabs(pb.position.y) * -1; //bounce off topif(pb.position.y >= SCREEN_HEIGHT)  pb.direction.y = fabs(pb.position.y; //bounce off bottom.

Once you have this working you can easily check to see if it colliding with the paddle. An example way to do this would be:

you need to know two point on the paddle the top closest edge from the ball and the bottom closest egde. then you nee to know the center position of the ball. All of these should be in vector2f's. you will also need to know the radius of the ball.

then do this.

if( pb.position.y > paddle.bottom.y && pb.position.y < paddle.top.y) //first check if the ball and paddle are with in the same rows
{
vector2f difference = pb.position - collision_temp;
difference.vabs(); //make it positive;
float len = sqrtf(difference.x * difference.x + difference.y * difference.y); // get the length
if(len < radius) // Then they collided!
}

//Thats a quick sloppy example. But it shoudld help you get started.

Using this representation makes alot of things easier later. If you need help on any of this or more clear explanation, ill be around most of the day and I'd be glad to help.

[Edited by - chad_420 on May 24, 2005 6:36:57 AM]

##### Share on other sites
look at your code for the ball an paddle and think more about what your actually trying to do.

assuming 0,0 to be at the top left of the screen.

if (ball.x < player.x+player.width){   if (((ball.y+ball.height)>player.y) && (ball.y < (player.y+player.height)))   {       ball.x.speed*=-1;   }}

something like that anyway.

j

##### Share on other sites
I'm going to go for (pretty much) a complete rewrite, starting tonight.

1. 1
2. 2
3. 3
Rutin
15
4. 4
5. 5

• 14
• 9
• 9
• 9
• 10
• ### Forum Statistics

• Total Topics
632912
• Total Posts
3009197
• ### Who's Online (See full list)

There are no registered users currently online

×