This video is 7 years old and it's really really good thanks i started like 2 days ago using MonoGame and i don't have any idea of how to make collisions Thanks :D
@ChickenSwek6 жыл бұрын
I spend a day on figuring out how to get collision to work in my game, this tutorial really helped me! Thanks!
@Oyyou6 жыл бұрын
I'm glad I could help out! :)
@raega3288 жыл бұрын
Making a Transition to MonoGame so all these tutorials are amazing man! Seen some of your XNA stuff too and it led me here, looking forward to any future tutorials you've got on the way :D
@aidenkoorsen68424 жыл бұрын
finally after a month of trying i finally did it thanks so much for this tutorial
@CeeJell3 жыл бұрын
This helped me so much! Thanks mate!
@ugib83775 ай бұрын
This was a really cool video. The only collision I've used thus far is bounding box, so seeing this style is neat. I feel like this style is far less prone to some bugs that bounding box has created for me. This would work much better for something like a side-scrolling platformer I think, where collisions are frequent. I've been using my own Textures, and ran into a problem with the velocity leaving a gap between my boxes. Gonna toy around with making a loop to decrement the velocity until it absolutely cannot move another pixel. First time I've even seen the #region/#endregion in C#. But I haven't been using the language for more than a few weeks, so meh.. Time to go google that shit.
@Oyyou5 ай бұрын
I had to quickly skim through to double check what collision was used here aha! Yeah - I quite liked checked the sides with the velocity included. It provided one of the better results from others that I played with Typically collision detection and response is such a pain, so it's always good to know what your options are SAT is always a fun read: dyn4j.org/2010/01/sat/ There used to be a really cool website for it - but I guess that doesn't exist anymore
@ugib83775 ай бұрын
@@Oyyou Nice dude, thanks for the link. I'll def. read that through. In the meanwhile, I fixed the gap bug on the program (for my textures at least) - Replacing the Velocity.X = 0 line on the Player.cs file. if (this.Velocity.X > 0) { for (int i = (int)this.Velocity.X; i >= 0; i--) { this.Velocity.X = i; if (this.IsTouchingLeft(sprite)) { continue; } else if (!this.IsTouchingLeft(sprite)) { break; } } } else if (this.Velocity.X < 0) { for (int i = (int)this.Velocity.X; i
@Oyyou5 ай бұрын
@@ugib8377 Amazing! It's nice to see you're being proactive and figuring out things like that :)
@droidilate7 жыл бұрын
Thank you for the fun tutorials. Question: Could you not use a Rectangle.Intersects instead of all those long position tests for collision detection? If it the rects don't intersect, then no need to test which side. Maybe your way is more efficient?
@Oyyou7 жыл бұрын
I'll be honest, I can't 100% remember why I stopped using "Intersects", but I think it was because you still need to know which side you hitting, so you can determine which direction we need to move after hitting! :)
@droidilate7 жыл бұрын
Well, I tried it and it worked, but its a bit tedious. It would be a little awkward as a tutorial. I didn't test to see if it's more performant, but I'd be surprised if it was. You have to create a "proposed" rectangle to test and you have to isolate the horizontal components. *shrug* It's too big to put it all in here, but this is the salient part (sorry about the formatting): public void Update(GameTime gameTime, List sprites){ var kb = Keyboard.GetState(); //Velocity will be the h and v speeds after collision detection Vector2 velocity = new Vector2(0,0); //L-R if (kb.IsKeyDown(this.Left)){ velocity.X = -this.Speed; } else if (kb.IsKeyDown(this.Right)){ velocity.X = this.Speed; } //U-D if (kb.IsKeyDown(this.Up)){ velocity.Y = -this.Speed; } else if (kb.IsKeyDown(this.Down)){ velocity.Y = this.Speed; } //Zero out velocity if we're hitting any other collidable sprites (any of them right now) for (int i=0; i
@kobraking64505 жыл бұрын
what if the sprite is rotated, how would you test the collision
@alicadus5 жыл бұрын
well my game is in monogame but in 3d and its a camera, how do i make a collision detection for that
@KenshirouLuke7 ай бұрын
Hello, thanks! I have one question: my entity is few pixel far from the other, the check will prevent the collision and stop my entity, but this will result in stopping it before it will get adjacent to the other. How can I resolve this case?
@ugib83775 ай бұрын
It's two months later, so I'm not sure if you've solved this yet. I posted a fix for it in my comment on this video. Sort comments by most recent. I used a for loop to decrement the velocity until it fits pixel perfect. I had the same problem you did, heh.
@Lexyvil6 жыл бұрын
This is where I am at when coding for my game. Making collision work from any four side and been stuck on that for days. Thank you! Though what is the #region for?
@ThatGuy-qv1uu5 жыл бұрын
I know you asked a long time ago and probably figured it out, but #region is just for organizing your code because you can collapse it down.
@nooitniets4 жыл бұрын
@@ThatGuy-qv1uu ahh thank you man!
@piggen51542 жыл бұрын
good video
@puppymaltese13 жыл бұрын
It helped me a lot thx 💖
@xornedge82042 жыл бұрын
Now I know why most of this stuff is drag and drop nowadays. This stuff is a painnnnn
@soundrogue44723 жыл бұрын
Okay how do you get this to work with camera follow?
@j0of Жыл бұрын
i'd like to add that, if you're extremely lazy like me, you can just store the initial position at the beginning of each frame, then reset the position to that if an overlap is detected! something like this: Vector2 position; Rectangle bounds; Rectangle obstacle; public void Update(GameTime gameTime) { Vector2 prevPos = position; bounds.X = (int)position.X; bounds.Y = (int)position.Y; if (bounds.Intersects(obstacle) { position = prevPos; } }
@c9bd4158 жыл бұрын
How come u didn't use rectangle.intersects?
@Oyyou8 жыл бұрын
+iG Cloud because doing the 4 detection checks wouldn't work in the same way. I can't remember off the top of my head, but you can replace the TouchsLeft and such and see if it makes a difference (:
@levy37726 жыл бұрын
Because when you move the character, you'll move its position without checking if it can go there, so if you move 100 pixels, you would intersect because you wont see that you've intersects until you are in the other rectangle
@LSparkzwz6 жыл бұрын
I guess you could have a copy of the rectangle and make it 1f larger and use that to check the intersections, so the real hitbox touches but never intersects
@markuspersson54598 жыл бұрын
Thanks alot for getting back at us with your awesome new tutorial series! Just a quick question here: Why exactly do you always create the Rectangle with a getter like this: public Rectangle Rectangle { get { return new Rectangle((int)Position.X, (int)Position.Y, _texture.Width, _texture.Height); } } Instead of simply creating the Rectangle in the beginning with public Rectangle Rectangle; and then initializing it in the constructor like this: Rectangle = new Rectangle((int)Position.X, (int)Position.Y, _texture.Width, _texture.Height); Would very much appreciate an answer to this!
@Oyyou8 жыл бұрын
We want the rectangle to move with the position. If we do your suggestion, the rectangle will only be set to the position at the point of being initialized. By putting it in the "get", the rectangle is updated to be in the location of "Position" every time somebody uses the Rectangle. It's kinda like having it in the Update method, except more contained.
@soundrogue44723 жыл бұрын
Bookmark for myself 9:25
@alicadus6 жыл бұрын
i have two problems. 1st i cant stop my player sprite from moving beyond the boundaries of the screen. 2nd, i want the ball sprite to bounce when it hits the player sprite. help?
@Ryo-jn7xi6 жыл бұрын
1st : Clamp the Position to the boundaries of the screen or check with "if"s its position if(sprite.Position.X = screenWidth)
@mizuni8 жыл бұрын
Hello, just wanted to ask why you put the if(sprite==this) continue; in the Player class (Update)? ^_^
@Oyyou8 жыл бұрын
Because a player doesn't want to check collision with itself!
@Jimaniki5 жыл бұрын
Thanks a lot !
@jaredchong40705 жыл бұрын
Can you make a video on how to build a geometric primitive with collision detection with it. I wanna build a physics engine but new to these kind of things.
@Oyyou5 жыл бұрын
If that made sense to me, then maybe. Unfortunately physics isn't my strong point (:
@jaredchong40705 жыл бұрын
@@Oyyou you dont need to learn the whole physics ,just how to make two polygonal shapes collide.
@bluepianist24975 жыл бұрын
i can't download -_- :(
@jarelcorpuz14398 жыл бұрын
Will this work with the platforms?
@Oyyou8 жыл бұрын
Yes, that was the intention of the code.
@jarelcorpuz14398 жыл бұрын
THANK YOU SO MUCH. am doing a project right now and it works :D subbed
@Oyyou8 жыл бұрын
Glad I could help :)
@lolipopckajj85706 ай бұрын
Брат ты шо неформал?
@scorpioga3t68 жыл бұрын
i don't know why yt autosub me ... any way hello from italy :)
@Oyyou8 жыл бұрын
+SCORPIOGA3T ! Because yt knows what you want? Hello, Italy! :D
@scorpioga3t68 жыл бұрын
wwwwoooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo. ok man get it.... get it ...
@MySongs8888 жыл бұрын
Subcribe you from 2014 when i 'm learning XNA,. Now i'm Unity Game Developer, why did you don't try Unity? It's power ful and free for all. :D
@Oyyou8 жыл бұрын
+C James I come and go from Unity. I just like all the programming you do on XNA/MonoGame. Grats on becoming a game dev (:
@MySongs8888 жыл бұрын
Thanks you, hope you will be have many awesome game :D
@diverseprogrammer185 жыл бұрын
Not all people use Unity3D at some point like I do. I'm not saying it's bad, it's just we have our own choice.
@jnelson167014 жыл бұрын
Coding a game is sooooo much easier using Gamemaker. A lot of work had to be done here to do just a simple task. And this is just for rectangles. I can only imagine the kind of code you would have to write if you were using more complicated shapes, like a spaceship or saucer shaped sprite.
@ugib83775 ай бұрын
Engines like Gamemaker or anything else come with a lot of easy fixes for things (Which is why a lot of people use them), but these engines also have rules you have to abide by, or work very hard to break in scripts. Engines also come with a lot of bloat. Making a simple sprite to do one thing, that sprite might come with a ton of fields and information the engine assigns it, when you only needed it to do X task. Frameworks like Monogame, or Pygame take care of stuff like rendering, input handling, and the basis for collision detection for you, while allowing you to create your own inner workings. It's not for everyone, but some people prefer to work that way. You can also load in Libraries for stuff like physics, or collision even. Or create your own. Which can take some of the excessive code away, but once again. You're operating within someone else's guidelines, similar to an engine.