• Advertisement
  • Popular Tags

  • Popular Now

  • Advertisement
  • Similar Content

    • By Herwald
      Hey, everyone! Will cut right to the chase:
      I'm a freelance illustrator with a bachelor's degree in graphic design, have done 2D animation work in the past and some illustration work for a few mobile games. However, my understanding of how an actual video game is made - especially, how to work with programmers - is very limited. That being said, there are Nintendo/Sega Genesis games from my youth that I'd love to, essentially, reskin based on my own designs and have them be playable on mobile devices with a more current day look and sound (reskin's maybe too strong of a word).
      One such game is "Evander Holyfield's Real Deal Boxing" (the old Sega Genesis game*). I'd like for the gameplay, the mechanics (the way the jabs, hooks, uppercuts, etc. are thrown), the fighter selection and the career mode to be pretty much identical to how EHRDB is, but - instead of realistic boxing - it would be a sci-fi themed game starring different monsters & creatures boxing each other. I'd like for it to be a 2D/2.5D game and I'd do all of the character designs & animations myself.
      It's a fairly simple game by today's standards, but, judging by what I've read & heard - the Achilles heel in most game development is always the coding, the programming.  Mostly because artists - not unlike myself - lack understanding of how to work with programmers and how complicated & time consuming their job is.
      So my question is, as programmers - how difficult and time consuming would a project like this be to you? How much would it cost me? How many programmers I might need to pull it off? What specific qualifications I should look for in a programmer? What are some of the potential underwater rocks I should take into consideration? And are there any rudimentary coding, programming, game development courses or books I should check out to have a better understanding on how to work with programmers? Assuming wannabe game developers pop up on forums like this all of the time - are there any Game Dev 101 videos or articles you'd recommend checking out?
      Any replies and information would be very much appreciated. Thank you very much!
    • By Angelic Ice
      I'm actually an intense user of Lua for scripting-related events, but there is one thing that bothers me a lot: Serialising an in action scene.
      Let's take a dialogue-system as an example. In Lua, I see these often realised via co-routines, and did that myself quite often, but it's a difficult act to serialise the exact state. I know about serialisers like Pluto, but those are not really clean solutions, a change in the Lua-version etc. and suddenly the behaviour is anything but defined. I don't think I would want to serialise the raw content of Lua's stack etc.
      This concludes to my question: How are these systems serialised?
      Using a state-machine could be one thing, but I fear that writing such code might be difficult for casual scripter that want to modify the game a bit on their own - being an inherent reason of me picking Lua, as it is simple to understand and learn.
      Any solutions to this? I would love to see implementations, prototypes, or articles of any measure. Maybe there is a different simple scripting-language that solves this better?
      Thanks for your time.
    • By Pixelated_Nate
      Hello all!
      I'm currently designing a 2D, Puzzle/Action RPG, in a similar vein to Legend of Zelda: Link to The Past, in Unity and require a Programmer partner in which to work with me.
      The project, yet to be titled, will feature:
      A semi-open world, represented through pixel art, in which the player traverses to enter dungeons and advance the story. A handful of side-quests that require memorizing details and using puzzle-mechanics. A fast-paced, melee combat system that will include dodging, blocking and utilizing four different attack types that can be switched on the fly. A simple inventory of "Key Items" to be used in order to advance the story. Day & Night system and Weather Effects, with weather effecting combat.  A very simple Dialogue System to convey information via colored text. Saving/Loading via exporting and importing a physical save file. Majority of the project is already planned out, with plans to release commercially and splitting the profits equally among the two of us. 
      I would request that the applicant is able to work semi-independently, following an outline, and that they have experience in both C# programming  *and* putting those scripts to use inside Unity, whilst I will be creating the Art, Music/SFX and doing Level Design (Though if you are also comfortable in assisting me with these, I wouldn't be opposed.).
      Work will be shared in either Github or Unity Collab (Applicants preference), with communication done via Discord. 
      For more information and to apply, please contact me at nathan.jenkins1012@gmail.com
      Thanks for reading! 
    • By Just4lol
      I'm looking for my dream teammate(s) to help me work on my Unity game. I still dont know where Im going with that project but I want to make a good final product that I would be able to sell or publish it for free on Steam.  Here a video of the prototype (The only thing I dint made is the skybox) https://www.youtube.com/watch?v=y2Otmt9jRkc
      My discord : Just4lol#46982
      I want somone at least as competent as me : 
      - I want somone with at least one year of experience in Unity (already worked with scriptable object and know oop).
      - Already worked with shaders or can do editors tools is a plus.
      - Can do 3d models in Blender or can do 2d art for the ui or particles effects.
      - Can make soundtracks or sound effects a bonus.
      Im a french Canadian so mind my english I will do my best to edit any errors I see. 
    • By Garret Thomson
      Hey all,
      I just wanted to share a VS extension. It's useful for other people who are debugging multiple processes (aka server, player1, player2) with breakpoints that will break across those processes. It just puts up a (user defined sized) label in your window telling you which process the debugger dropped into. It's simple, but useful for rapid fire debugging for multiple processes to make it obvious that process you broke in. Just put some cookie text in your command lines and configure it from View -> Other Windows -> Process Hint.
      Feedback or questions welcome.
  • Advertisement
  • Advertisement

C# Trouble with Collision Detection & Response in Monogame

Recommended Posts

