The character passes through the slab along the x coordinate

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
NewbiePeta
Prole
Posts: 1
Joined: Sat Sep 28, 2024 3:52 pm

The character passes through the slab along the x coordinate

Post by NewbiePeta »

Basically the player moves through the platform at the x coordinate since I don't do anything with it. How to make sure that when the CheckCollision function detects an intersection of objects, the player returns back

I did this through Google Translate so I think it won’t be very clear what I meant

main.lua

Code: Select all

function love.load(arg)
  if arg and arg[#arg] == "-debug" then require("mobdebug").start() end
  camera = require "libraries/camera"  
  cam = camera()  
  platforma = {}
  player = {}
  wall={}  
  player.x = 300
  player.y = 400
  player.width = 60  
  player.height = 120  
  player.hp = 100
  player.speed = 300
  player.gravity = 600
  player.jump = 200  
  
  platforma.x = 0
  platforma.y = 900
  platforma.width = 1800
  platforma.height = 30
  
  wall.x = 200
  wall.y = 850
  wall.width = 50
  wall.height = 50  
end  

function love.update(dt)
  if CheckCollision(player, platforma) then 
	--I don't know what's here
  else
	player.y = player.y + player.gravity*dt 
  end
  
  if love.keyboard.isDown("a") then
    player.x = player.x - player.speed*dt
  elseif love.keyboard.isDown("d") then
    player.x = player.x + player.speed*dt
  elseif love.keyboard.isDown("w") then
	--хз че писать  
  end  
  
  cam:lookAt(player.x, player.y)  
end
  
function love.draw()
  cam:attach()
    love.graphics.setBackgroundColor(0,0,0)
  
    love.graphics.setColor(1,1,0,1)
    love.graphics.rectangle("fill", player.x, player.y, player.width, player.height)
  
    love.graphics.setColor(0,0.4,0,1)
    love.graphics.rectangle("fill", platforma.x, platforma.y, platforma.width, platforma.height)
  
    love.graphics.setColor(0.5,0,0,1)
    love.graphics.rectangle("fill", wall.x, wall.y, wall.width, wall.height)
  cam:detach()
end  

function CheckCollision(object1, object2)
  local x1 = object1["x"]
  local x2 = object2["x"]
  local y1 = object1["y"]
  local y2 = object2["y"] 
  local width1 = object1["width"]
  local width2 = object2["width"]
  local height1 = object1["height"]
  local height2 = object2["height"]  
  return x1 < x2+width2 and
         x2 < x1+width1 and
         y1 < y2+height2 and
         y2 < y1+height1 
end
conf.lua

Code: Select all

function love.conf(t)
    t.window.width = 1900
    t.window.height = 1000
    t.window.vsync = 0
    t.window.title = "Test"           
    t.window.borderless = false     
    t.window.resizable = false            
end
And this library
Attachments
camera.lua
(5.92 KiB) Downloaded 84 times
User avatar
Azzla
Prole
Posts: 43
Joined: Sun Mar 29, 2020 2:23 am

Re: The character passes through the slab along the x coordinate

Post by Azzla »

Re-order your update function like so:

Code: Select all

function love.update(dt)
  cam:lookAt(player.x, player.y)  
  if love.keyboard.isDown("a") then
    player.x = player.x - player.speed*dt
  elseif love.keyboard.isDown("d") then
    player.x = player.x + player.speed*dt
  elseif love.keyboard.isDown("w") then
	--хз че писать  
  end  
  
  
  if CheckCollision(player, platforma) then 
	--I don't know what's here
  else
	player.y = player.y + player.gravity*dt 
  end
end
Remember that all games need to do these three things in order, on every frame:
1. Read input from the player. | love.keyboard.isDown
2. Simulate the game logic. | love.update
3. Render the game to the screen. | love.draw

You reversed the order of the first two by checking for collisions first (simulate game logic), then responding to player input (keyboard presses). This means that no matter how you alter the players position in the collision response, the player's input will make the resulting position unreliable.

The code you have should stop the player falling through floors at least, but if you ever need more advanced collision resolution, it can get a bit hairy. I'd recommend something like bump.lua for a simple platformer like your example.
libraries: Stalkpile
electronic music stuff: https://soundcloud.com/azzlamusic
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 1 guest