So I dont like to do this but here is a chunk of code...
So hey... Uh... I did not want to do this but I'm absolutely stuck... My character when touching a wall slowly goes into the ground... And I dont know why. I tried a lot of things but I just cant. https://microstudio.io/i/MurdexStudio/with_this/
The problem is in the object player file within the update. Probably with the end but I'm not sure :( Sorry and thank you.
for each axis check collision separately .
Code generated by AI .
update = function(map)
local _initialX = clone(this.x)
local _initialY = clone(this.y)
local _direction = keyboard.D - keyboard.A
if _direction != 0 then
local _xVelocityAccelerated = this.xVelocity + _direction * this.acceleration
this.xVelocity = max(min(_xVelocityAccelerated, this.speed), -this.speed)
else
if this.xVelocity > 0 then
this.xVelocity = max(0, this.xVelocity - this.friction)
elsif this.xVelocity < 0 then
this.xVelocity = min(0, this.xVelocity + this.friction)
end
end
if this.grounded == false then
this.yVelocity -= this.gravity
if this.yVelocity < -this.maxFallSpeed then
this.yVelocity = -this.maxFallSpeed
end
end
if keyboard.press.SPACE and this.grounded then
this.yVelocity = this.jumpHeight
this.grounded = false
end
resolveCollision = function(axis)
local velocity = 0
if (axis == "x") then
velocity = this.xVelocity
else
velocity = this.yVelocity
end
local originalVelocity = velocity
while abs(velocity) > 0.001
local tempPos = 0
if (axis == "x") then
tempPos = _initialX + velocity
else
tempPos = _initialY + velocity
end
local collision = false
for hb in map.cHitBoxes
local tempHitBox = clone(this.hitBox)
if axis == "x" then
tempHitBox.x = tempPos
else
tempHitBox.y = tempPos
end
if cCollision(tempHitBox, hb) then
collision = true
break
end
end
if collision then
velocity *= 0.5
else
if axis == "x" then
this.x = tempPos
else
this.y = tempPos
end
break
end
end
end
local xCollided = resolveCollision("x")
local yCollided = resolveCollision("y")
if yCollided and this.yVelocity < -0.01 then
this.grounded = true
this.yVelocity = 0
else
this.grounded = false
end
if sign(_direction) != 0 then
this.direction = _direction
end
this.hitBox.x = this.x
this.hitBox.y = this.y
if this.grounded then
if abs(this.xVelocity) > 0.1 then
this.state = "run"
else
this.state = "idle"
end
else
this.state = "jump"
end
end
I'm sorry but it doesnt work.
https://microstudio.io/i/Loginus/with_this/
Collision works perfectly!
If you modified the code a bit, you would have an easier task.
Typically, three variables are used:
position, velocity and acceleration (for each axis).
In the update function, you check if the key is pressed.
If so, you set the acceleration.
Then you update the velocity (calculate the velocity from the current velocity and the current acceleration and mass).
You calculate the next position of the object in the temporary variable and check if it will collide.
See how it is done in QuickEngine.
I put the charcter in the new pos but save the prev pos in another variable.