Strange physics collision using STI?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
Eamonn
Party member
Posts: 550
Joined: Sat May 04, 2013 1:29 pm
Location: Ireland

Strange physics collision using STI?

Post by Eamonn »

I'm using the STI module in order to load my tile maps and set up physics world collision. For some reason, the player will randomly collide with one of the tiles. If he moves up a bit, he can get past it, so it's not like it's the tile in front of him. It also occurs inconsistently. For instance, when I run the game one time, it may never happen. When I run it again, I may not even be able to move. If I move the opposite direction from which I've been stuck and then move back, I'll be able to move again. Here is a gif I created very quickly to help demonstrate my problem better: https://goo.gl/photos/wdCxKDqoYtWTJd8c7 (I couldn't find another service to use, so I just used Google Photos). Here's an image outlining the collision borders in STI for the player and collidable tiles: https://goo.gl/photos/BymKVK1EzSMpQegZA

I'm not entirely sure why this is happening. Here's the code for loading and updating, although it's still rather messy:

Loading:

Code: Select all

  map = sti.new('assets/maps/test_map')
  map:addCustomLayer('Sprite Layer', 4)

  lp.setMeter(32)

  world = lp.newWorld(0, 1000)

  collision = map:initWorldCollision(world)

  sprite_layer = map.layers['Sprite Layer']
  sprite_layer.sprites = {
    player = Player.new(100, 37 * 32, 32, 32, 0, 6, 6, true, 4, 0, 20000, 'right')
  }

  player = sprite_layer.sprites.player

  player.body = lp.newBody(world, player.x, player.y, 'dynamic')
  player.shape = lp.newRectangleShape(13 * 6, 24 * 6)
  player.fixture = lp.newFixture(player.body, player.shape)

  player.body:setLinearDamping(5)
  player.body:setFixedRotation(true)

  translate_x = math_floor(-player.x + window_width / 2 - player.width - 16)
	translate_y = math_floor(-player.y + window_height / 2 - player.height - 16)

  function sprite_layer:update(dt)
    for _, sprite in pairs(self.sprites) do
      Player.update(dt)
    end
  end

  function sprite_layer:draw()
    for _, sprite in pairs(self.sprites) do
      Player.draw()
    end
  end
Updating:

Code: Select all

  world:update(dt)

  translate_x = math_floor(-player.x + window_width / 2 - player.width - 16)
	translate_y = math_floor(-player.y + window_height / 2 - player.height - 16)

  local x, y = 0, 0

  if lk.isDown("w") or lk.isDown("up")		then y = y - player.speed end
  if lk.isDown("s") or lk.isDown("down")	then y = y + player.speed end
  if lk.isDown("a") or lk.isDown("left")	then x = x - player.speed end
  if lk.isDown("d") or lk.isDown("right")	then x = x + player.speed end

  player.body:applyForce(x, y)
	player.x, player.y = player.body:getWorldCenter()

  map:update(dt)
I believe that's all the relevant code, however if you'd like more I'll edit the post.

If anyone could help me out with an explanation and solution to this problem, I'd appreciate it. I updated STI not even two days ago, and STI hasn't been updated in a few months, so I'm definitely up to date.

Thanks!
"In those quiet moments, you come into my mind" - Liam Reilly
User avatar
Jeeper
Party member
Posts: 611
Joined: Tue Mar 12, 2013 7:11 pm
Contact:

Re: Strange physics collision using STI?

Post by Jeeper »

Box2d will get issues if you have multiple "boxes" that are next to eachother like this (even if it is pixel perfect):

[ ][ ][ ]

What you want is 1 box like this:

[______]

Else you will keep getting stuck on invisible walls.
User avatar
CrackedP0t
Citizen
Posts: 69
Joined: Wed May 07, 2014 4:01 am
Contact:

Re: Strange physics collision using STI?

Post by CrackedP0t »

Yeah, what the guy above me said. Honestly, I'd just use Bump.lua instead of Box2d; it's way simpler and less buggy for basic bounding box stuff. github.com/kikito/bump.lua
/人 ◕‿‿◕ 人\
Here, have an umlaut. Ö
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: Strange physics collision using STI?

Post by davisdude »

I recommend this read: http://concernedjoe.com/devblog/joes-anatomy/
Great for Box2d general tips and such
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
meleeman
Prole
Posts: 5
Joined: Mon Apr 30, 2018 7:46 am

Re: Strange physics collision using STI?

Post by meleeman »

davisdude wrote: Wed Jul 08, 2015 7:30 pm I recommend this read: http://concernedjoe.com/devblog/joes-anatomy/
Great for Box2d general tips and such
blog moved it's host. cause it's not there anymore.
Post Reply

Who is online

Users browsing this forum: No registered users and 12 guests