Sign in to follow this  

(if) value is close to value then execute this line? (C++)

This topic is 3552 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

I want to use 2 variables. 1. Ball's X position 2. Paddle Y Position. So when the ball gets in the correct X axes "AND" and the ball is at say 2.4 X and the paddle is 2.0 Y. How can I say. If Ball Is Around Or Close too Paddle value pose then execute this line of code. So how can I do this in C++?

Share this post


Link to post
Share on other sites
I'd just wrap it in a function (psuedo code):

boolean isWithinDelta(int x, int y, int delta){
if(abs(x - y) <= delta)
return true;
else
return false;
}

eg:

if isWithinDelta(ball.x, paddle.x, 0.4)
doCollision();

Share this post


Link to post
Share on other sites
Quote:
Original post by Peon
I'd just wrap it in a function (psuedo code):

boolean isWithinDelta(int x, int y, int delta){
if(abs(x - y) <= delta)
return true;
else
return false;
}

eg:

if isWithinDelta(ball.x, paddle.x, 0.4)
doCollision();

...
boolean isWithinDelta(int x, int y, int delta){
return abs(x - y) <= delta;
}

Share this post


Link to post
Share on other sites
Quote:
Original post by ajm113
That seems to work,


Really? Because it shouldn't.

boolean isWithinDelta(int x, int y, int delta){


if isWithinDelta(ball.x, paddle.x, 0.4)


Or at very least, you may get unpredictable results.

Share this post


Link to post
Share on other sites
I noticed the ball is jumping around like crazy when it gets close. Ill try the new code. I also changed the int on the function to floats. But it seems no matter the value on delta it seems to mess up on the same place... :(

Share this post


Link to post
Share on other sites
Quote:
Original post by ajm113
I noticed the ball is jumping around like crazy when it gets close. Ill try the new code. I also changed the int on the function to floats. But it seems no matter the value on delta it seems to mess up on the same place... :(


Educated guess: you detect a second collision the frame after you detected the first collision. If you simply invert the x-velocity of your ball on collision you will get uncontrolled jumping.
There are a few ways of dealing with this.
1) when the ball collides you offset your ball to be outside of collision range so it will not collide a second time the frame after.
2) you check in your collision detection routine if the ball is moving towards the paddle or moving a way from it. If it's moving away from said paddle then you do not have a collision.

Share this post


Link to post
Share on other sites
I don't really know what I am doing I spent hours trying to figure this out.

Not trying to be spoon feed here or anything. But it just seems my ball just ignores the paddles no matter how I change it. It is kinda brain twisting I am using what Julian90 function, but it seems it it aether way the ball ignores the paddles a little bit, but still acts strange when say player 1 paddle is moving up or down when the ball is not going to it. Any sources that I can look at or examples?

I did try a if statment when to check when it will alow the Julian90 function to work or not, but the results are just a stuck ball almost even when the paddle leaves or not close to it.

Share this post


Link to post
Share on other sites
Hmm... Perhaps you should post the code that handles your ball - specifically, that which updates its position and velocity, your current collision detection code, and the code that uses that collision detection, and what it does with the results of that test.

Share this post


Link to post
Share on other sites
I'm having a hard time figuring out what the relationship between the paddle's y position and ball's x position has to do with collision detection. I'm assuming you are making a pong clone and that your paddle position is not changing on the x axis. I am also assuming that the ball moves on the x and y axis. This means you're going to need more information in your collision detection than just the paddle y position and ball x position.

If this is true all the examples of collision detection that are shown will treat the paddles Y axis movement as if it were moving on the X axis. This would explain why you would have collision detection with the ball when you are moving up and down and it is no where near the ball's position.

An easy solution would be to use axis-aligned bounding boxes for collision detection.

Share this post


Link to post
Share on other sites
Ok, that makes sense. Though I want to know is how will bounding boxes be generated? Like what defines them as "solid" objects? How do I make that type of bounding box sort of speak? Not a lot of sources on it in short.

[Edited by - ajm113 on March 24, 2008 12:58:33 AM]

Share this post


Link to post
Share on other sites
If you're working in 2D all you need to do is find the maximum and minimum extents of your object in the X and Y direction. Your ball, for example, would have maximum and minimum extents equal to the ball's radius with your AABB being centered around the ball's origin.

More information can be found here.

Share this post


Link to post
Share on other sites

This topic is 3552 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this