Jump to content
  • Advertisement
Sign in to follow this  
ukdeveloper

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

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, Sorry to say that Pong clone of mine has finally driven me up the wall, after 5 days. The ball bounces fine off the walls like it should, and behaves correctly in that respect. It won't bounce off the paddles properly, though. At the current time, it doesn't at all. Sometimes when I change things, it bounces in a straight line back the way, and bounces off an invisible line when the paddle is moved down. I've had enough, I can't fix it. I was wondering, can somebody pretty pretty please take a look and try and fix it for me? I've tried virtually everything, including everything that people have told me in my 4 or so Gamedev threads about this one particular problem Here's the latest build, everything's there that you need: Clicky Please? I'd be eternally grateful, I just want this to work properly as my first non text based game. Once I've seen your changes and understood them, I shouldn't need any more help. The problem I'm having is so simple, yet it's about to put me off game development for ever because it simply won't fix. The code and variable names shouldn't be too hard to follow for you. If you have any problems understanding my code, please ask. Thanks for your time, ukdeveloper.

Share this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 1
pb.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 top

if(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 collision_temp(paddle.top.x, pb.position.y);
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 this post


Link to post
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 this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!