Note that there are some explanatory texts on larger screens.

plurals
  1. POXNA 2D Platformer Collision and Gravity
    primarykey
    data
    text
    <p>I know this question might get asked a lot and for that I am sorry. But I have had trouble with collisions in my game for a while and I would like some help.</p> <p>First off, the game is a 2D Platformer. Each solid is put in a list. I have this code for collision detection which works pretty good for me:</p> <pre><code> if (player.rectangle.Intersects(rect)) { player1Collision = true; colSolid = solid; colRectangle = rect; } if (player1Collision) { Vector2 pos = player.position; Vector2 pLeft = new Vector2(player.BoundingBox.Left, 0); Vector2 pRight = new Vector2(player.BoundingBox.Right, 0); Vector2 pTop = new Vector2(0, player.BoundingBox.Top); Vector2 pBottom = new Vector2(0, player.BoundingBox.Bottom); Vector2 sLeft = new Vector2(colSolid.BoundingBox.Left, 0); Vector2 sRight = new Vector2(colSolid.BoundingBox.Right, 0); Vector2 sTop = new Vector2(0, colSolid.BoundingBox.Top); Vector2 sBottom = new Vector2(0, colSolid.BoundingBox.Bottom); if (player.rectangle.Intersects(colRectangle)) { if (player.velocity.X &gt; 0 &amp;&amp; Vector2.Distance(pRight, sLeft) &lt; player.texture.Width / 2)//left { player.velocity.X = 0f; pos.X = colSolid.BoundingBox.Left - player.BoundingBox.Width; } else if (player.velocity.X &lt; 0 &amp;&amp; Vector2.Distance(pLeft, sRight) &lt; player.texture.Width / 2)//right { player.velocity.X = 0f; pos.X = colSolid.BoundingBox.Right; } if (player.velocity.Y &gt; 0 &amp;&amp; Vector2.Distance(pBottom, sTop) &lt; player.texture.Height/ 2) //top { player.velocity.Y = 0f; player.gravityOn = false; pos.Y = colSolid.BoundingBox.Top - player.BoundingBox.Height; } else if (player.velocity.Y &lt; 0 &amp;&amp; Vector2.Distance(pTop, sBottom) &lt; player.texture.Height / 2)//bottom { player.velocity.Y = 0f; pos.Y = colSolid.BoundingBox.Bottom; } player.position = pos; } else { player.gravitySpeed = 0.15f; player.gravityOn = true; } } </code></pre> <p>However the problem is that if the player is not intersecting with the rectangle I set the gravity to on, therefore he falls continuously as he collides with the solid and then is put on top to not collide with it. All I need to know is: how can I avoid this? Is there any other way that I could set the gravity to on without the player falling towards the solid continuously, only to be put back on top of the solid to fall again?</p> <p>Any help is appreciated.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload