Discord
Login
Community
DARK THEME

How to make it so the player doesn't clip into block?

https://microstudio.io/DanMan97/minceraft2/

I've been trying and trying and trying but to no avail.

  collision = function(x1,y1,w1,h1,x2,y2,w2,h2)
    this.hit = false
    distx = abs((x2-x1))
    if distx <= w2/2 + w1/2 then    
      disty = abs((y2-y1))        
      if disty <= h2/2 + h1/2 then
          this.hit = true
      else
      end
    end
  end

https://microstudio.dev/i/HomineLudens/aabb/ - fast collision detection of 2 rectangles

you set a local variable that contains the current position plus the direction of movement. and you run a collision check on this temporary variable. If there is no collision, you can move the player's character.

That's what I've been trying to do, though I did switch to your collision code, I don't notice anything different but it probably better than mine so thanks.

Calculating a square root requires a series of steps, including division, addition, comparison - up to 64 times.

Add FPS display with your code where you have the square root calculated and for the code without calculating the square root you will see the differences.

I have a difference of 10 FPS

  • 40 FPS with square root calculation
  • 50 FPS without calculation

https://microstudio.io/i/Loginus/minceraft22/ << check the code

Well holy cow, it works. I can no longer clip into blocks, and it's laggy enough so you can't see the bounce. Thanks I been trying for ever for it to work

Funny thing: the game is already in the megabytes, my first project in the megabytes.

You can still speed up the program. Now you check during each "update" for each block whether there was a collision with the mouse. You only need to do this when the player has pressed the mouse button.

main file , update function >>

  if mouse.pressed then 
    blocks.forEach( function( item ) item.mouseCollide() end)
  end

https://microstudio.io/i/Loginus/minceraft22/

Post a reply

Progress

Status

Preview
Cancel
Post
Validate your e-mail address to participate in the community