MonoGame Tutorial 009 - Sprite Collision Detection and Response

  Рет қаралды 32,938

Oyyou

Oyyou

Күн бұрын

Пікірлер: 57
@artempest7137
@artempest7137 5 ай бұрын
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
@ChickenSwek
@ChickenSwek 6 жыл бұрын
I spend a day on figuring out how to get collision to work in my game, this tutorial really helped me! Thanks!
@Oyyou
@Oyyou 6 жыл бұрын
I'm glad I could help out! :)
@raega328
@raega328 8 жыл бұрын
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
@aidenkoorsen6842
@aidenkoorsen6842 4 жыл бұрын
finally after a month of trying i finally did it thanks so much for this tutorial
@CeeJell
@CeeJell 3 жыл бұрын
This helped me so much! Thanks mate!
@ugib8377
@ugib8377 5 ай бұрын
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.
@Oyyou
@Oyyou 5 ай бұрын
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
@ugib8377
@ugib8377 5 ай бұрын
@@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
@Oyyou
@Oyyou 5 ай бұрын
@@ugib8377 Amazing! It's nice to see you're being proactive and figuring out things like that :)
@droidilate
@droidilate 7 жыл бұрын
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?
@Oyyou
@Oyyou 7 жыл бұрын
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! :)
@droidilate
@droidilate 7 жыл бұрын
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
@kobraking6450
@kobraking6450 5 жыл бұрын
what if the sprite is rotated, how would you test the collision
@alicadus
@alicadus 5 жыл бұрын
well my game is in monogame but in 3d and its a camera, how do i make a collision detection for that
@KenshirouLuke
@KenshirouLuke 7 ай бұрын
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?
@ugib8377
@ugib8377 5 ай бұрын
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.
@Lexyvil
@Lexyvil 6 жыл бұрын
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-qv1uu
@ThatGuy-qv1uu 5 жыл бұрын
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.
@nooitniets
@nooitniets 4 жыл бұрын
@@ThatGuy-qv1uu ahh thank you man!
@piggen5154
@piggen5154 2 жыл бұрын
good video
@puppymaltese1
@puppymaltese1 3 жыл бұрын
It helped me a lot thx 💖
@xornedge8204
@xornedge8204 2 жыл бұрын
Now I know why most of this stuff is drag and drop nowadays. This stuff is a painnnnn
@soundrogue4472
@soundrogue4472 3 жыл бұрын
Okay how do you get this to work with camera follow?
@j0of
@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; } }
@c9bd415
@c9bd415 8 жыл бұрын
How come u didn't use rectangle.intersects?
@Oyyou
@Oyyou 8 жыл бұрын
+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 (:
@levy3772
@levy3772 6 жыл бұрын
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
@LSparkzwz
@LSparkzwz 6 жыл бұрын
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
@markuspersson5459
@markuspersson5459 8 жыл бұрын
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!
@Oyyou
@Oyyou 8 жыл бұрын
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.
@soundrogue4472
@soundrogue4472 3 жыл бұрын
Bookmark for myself 9:25
@alicadus
@alicadus 6 жыл бұрын
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-jn7xi
@Ryo-jn7xi 6 жыл бұрын
1st : Clamp the Position to the boundaries of the screen or check with "if"s its position if(sprite.Position.X = screenWidth)
@mizuni
@mizuni 8 жыл бұрын
Hello, just wanted to ask why you put the if(sprite==this) continue; in the Player class (Update)? ^_^
@Oyyou
@Oyyou 8 жыл бұрын
Because a player doesn't want to check collision with itself!
@Jimaniki
@Jimaniki 5 жыл бұрын
Thanks a lot !
@jaredchong4070
@jaredchong4070 5 жыл бұрын
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.
@Oyyou
@Oyyou 5 жыл бұрын
If that made sense to me, then maybe. Unfortunately physics isn't my strong point (:
@jaredchong4070
@jaredchong4070 5 жыл бұрын
@@Oyyou you dont need to learn the whole physics ,just how to make two polygonal shapes collide.
@bluepianist2497
@bluepianist2497 5 жыл бұрын
i can't download -_- :(
@jarelcorpuz1439
@jarelcorpuz1439 8 жыл бұрын
Will this work with the platforms?
@Oyyou
@Oyyou 8 жыл бұрын
Yes, that was the intention of the code.
@jarelcorpuz1439
@jarelcorpuz1439 8 жыл бұрын
THANK YOU SO MUCH. am doing a project right now and it works :D subbed
@Oyyou
@Oyyou 8 жыл бұрын
Glad I could help :)
@lolipopckajj8570
@lolipopckajj8570 6 ай бұрын
Брат ты шо неформал?
@scorpioga3t6
@scorpioga3t6 8 жыл бұрын
i don't know why yt autosub me ... any way hello from italy :)
@Oyyou
@Oyyou 8 жыл бұрын
+SCORPIOGA3T ! Because yt knows what you want? Hello, Italy! :D
@scorpioga3t6
@scorpioga3t6 8 жыл бұрын
wwwwoooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo. ok man get it.... get it ...
@MySongs888
@MySongs888 8 жыл бұрын
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
@Oyyou
@Oyyou 8 жыл бұрын
+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 (:
@MySongs888
@MySongs888 8 жыл бұрын
Thanks you, hope you will be have many awesome game :D
@diverseprogrammer18
@diverseprogrammer18 5 жыл бұрын
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.
@jnelson16701
@jnelson16701 4 жыл бұрын
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.
@ugib8377
@ugib8377 5 ай бұрын
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.
MonoGame Tutorial 010 - 2-Player Pong Game!
26:25
Oyyou
Рет қаралды 12 М.
MonoGame Tutorial 013 - Game States (Main Menu)
13:52
Oyyou
Рет қаралды 28 М.
Lamborghini vs Smoke 😱
00:38
Topper Guild
Рет қаралды 65 МЛН
Что-что Мурсдей говорит? 💭 #симбочка #симба #мурсдей
00:19
Симбочка Пимпочка
Рет қаралды 4,7 МЛН
Chain Game Strong ⛓️
00:21
Anwar Jibawi
Рет қаралды 27 МЛН
Мясо вегана? 🧐 @Whatthefshow
01:01
История одного вокалиста
Рет қаралды 7 МЛН
Getting Started With MonoGame
34:18
World of Zero
Рет қаралды 89 М.
MonoGame Tutorial 006 - Sprite Shooting Bullets
18:26
Oyyou
Рет қаралды 19 М.
MonoGame Tutorial 014 - Camera Following Sprite
9:12
Oyyou
Рет қаралды 20 М.
MonoGame Tutorial 007 - Sprite Death and Respawn
22:56
Oyyou
Рет қаралды 10 М.
MonoGame Tutorial 011 - Sprite Animation
15:50
Oyyou
Рет қаралды 29 М.
MonoGame Tutorial 002 - Moving a Sprite
7:25
Oyyou
Рет қаралды 20 М.
MonoGame Tutorial 001 - Drawing a Sprite
10:00
Oyyou
Рет қаралды 51 М.
Lamborghini vs Smoke 😱
00:38
Topper Guild
Рет қаралды 65 МЛН