Player Hitboxes Acting Strange

Description: Archive discussion
============================
Original description:
Official subforum for LunaLua - an extension framework for SMBX Engine Version 1.3 (which a core of SMBX 2.0). Share your creations that require the use of LunaLUA. You may also share info/tutorials on how to use LunaLUA.

Sambo M
Topic author, Count
Count
Avatar
Sambo M
Topic author, Count
Count
Age: 26
Reputation: 15
Posts: 264
Joined: 27 Jun 2014

Post #1by Sambo » 30 Jan 2016, 4:01

I have this script that creates some custom hitboxes for each player. For some reason, the position of one is affecting the position of the other. P1 and P2's hitboxes both move when only 1 player is moving, and neither of them are positioned at the same place as the player. The way they appear to move doesn't seem to make any sense. Here is the script that I have. It attempts to make a table of all players and run for each player to reduce file size and make it so I don't have to make 2 copies of the script, one for each player. In single player, it works fine.

Code: Select all

function smbxplus.onLoop()
   for INDEX,PLAYER in pairs(Player.get()) do
      --PLAYER 1
      local hitbox = colliders.getAABB(PLAYER) --Load P1's hitbox
      --Create a 8 px thick collision sensor on each side of the PLAYER
      PlayerHitbox[INDEX] = {
               top = colliders.Box(PLAYER.x, PLAYER.y - 8, hitbox.width, 8),
               left = colliders.Box(PLAYER.x - 8, PLAYER.y, 8, hitbox.height),
               right = colliders.Box(PLAYER.x + hitbox.width, PLAYER.y, 8, hitbox.height),
               bottom = colliders.Box(PLAYER.x, PLAYER.y + hitbox.height, hitbox.width, 8),
               innerTop = colliders.Box(PLAYER.x + 8, PLAYER.y + 8, 8, 8),
               innerBottom = colliders.Box(PLAYER.x + 8, PLAYER.y + hitbox.height - 16, 8, 8)
               }
      if smbxplus.debug then
         PlayerHitbox[INDEX].top:Draw(0xff000099)
         PlayerHitbox[INDEX].left:Draw(0x00ff0099)
         PlayerHitbox[INDEX].right:Draw(0x0000ff99)
         PlayerHitbox[INDEX].bottom:Draw(0xffff0099)
         PlayerHitbox[INDEX].innerTop:Draw(0x44444499)
         PlayerHitbox[INDEX].innerBottom:Draw(0xffffff99)
      end
   ...
   end
...
end

The "..." represents parts of the code I removed because they don't need checked.

Another problem, which makes collision detection imprecise, is that in single player (and two player once I can get it fixed), the position of the hitboxes is slightly out of sync with the player's position when he is moving, so that the player will hit a block slightly before the custom hitbox does.
Image
Current Project:
Image

Sambo M
Topic author, Count
Count
Avatar
Sambo M
Topic author, Count
Count
Age: 26
Reputation: 15
Posts: 264
Joined: 27 Jun 2014

Post #2by Sambo » 31 Jan 2016, 1:12

Apparently, the problem is in the Box:draw function in the colliders library. The colliders themselves are in the correct positions; they are simply drawn in the wrong position. The strange thing is that in my old script with a separate copy for each player, this does not occur.

The other problem with the position being slightly out of sync has also been fixed. I have found that adding the player's speed to the position of the colliders causes them to move in sync with the player. So for anyone who wants to hook a collider up to a player, simply add player.speedX to the x position and player.speedY to the Y position to make it move in sync with the player.
Image
Current Project:
Image

Hoeloe F
Nice citizen
Nice citizen
Hoeloe F
Nice citizen
Nice citizen
Reputation: 5
Posts: 16
Joined: 3 Nov 2014

Post #3by Hoeloe » 31 Jan 2016, 11:16

Sambo wrote:Apparently, the problem is in the Box:draw function in the colliders library. The colliders themselves are in the correct positions; they are simply drawn in the wrong position. The strange thing is that in my old script with a separate copy for each player, this does not occur.

The other problem with the position being slightly out of sync has also been fixed. I have found that adding the player's speed to the position of the colliders causes them to move in sync with the player. So for anyone who wants to hook a collider up to a player, simply add player.speedX to the x position and player.speedY to the Y position to make it move in sync with the player.

Adding the player's speed is not the correct solution here. That's only accurate in some cases. First you want to make sure you have LunaLua 0.7.3 installed. Then, instead of using onLoop, which is now deprecated, use onTickEnd, which will run after physics calculations and put the colliders in the correct position relative to the player.

On another note, you're better off not creating new colliders every frame. Once you've constructed a collider you can move it around and resize it as you like, and it's a lot neater to do things that way.

Sambo M
Topic author, Count
Count
Avatar
Sambo M
Topic author, Count
Count
Age: 26
Reputation: 15
Posts: 264
Joined: 27 Jun 2014

Post #4by Sambo » 1 Feb 2016, 3:17

Adding the player's speed is not the correct solution here. That's only accurate in some cases. First you want to make sure you have LunaLua 0.7.3 installed. Then, instead of using onLoop, which is now deprecated, use onTickEnd, which will run after physics calculations and put the colliders in the correct position relative to the player.

I see. I'll look into it. I'm just rather impatient waiting for help, and I tried to figure out my own way.
So, if I just change the whole function's name to onTickEnd, will everything else in it still work?

On another note, you're better off not creating new colliders every frame. Once you've constructed a collider you can move it around and resize it as you like, and it's a lot neater to do things that way.

Oh, I knew that, but for some reason I didn't do it that way.

Added after 1 day 5 hours:
I moved the function to onTickEnd, and now I have the same problem as before, with the position of the hitboxes being out of sync with the player. This time, the position is correct, but once the camera moves, the position of the hitbox starts to lag behind the player again, and the player collides with blocks before they break.
Image
Current Project:
Image


Return to “LunaLua - Extension Framework”

Who is online (over the past 5 minutes)

Users browsing this forum: 4 guests