Hello Guest

Author Topic: Getting collisions to work (OGMO created map)  (Read 2312 times)

0 Members and 1 Guest are viewing this topic.

Cone Arex

  • Member
  • Posts: 1
    • View Profile
Getting collisions to work (OGMO created map)
« on: June 27, 2015, 01:35:11 PM »
Hello guys,
I recently started toying with Otter and have come across a problem. I created a level using OGMO and I can't get the collisions to work. The player entity still walks over every tile.
Code: [Select]
Global.ogmoProj1 = new OgmoProject(Assets.OGMO_PROJECT, Assets.OGMO_IMG);

Global.ogmoProj1.BaseTileDepth = 0;
Global.ogmoProj1.TileDepthIncrement = 0;
Global.ogmoProj1.RegisterTag(1, "Walls");
 
int[] tags = {1};

Global.ogmoProj1.LoadLevel(Assets.OGMO_MAP, this);
           
Global.ogmoProj1.GetEntityFromLayerName("Walls").AddCollider(new GridCollider(Global.GAME_WIDTH, Global.GAME_HEIGHT, 32, 32, 1));
grid = (GridCollider) Global.ogmoProj1.GetEntityFromLayerName("Walls").Collider;

Entity gridEntity = new Entity(0, 0, null, grid);
Add(gridEntity);

Der Code for walking looks like this(excerpt):
Code: [Select]
...
else if (Global.PlayerSession.Controller.Button("Right").Down)
            {
                if (X+WIDTH+moveSpeed < Global.GAME_WIDTH)
                {
                    newX = X + moveSpeed;
                    if (!checkScene.grid.GetRect(newX, Y, newX + WIDTH, Y + HEIGHT, false))
                    {
                        xSpeed = moveSpeed;
                    }

                    direction = Global.DIR_RIGHT;
                    sprite.FlippedX = false;
                }
            }
...
What am I doing wrong?

Fruckert

  • Member
  • Posts: 60
  • Total Square
    • View Profile
Re: Getting collisions to work (OGMO created map)
« Reply #1 on: June 27, 2015, 02:56:09 PM »
Try taking a look at the Basic and Platforming Movement classes, they're a pretty good starting point for this kind of thing.
Usually, your player entity's Collider is what you use to check for collisions, as in:
Code: [Select]
...
else if (Global.PlayerSession.Controller.Button("Right").Down)
            {
                if (X+WIDTH+moveSpeed < Global.GAME_WIDTH)
                {
                    newX = X + moveSpeed;
                    if (!Collider.Collide(newX, Y, checkScene.Grid))
                    {
                        xSpeed = moveSpeed;
                    }

                    direction = Global.DIR_RIGHT;
                    sprite.FlippedX = false;
                }
            }
...

This probably will require a bit more debugging and stuff, as I'm not entirely sure what the rest of your player class looks like, or if this is a platformer or whatever, but hopefully this should get you in the right direction.

edit: Woops, I'm pretty sure I called the wrong function. The proper one is Overlap.
Collide returns whatever you're colliding with, not a bool.
« Last Edit: June 27, 2015, 03:18:29 PM by Fruckert »

Kyle

  • Administrator
  • Member
  • Posts: 258
    • View Profile
Re: Getting collisions to work (OGMO created map)
« Reply #2 on: June 28, 2015, 09:52:43 PM »
For the ogmo project, the collider for your grid should be added when you call LoadLevel, so you shouldn't have to get the collider and then add another entity with that collider.

And yeah, you want to be using the Collide and Overlap functions for checking for collisions with your grid.  Collide does return the Collider that is overlapping, and Overlap just returns true or false if there is an overlap.