Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Collision of two sprites is very weird  (Read 2339 times)

0 Members and 1 Guest are viewing this topic.

Kenox

  • Newbie
  • *
  • Posts: 3
    • View Profile
Collision of two sprites is very weird
« on: April 26, 2019, 06:39:02 pm »
Hello Community,

I'm new in this forum and I need some help from you, because I'm currently hanging on some collision stuff.
I can't explain it in words, so watch this video, so you can understand what I mean with it's weird: https://streamable.com/k5frd

Code (C#):
// UPDATE
bool canMove = true;
            if(collision())
            {
                canMove = false;
            }
            else
            {
                oldPos = sprite.Position;
            }

            if(canMove)
            {
                pos += vel * dt;
                sprite.Position = pos;
            }
            else
            {
                sprite.Position = oldPos;
                canMove = true;
            }

// INPUT HANDLER
if(Keyboard.IsKeyPressed(Keyboard.Key.W) || Keyboard.IsKeyPressed(Keyboard.Key.Up)) {
                dir.Y -= 1.0f;
            }
            if (Keyboard.IsKeyPressed(Keyboard.Key.A) || Keyboard.IsKeyPressed(Keyboard.Key.Left))
            {
                dir.X -= 1.0f;
                sprite.Origin = new Vector2f(sprite.GetLocalBounds().Width, 0);
            }
            if (Keyboard.IsKeyPressed(Keyboard.Key.S) || Keyboard.IsKeyPressed(Keyboard.Key.Down))
            {
                dir.Y += 1.0f;
            }
            if (Keyboard.IsKeyPressed(Keyboard.Key.D) || Keyboard.IsKeyPressed(Keyboard.Key.Right))
            {
                dir.X += 1.0f;
                sprite.Origin = new Vector2f(0, 0);
            }

            if(dir.X == 0.0f && dir.Y == 0.0f)
            {
                dir = new Vector2f(0.0f, 0.0f);
            }
            else
            {
                dir = dir / (float)Math.Sqrt(2.0f);
            }

            SetDirection(dir);

// SET DIRECTION

private void SetDirection(Vector2f dir)
        {
            vel = dir * speed;
        }

// COLLISION CHECK

private bool collision()
        {
            if(Program.map != null)
            {
                foreach (Resource resource in Program.map.resources)
                {
                    if (CollisionTester.PixelPerfectTest(sprite, resource.sprite, 0))
                    {
                        return true;
                    }
                }
            }
            return false;
        }

Appreciate all kind of help. Thanks :)

Arcade

  • Full Member
  • ***
  • Posts: 230
    • View Profile
Re: Collision of two sprites is very weird
« Reply #1 on: April 26, 2019, 08:59:11 pm »
During a collision you seem to reset you sprite's position back to 'oldPos', but you never reset the variable you used to calculate the position, 'pos'.

Kenox

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Collision of two sprites is very weird
« Reply #2 on: April 26, 2019, 09:41:27 pm »
During a collision you seem to reset you sprite's position back to 'oldPos', but you never reset the variable you used to calculate the position, 'pos'.
Ohh, right. What a big mistake from me. - Thanks, now it works a bit.

Another question: Is it possible to let it look cleaner and better without that flickering (because of setting the position) while colliding with (in this example) the resource?

Video: https://streamable.com/zorot

Greetings

Arcade

  • Full Member
  • ***
  • Posts: 230
    • View Profile
Re: Collision of two sprites is very weird
« Reply #3 on: April 27, 2019, 12:18:16 am »
You didn't show how you draw your objects, but I would guess you're splitting up your player movement and collision response into separate frames, causing your object to alternate between collision and non-collision each frame. Basically, your logic probably flows something like:
1. Move object
2. Draw frame
3. If object is colliding, move object back.
4. Draw frame
5. Repeat

What you probably want is something more like:
1. Move object
2. If object is colliding, move object back.
3. Draw frame
4. Repeat

By the way, now might be a good time to learn how to use a debugger if you haven't already  ;) . These types of problems are usually pretty easy to spot when using one

Kenox

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Collision of two sprites is very weird
« Reply #4 on: April 27, 2019, 11:15:45 am »
You didn't show how you draw your objects, but I would guess you're splitting up your player movement and collision response into separate frames, causing your object to alternate between collision and non-collision each frame. Basically, your logic probably flows something like:
1. Move object
2. Draw frame
3. If object is colliding, move object back.
4. Draw frame
5. Repeat

What you probably want is something more like:
1. Move object
2. If object is colliding, move object back.
3. Draw frame
4. Repeat

By the way, now might be a good time to learn how to use a debugger if you haven't already  ;) . These types of problems are usually pretty easy to spot when using one

Thanks again!! Now it's working. For others who maybe need help: I've changed the collision checker in my Update Method:
            pos += vel * dt;
            sprite.Position = pos;

            bool IsCollision = collision();
            Program.collisionText.DisplayedString = "Collision: " + IsCollision.ToString();
            if (IsCollision)
            {
                sprite.Position = oldPos;
                pos = sprite.Position;
            }
            else
            {
                oldPos = sprite.Position;
            }

:)

 

anything