Jump to content
  • Advertisement
fullweeb

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
Advertisement

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
  • Advertisement
  • Popular Tags

  • Similar Content

    • By Woody Stevens
      Hi,
       
      I am looking for a TCP or HTTP networking library similar to Lidgren (UDP)?
       
      This is primarily for sending game map data and potentially other large messages from Server to Client.
       
      I do want to keep Lidgren for my chat messages, player position, small fast updates etc. I especially love the flow of data and the library usage in general, so any libraries of a similar style would be excellent. Preferably something open source, free and reliable.
      I also must be able to swap between localhost and an ip address with ease, like Lidgren, as I run a server for singleplayer/mp/lan.
       
      My game maps are similar to minecraft, but it is 2d and only one Z-level, so i'm sending a jagged array of Tile object data (currently only enum TileID.Grass) down the pipe to the Client. Problem is if i'm sending a large map 1024 x 1024 tiles down the to client that's quite a lot of data, and Lidgren is relatively slow to build the writes (before the message is even sent!). It is fine when i'm using smaller maps < 512 x 512 ( xTiles * yTiles ).

      I know about chunking and will look into implementing this later, whilst taking into account the user's position in the world to only send nearby chunks.
       
      An example of my code that can be slow:
      private void WriteWorld(NetOutgoingMessage outgoing) { try { var world = WorldManager.Instance.CurrentWorld; outgoing.Write(world.XTiles); outgoing.Write(world.YTiles); for (int x = 0; x < world.XTiles; x++) { for (int y = 0; y < world.YTiles; y++) { // Write Tile obj data outgoing.Write((int)world.Tiles[x][y]); // <-------- Slow here when xTiles and yTiles are each > 512 ! } } } catch (Exception ex) { // log send error } }  
      I'd love to hear from you guys, especially if any of you have come across a similar challenge.
    • By ethancodes
      I'm working on a system for my game that will allow the player to stack pick ups in a queue. As one pick up expires, the next automatically activates. I'm having an issue though where if I pick up the first one, it activates fine, but if i pick up a second directly after it, it overrides the first one, activates the second one, and then once it has run it's course, everything goes back to normal gameplay, no first pick up. I'm not sure why this is happening. Hopefully someone can spot what I'm doing wrong in my code.
      Here is the code for the pick up manager:
      // Update is called once per frame void Update () { if (pickUpQueue.Count != 0 && !pickUpActive) { pickUpActive = true; pickUpQueue[0].ActivatePickUp(); } DeactivatePickUp(); } void DeactivatePickUp () { if (pickUpQueue.Count != 0 && pickUpActive) { Destroy (pickUpQueue [0]); pickUpQueue.RemoveAt (0); pickUpActive = false; } } And here is the PickUp:
      public override void ActivatePickUp () { ball.GetComponent<Ball>().Speed = 2.0f; //increase ball speed... ball.GetComponent<Ball>().StartCoroutine(timer); //...set time that power up is active }  
      There is also a Base Pick Up:
      public void OnCollisionEnter2D (Collision2D collision) { Vector2 tweak = new Vector2 (Random.Range(0f, 0.2f),Random.Range(0f, 0.2f)); this.gameObject.GetComponent<Rigidbody2D>().velocity += tweak; //if the pickup makes contact with the paddle or ball.... if (collision.gameObject.tag == "Paddle" || collision.gameObject.tag == "Ball") { GameObject.FindObjectOfType<GameManager>().GetComponent<PickUpManager>().pickUpQueue.Add(this); Destroy(gameObject); //...and finally destroy power up object } } As a side note, I am trying to find a solution to this that will work for all of my pickups. Some pickups are ammo based, some are timed. 
    • By Hellados
      Hello guys, my name is Giorgi and i'm newbie game developer i'm learning Pixel art and after pixel art  i want learn C# and don't know how and where start i'm bad with programming language and know only HTML/CSS
    • By D34DPOOL
      Edit Your Profile D34DPOOL 0 Threads 0 Updates 0 Messages Network Mod DB GameFront Sign Out Add jobEdit jobDeleteC# Programmer for a Unity FPS at Anywhere   Programmers located Anywhere.
      Posted by D34DPOOL on May 20th, 2018
      Hello, my name is Mason, and I've been working on a Quake style arena shooter about destroying boxes on and off for about a year now. I have a proof of concept with all of the basic features, but as an artist with little programming skill I've reached the end of my abilities as a programmer haha. I need someone to help fix bugs, optomize code, and to implent new features into the game. As a programmer you will have creative freedom to suggest new features and modes to add into the game if you choose to, I'm usually very open to suggestions :).
      What is required:
      Skill using C#
      Experience with Unity
      Experience using UNET (since it is a multiplayer game), or the effort and ability to learn it
      Compensation:
      Since the game currently has no funding, we can split whatever revenue the game makes in the future. However if you would perfer I can create 2D and/or 3D assets for whatever you need in return for your time and work.
      It's a very open and chill enviornment, where you'll have relative creative freedom. I hope you are interested in joining the team, and have a good day!
       
      To apply email me at mangemason@yahoo.com
    • By Andrew Parkes
      I am a talented 2D/3D artist with 3 years animation working experience and a Degree in Illustration and Animation. I have won a world-wide art competition hosted by SFX magazine and am looking to develop a survival game. I have some knowledge of C sharp and have notes for a survival based game with flexible storyline and PVP. Looking for developers to team up with. I can create models, animations and artwork and I have beginner knowledge of C sharp with Unity. The idea is Inventory menu based gameplay and is inspired by games like DAYZ.
      Here is some early sci-fi concept art to give you an idea of the work level. Hope to work with like minded people and create something special. email me andrewparkesanim@gmail.com.
      Developers who share the same passion please contact me, or if you have a similar project and want me to join your team email me. 
      Many thanks, Andrew.

    • By mike44
      The reference assemblies for framework ".NETFramework,Version=v3.5" were not found. To resolve this, install the SDK or Targeting Pack for this framework version or retarget your application to a version of the framework for which you have the SDK or Targeting Pack installed. Note that assemblies will be resolved from the Global Assembly Cache (GAC) and will be used in place of reference assemblies. Therefore your assembly may not be correctly targeted for the framework you intend.

      Hi
      what to do with the above error in ms code/Unity3d Project on Ubuntu 18.04? I've installed it like:
      https://www.microsoft.com/net/learn/...ux/ubuntu18-04
      Many thanks
    • By Dave Haylett
      Hi.
      I have pulled in five NuGet packages for my Visual Studio 2017 project, however when I build the project, VS spits out 10 .DLL and .XML files in the root of the binary folder, to do with the packages. Can't I shove them into a \packages folder so the user doesn't see these ugly resources next to the .exe file?
      I've Googled moving the packages but the only responses seem to be around moving the installation folder of the NuGet packages on the local machine, as opposed to where VS builds them to.
    • By MoreLion
      hey all! We are looking for members for our Unity horror game! 
      Here’s the story:
      After a deadly virus plunges the world into chaos killing 85% of the human population there are now what they call “zones” these zones are watched very closely by the surviving government, people are checked every day for the virus, even if you touch the spit or any human waste or fluids of the victim who is infected, you will die. But one day, people in the west zone start to go missing, 1 woman goes outside the walls to uncover the mystery, is there more to the virus than meets the eye?, That is where your story starts.
      This game is not a long development game, I have loads other game ideas,
      I will also allow you to have a bit of creative freedom if you wish to add or share a idea!
      And no, it’s not a zombie game lol I feel like zombie games are too generic, in this game you will encounter terrifying beasts!
      There is some concept art one of our concept artists have made
      If interested email liondude12@gmail.com
    • By Victor Rodriguez
      Hi there! Is the first time that I'm posting here so I'm sorry if I'm doing it wrong ha. 
      So here it comes, my doubt is, I'm doing a game with different levels, each of these levels in one different scene. Each scene contains to cameras that you can change pressing a button. Everything works fine. 
      The only problem is that I would like it to look a bit more professional, and I would like that if you finish the level with camera2, the next level start the same way. I've been thinking about using dontdestroyonloadon both cameras, but obviously this cameras need to be attached to the player to make the movement work, what do you recommend? Sorry If I've explained it in a messy way, and feel free to dm me for anything. Thanks in advance! 
    • By Jemme
      Hello,
      I have built games in the past in unity and XNA, android SDK etc. but it’s always been quite specific, when building a small Game Engine or more accurately a sub-set of code that is reusable across projects the tendency to structure the project falls towards composition of objects that are more "generic" than toward inheritance-based models.
      For the Engine specific sub-systems, we have components such as a MeshInstance/Renderer that contains a Handle to a Mesh in a MeshLibary and a handle to a Material from the material library which in turn hold PSO data etc. We can also have Components such as SoundSorce, which are managed by an AudioSystem and contains the data to specify what sound, at what volume etc.
      These systems can be Set up and shut down from within the Engine/Game class such as:
      void Engine::Run() { if (Initialize()) { //Main loop call the systems //Dispatch Events } ShutDown(); } void Engine::Initialize() { //Init all sub-systems FileSystem.Init(); RenderSystem.Init(); PhysicsSystem.Init(); AudioSystem.Init(); WorldManager.Init(); //Creats Entities } void Engine::ShutDown() { //shutdown all sub-systems WorldManager.ShutDown(); AudioSystem.ShutDown(); PhysicsSystem.ShutDown(); RenderSystem.ShutDown(); FileSystem.ShutDown(); } But where does logic go? in smaller games like XNA or SFML you generally create some managers, let’s take the example of darkest dungeon.
      They have various managers like:
      Darkest Dungeon Manager Campaign Selection Manager Estate manager Party formation manager Town manager etc.. All the logic and systems to run the game are based within the managers, and objects contain scripts of data that are fed thorough the systems.
      In a small reusable code base how would you separate the logic from the engine, should like the smaller games all the managers just be shoved into the Engine Class even though that goes against a reusable data-driven framework? Should they just be stuck in a GameLogic system that's is initialized in the Engine Initialism function. And how do people tend to connect data scripts to the other various engine systems without causing too much game specific coupling. For example, you can use an Event System, but firing and event such as DAMAGED_BY_ZOMBIE and having the internal engine respond to that seems to break the separation from the low-high level of the engine system.
      Would be great to here some opinions from the community on this subject as it is quite a vital and potentially problem prone aspect of engine/game development.
      Thanks.
  • Advertisement
  • Popular Now

  • Forum Statistics

    • Total Topics
      631367
    • Total Posts
      2999592
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!