So, I currently have a Rectangle that updates with player position, and is the size of the sprite. I'm using Tiled as my tile editor, and have an Object layer called "collision" which I loop over when loading the map, and store each object as a Rectangle in a list. Then in my Update I check if the player is colliding with any of those rectangles (via looping over the list) with Rectangle.Intersects(Rectangle). And if a collision is detected, I handle it with this function:


private static void HandleCollision()
    if (_currentLevel.IsColliding())
        float deltaLeft = Math.Abs((Player.Instance.Position.X + Player.Instance.PlayerCollider.Width) - (_currentLevel.collidingRectangle.X));
        float deltaRight = Math.Abs((Player.Instance.Position.X) + (_currentLevel.collidingRectangle.X + _currentLevel.collidingRectangle.Width));
        float deltaUp = Math.Abs((Player.Instance.Position.Y + Player.Instance.PlayerCollider.Height) - (_currentLevel.collidingRectangle.Y));
        float deltaDown = Math.Abs((Player.Instance.Position.Y) - (_currentLevel.collidingRectangle.Y + _currentLevel.collidingRectangle.Height));
        if (deltaLeft < deltaRight && deltaLeft < deltaUp && deltaLeft < deltaDown)
            Player.Instance.Position.X -= deltaLeft;
        else if (deltaRight < deltaLeft && deltaRight < deltaUp && deltaRight < deltaDown)
        Player.Instance.Position.X += deltaRight;
        else if (deltaUp < deltaLeft && deltaUp < deltaRight && deltaUp < deltaDown)
            Player.Instance.Position.Y -= deltaUp;
        else if (deltaDown < deltaLeft && deltaDown < deltaRight && deltaDown < deltaUp)
            Player.Instance.Position.Y += deltaDown;

I know this is ugly, but that's not the problem, the problem is that when it detects a collision, the player "spazzes out" and just bounces in and out of the rectangle it's colliding with. I've tried a couple other ways too, like just setting the player position without using absolute values, with similar calculations. The same problem happens though. Also it allows the player to just phase through platforms from one side, and the player will get teleported to other sides sometimes, so this is very bad.  

The "spaz" out seems to happen when detecting collision from 2 seperate rectangles, it works "fine" when just going straight down onto a single platform if there are no other platforms around it, but up and sides don't work correctly and if it's close to other platforms then the player might get teleported to another side.

I'm not sure how else to handle it, I've done research and googled a lot, as well as searched here, but everything I find is about the actual collision detection, which I'm doing via the Rectangle.Intersects() method. I've been at this for 3-4 days, before deciding to post here.

I also tried to do it by "requesting" input, like checking where the player would be the next frame, then if it was going to intersect with the rectangle, don't move, and if it wouldn't intersect, it was free to move, but that was really buggy as well with pretty much the same results as this.

Here's a GIF of what it looks like in action. ( not sure why the actual rectangles are off position, I assume something to do with casting the positions from double to int when reading them in )


Edited by fullweeb

Share this post

Link to post
Share on other sites

Of-course it bounces. You are doing this:

  • Detect rectangle intersection using Monogame API calls (this only tells you that an intersection occurred)
  • Make the player going back/forward by the tile rectangle

The problem with this method is that the player will be pushed by the same width/height no matter where the collision did happen. With the most common implementation of player's x/y changing by a certain amount of pixels per frame/update, depending the speed of movement and where the starting position was, the collision may be detected when player is 5 pixels inside the tile, 3 pixels inside the tile, 14 pixels inside the tile, or not get detected at all, for example player is at x = 5, tile is at x = 22, tile and player box are 16x16, and you set speed at 40 pixels per frame, resulting in the player going through the tile with no collision being detected, because player was at 5 one frame, and at 45 the next frame.

[Animated gif here]

A quick fix

Calculate the penetration vector of the player rectangle into the tile rectangle. By moving the player back by the penetration vector, not the tile width, the player will be rejected and leaving touching the tile side.

This method has issues with tile based maps, that you will need to address somehow but you have a starting point now. Experiment to adapt to your needs.

This is what you want: https://vimeo.com/64923588

The formula for the penetration vector, as long as we are in Axis Aligned Bounding Box (AABB) land, is simple:

TX = Player.x - Tile.x;
TY = Player.y - Tile.y;

But you will push the player only in the direction of the axis with greater absolute value.

Read more on penetration vector for AABB: http://hamaluik.com/posts/simple-aabb-collision-using-minkowski-difference/

Possible solutions to "player stuck between tiles" or "player pushed out of an entire line of tiles":

Possible solutions to "player still miss the tile if moving too fast":

  • Don't allow player to move more pixels, per frame, than the tile width (only possible if you have a constant tile width).
  • Use a more complex AABB collision/penetration detection that account for moving AABB: Example: http://scarymazezgame.org/scary-maze/scary-maze-game-7
Edited by Lorraine Craig

Share this post

Link to post
Share on other sites

A better and more reliable approach would be that you do not move the player by default but calculate a prediction vector first pointing to where the player would be after frame update when there would be no collisions between the just state and the predicted state.

Check any collsion rects against this vector using a simple rectangle->line intersection test to see if there would be a collision occuring. If a collision was detected you need to adjust the vector to point to the closest AABB point that is on but not inside the rectangle. Do this for any possible collision target on that vector.

After any collisions were calculated move the player to its new position.

I did this in an AI driver simulation to move vehicles outside of the players field of view that leads to less penetration of the physics cycle.

Share this post

Link to post
Share on other sites

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

  • Advertisement