[SOLVED] Sidescroller Collision?

Can anybody point me in the right direction of doing sidescroller collision and physics?

Well not sure if it is the right direction but here some example logic.

//You have 2 list enemybullets and playerbullets;

foreach (Enemy enemy in enemies)
{
foreach (Gun g in enemy.guns)
{
enemybullets.Add(g?.Fire());//fire enemy projectiles/bullets.
}
}
foreach (Gun g in Globals.player.car.trunks[Globals.player.car.activeTrunk].guns)
{
if (g != null && !Globals.player.car.tta.flag)
playerbullets.Add(g.Fire());//fire players…
}

playerbullets.ForEach(delegate (Bullet b)
{
b.Move();//Move them
});

        enemybullets.ForEach(delegate (Bullet b)
        {
            b.Move();Move them
        });

//Collison cheks here
playerbullets.ForEach(delegate (Bullet b)
{
foreach (Enemy e in enemies)
{
if (!b.endoflife&&CollisonDedector.Dedect(b.position, e.position, b.texture, e.texture))
{
b.endoflife = true;
e.Move(-50, -10);
e.healt -= b.damage;
Globals.player.money+=100-e.healt;
}
}

        });
        enemybullets.ForEach(delegate (Bullet b)
        {
            if (CollisonDedector.Dedect(b.position, Globals.player.car.trunks[Globals.player.car.activeTrunk].position, b.texture, Globals.player.car.trunks[Globals.player.car.activeTrunk].texture))
            {
                if (!b.endoflife)
                    Globals.player.car.trunks[Globals.player.car.activeTrunk].healt = Globals.player.car.trunks[Globals.player.car.activeTrunk].healt - b.damage;
                b.endoflife = true;
            }
        });

//Remove dead thnigs.
playerbullets.RemoveAll(b => b.endoflife);
enemybullets.RemoveAll(b => b.endoflife);

        foreach (Enemy e in enemies)
        {
            if ( e.healt <= 0)
                {
                enemyKillCount++;
                }
        }
        enemies.RemoveAll(e => e.healt <= 0);

İ willl post the logic below

1-Have bullet list.(for player ,enemy and/or whatever)
2-Fill the list when you fire.
3-Move the bullets from the lists.
4-Check collisons. Check every bullet against every target of that bullet.
5-Clear dead things.

Basic Collison dedection is this. There are some expert things like first checking who has a chance to hit then do the Collison dedection only on them or how you manage the dedection. Easiest way to dedect collison is compare 2 rectangles or 2 circles.

What things need to collide?

And how much physics do you want? what needs to react?
(simulated building collapse, or simple cannon ball ballistics?)

Yeah, the right direction in a sidescroller is this way:
:point_right:

if you really want to mix it up, the left direction is thata way:
:point_left:

:wink:

XNA.com has a platformer starter kit

I just want the player not going through the ground. I tried doing some stuff but i couldn’t move left and right while colliding

What is your ground made of? By that I mean, how do you plan on telling the ground from the air? Are you using tiles, some of which block movement, are you using line segments to simulate a surface, are you using colors to distinguish ground from air?
Or you could have collision simply at a certain height.

-Point is, we have to find a difference between land and air for your collision detection to have something to check…

I am using tiles with a rectangle as the “hitbox”

Ok… And what part of the player will collide with these terrain rects?

Does the player also use a rectangle? -So you are testing the player rect against nearby tile rects?

Yes i check if the two rectangles intersect, and then i check whether the colliding tile is on top or bottom or sides

Ok, before we move on, you should know there are a lot of ways to do collision detection, so we wont be able to cover all the options, even if I knew them…

Here is just a suggestion off the top of my head:
When you get a move order in any direction, check to see if you can move in that direction, or that would cause collision.

If collision would occur during the next move, move the character to the extreme most point of its current tile, and prevent further movement.

For downward collision, imagine your character rectangle can overlap 2 terrain tiles when it stands between them. In fact, in most cases your character has bottom corners in 2 tiles. (like mario will stand on one tile, but part of him will be slightly over the edge on one side or the other)

So you can just check these bottom corner positions one move ahead, whether they are contained in a nearby terrain rectangle.

If there is no collision for these character bottom corners, player falls or moves down…
(increase down momentum or move the player south)

If there is collision, player is moved to near collision point (on top of the colliding tile)…

Hey, thanks for the advice. I think i have a good idea on how to approach this now :smiley: