❔ looking to find smneto help me understand offsets via Rects/collission (also help w c# in general)

hello so im looking for some help, to help me with learning offsets for a rect via collission someone that i know has been helping me here n there when he has time, and we started off by me learning how to make pong in c# via monogame. Keep in m,ind i have 0 coding background, and allthough i suck at math...this has been super fun so far...at least as long as im able to understand everything going on haha... Im eager to be able to start actually doing some cool stuff with pong and possibly use that to create other arcade style games...its just sometimes i just wanna rip my hair out...as bad as that sounds only being pong..but its something im super eager about learning, its just super frustrating at times though
35 Replies
JakenVeina
JakenVeina2y ago
to be completely honest, my recommendation is to not jump into game development with no programming experience at all
marine2oorah4u
marine2oorah4uOP2y ago
Ive gotten up to the point where you start adding collissions What i am confident in knowing, is theres 4 points, or this
JakenVeina
JakenVeina2y ago
not game development in something like monogame, anyway
marine2oorah4u
marine2oorah4uOP2y ago
its fine, its super interesting and i think im doing good at it so far
marine2oorah4u
marine2oorah4uOP2y ago
marine2oorah4u
marine2oorah4uOP2y ago
the blue square in the center i just tried to add on my own via experiemnting but the other stuff i was able to do with for the most part understanding of how everything works but anyways 1sec
marine2oorah4u
marine2oorah4uOP2y ago
marine2oorah4u
marine2oorah4uOP2y ago
marine2oorah4u
marine2oorah4uOP2y ago
this is the part that im on atm
marine2oorah4u
marine2oorah4uOP2y ago
i havent dove too much into this, because ive been busy the last couple days/tired when i am home but this is the part that im currently on
JakenVeina
JakenVeina2y ago
what is your question?
marine2oorah4u
marine2oorah4uOP2y ago
those were what i asked
marine2oorah4u
marine2oorah4uOP2y ago
uh i guess not rn bcs im about to go to bed but the part im looking for help on is taking what craig said, and i gues help on breaking it down even further, or maybe not breaking it down, but help with understanding each part more/better which i imagine that once i get collissions down to a point, that the rest of pong will become super easy being the rest of pong would be basically the rest of the logic, such as keeping score, knowing when the ball scores, etc and then probably menu stuff and/or levels?
JakenVeina
JakenVeina2y ago
yeah, I'm lying in bed on my phone right now. Not only do I not have much interest in reading through an entire other thread, those screenshots are illegible. You will get more out of help in a forum like this the more you put into it. If you can't distill your problem down to a couple sentences of a question that we can at least start with, it's a sign thay maybe you haven't thought it through for yourself enough first.
marine2oorah4u
marine2oorah4uOP2y ago
if you dont have interest in helping, then why are you in the thread then? no offense
JakenVeina
JakenVeina2y ago
I didn't say I have no interest in helping at the moment, I don't really know what you want help WITH something to do with collision
marine2oorah4u
marine2oorah4uOP2y ago
learning collission, or 1sec
JakenVeina
JakenVeina2y ago
and the screenshots are illegible
marine2oorah4u
marine2oorah4uOP2y ago
"Examine this diagram. 4 balls, numbered 1 to 4 (in blue) and the black paddle.
if (ballRect.X <= player1Rect.X + player1Rect.Width && // point 1 X <= point 2 X
ballRect.Y <= player1Rect.Y + player1Rect.Height && // point 1 Y <= point 3 Y
ballRect.Y + ballRect.Height >= player1Rect.Y) // point 3 Y <= point 2 Y
{
ballVelocityX = -ballVelocityX;
}
if (ballRect.X <= player1Rect.X + player1Rect.Width && // point 1 X <= point 2 X
ballRect.Y <= player1Rect.Y + player1Rect.Height && // point 1 Y <= point 3 Y
ballRect.Y + ballRect.Height >= player1Rect.Y) // point 3 Y <= point 2 Y
{
ballVelocityX = -ballVelocityX;
}
The first line, as the comment says, is testing if the X of point 1/3 of the ball (the left edge) is <= (less than or equal to) the X of point 2/4 of the paddle (the right edge). Because the ball is moving to the left, we are testing if the movement of the ball for this frame (it's velocity) has caused it to move from the right hand side of the paddle right edge to either directly on its right edge or to the left of it. If this is true, then the ball has potentially collided with the paddle on this frame. So now we have to also test the Y positions to check if it has actually hit the paddle, or if it has just moved past it like ball 4 will do. So the second line of that if statement is testing if the Y of point 1 of the ball is <= (less than or equal to) the Y of point 4 of the paddle. Balls 1, 2 and 3 all meet this condition. Their point 1 Y is <= the paddles point 4 Y. Because ball 4 fails that test, we know it cannot hit the paddle, so it doesn't even bother with the next test. So if Y of point 1 of the ball is <= point 4 of the paddle, we know it is still possible the ball could have collided with the paddle, so we now do line 3 of that if statement. Line 3 is testing if the Y of point 3 of the ball is >= (greater than or equal to) the Y of point 2 of the paddle. If the Y is < then we know the ball is above the paddle and has not collided. You will see that the Y of point 3 for all the balls is greater than the Y of point 2 of the paddle, so they will all hit the paddle except ball 4 which we stopped testing for in line 2 (it failed so the if statement aborts and the velocity is not negated). And that is why line 2 and 3 of that if statement are exactly the same for the other paddle. The Y tests don't change, because we are only testing the left/right (horizontal) edges. If we were to also test the top and bottom edges, which is necessary for a robust collision, then we would also need to change the Y tests depending which edge we are testing."
marine2oorah4u
marine2oorah4uOP2y ago
marine2oorah4u
marine2oorah4uOP2y ago
not rn but at one point, i want to be able to take what my friend says here in this (his post) and to take this code
if (ballRect.X <= player1Rect.X + player1Rect.Width && // point 1 X <= point 2 X
ballRect.Y <= player1Rect.Y + player1Rect.Height && // point 1 Y <= point 3 Y
ballRect.Y + ballRect.Height >= player1Rect.Y) // point 3 Y <= point 2 Y
{
ballVelocityX = -ballVelocityX;
}
if (ballRect.X <= player1Rect.X + player1Rect.Width && // point 1 X <= point 2 X
ballRect.Y <= player1Rect.Y + player1Rect.Height && // point 1 Y <= point 3 Y
ballRect.Y + ballRect.Height >= player1Rect.Y) // point 3 Y <= point 2 Y
{
ballVelocityX = -ballVelocityX;
}
and to break each line/condition down or to understand each outcome/scenario better or just overall an easier understanding but again dont worry about doing it right now that's just what id overal would love help on/with
JakenVeina
JakenVeina2y ago
what are X and Y in reference to these objects the upper-left corner?
marine2oorah4u
marine2oorah4uOP2y ago
x,y is point 1 or here
marine2oorah4u
marine2oorah4uOP2y ago
JakenVeina
JakenVeina2y ago
the beauty of text communication like this is it's asynchronous. You can leave and come back later.
marine2oorah4u
marine2oorah4uOP2y ago
i know im just saying dont worry about diving deep into this
JakenVeina
JakenVeina2y ago
and I'm saying that's kinda silly
marine2oorah4u
marine2oorah4uOP2y ago
tbh you replied to my post almost instantly, so i was quite supprised i got someone to respond or had any interest so suddenly lol which im appreciative of
JakenVeina
JakenVeina2y ago
I'll look at it now and ask a few questions now because I'm available now. You respond at your leisure
marine2oorah4u
marine2oorah4uOP2y ago
ok
JakenVeina
JakenVeina2y ago
so, assuming positive x is right, and positive y is down ballRect.X <= player1Rect.X + player1Rect.Width the left edge of the ball is to the left of, or overlapping with, the right edge of player1 ballRect.Y <= player1Rect.Y + player1Rect.Height the top edge of the ball is above or overlapping with the bottom edge of player1 ballRect.Y + ballRect.Height >= player1Rect.Y the bottom edge of the ball is below or overlapping with the top edge of player1 ballVelocityX = -ballVelocityX; Mirror the velocity of the ball in the horizontal direction
TheRanger
TheRanger2y ago
Rectangle contains a method called Intersect no need to write some logic when the framework already defined one for you
JakenVeina
JakenVeina2y ago
excellent point assuming those are values of type Rectangle and it's still a good idea to have a grasp on how built-in stuff works, under the hood
TheRanger
TheRanger2y ago
they are, they posted their code in #game-dev before
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server