How should I implement physics using windshield?
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
How should I implement physics using windshield?
This is the first time I am creating my own game using love2d. I decided to use windshield for my physics because I believed Box2D didn't have the tools I needed to make the process simple and concise. However, I am having issues with getting it to work for a platformer game. The tutorial I used was mainly for a top-down Zelda clone, and while my character is moving, the animations are a bit off. When I move left to right, the y velocity changes even though the I am not adding any forces to the y direction of the collision box. I feel like this is a simple fix but I wasn't able to find much information in the windshield documentation. Is there a way I can fix this small issue?
Re: How should I implement physics using windshield?
Could you post your code?
Tools: Hot Particles, LuaPreprocess, InputField, (more) Games: Momento Temporis
"If each mistake being made is a new one, then progress is being made."
"If each mistake being made is a new one, then progress is being made."
Re: How should I implement physics using windshield?
Code: Select all
Player = {}
-- load functions
function Player:load()
self.x = 100
self.y = 0
self.width = 48
self.height = 48
self.collider = World:newRectangleCollider(self.x * 2, self.y * 2, self.width * 2, self.height * 2)
self.collider:setFixedRotation(true)
self.maxSpeed = 400
self.spriteSheet = love.graphics.newImage("Sprites/Will Ishan Nielsen.png") -- saves sprite sheet
self.grid = anim8.newGrid(24, 24, self.spriteSheet:getWidth(), self.spriteSheet:getHeight()) -- converts sprite sheet into a grid of frames
self.grounded = false
self.xVel = 0
self.yVel = 0
self.scale = 2
Player:animations()
end
function Player:animations() -- loads in animations
local g = self.grid
idle = anim8.newAnimation(g(("1-4"), 1), 0.1)
runAnim = anim8.newAnimation(g(("6-9"), 1), 0.1)
jumpAnim = anim8.newAnimation(g((11), 1), 0.1)
fallAnim = anim8.newAnimation(g((10), 1), 0.1)
currentAnim = idle
end
-- Update functions
function Player:update(dt)
self.xVel, self.yVel = self.collider:getLinearVelocity()
currentAnim:update(dt)
if self.yVel ~= 0 then
self.grounded = false
else
self.grounded = true
end
self:move(dt)
self:skyAnim()
end
function Player:move(dt)
if love.keyboard.isDown("d") and self.xVel < self.maxSpeed then -- moves player to the right
currentAnim = runAnim
self.collider:applyForce(5500, 0)
elseif love.keyboard.isDown("a") and self.xVel > -self.maxSpeed then -- moves player to the left
currentAnim = runAnim
self.collider:applyForce(-5500, 0)
else
currentAnim = idle
end
end
-- miscellaneous
function Player:jump(key) -- controls for jumping
if key == "w" and self.grounded then
self.collider:applyLinearImpulse(0, -10000)
end
end
function Player:facing(key) -- working
if key == "d" then
self.scale = 2
elseif key == "a" then
self.scale = -2
end
end
function Player:skyAnim()
if self.grounded == false then
if self.yVel > 0 then
currentAnim = jumpAnim
elseif self.yVel < 0 then
currentAnim = fallAnim
end
end
end
-- draw function
function Player:draw()
currentAnim:draw(self.spriteSheet, self.x, self.y - self.width / 2, nil, self.scale, 2, 12)
end
This is the player functions
Code: Select all
function love.update(dt)
Player.x = Player.collider:getX() / 2
Player.y = Player.collider:getY() / 2
World:update(dt) -- updates what is happening to the world
Player:update(dt)
end
Last edited by MaxGamz on Fri Oct 28, 2022 1:35 pm, edited 1 time in total.
Re: How should I implement physics using windshield?
This forum doesn't support markdown styling for code, please use:
[code]
-- your code here
[/code]
[code]
-- your code here
[/code]
Re: How should I implement physics using windshield?
I assume the reason the y position changes is because of gravity. The code you posted omits how the World object is created, but I assume the gravity isn't set to zero. And the reason the animation is drawn in the wrong place (I assume that's the problem) is because you grab the player's x and y from the collider before running the physics simulation (World:update()), thus the rendering will be one frame behind the physics.
(I'm assuming a lot here. It would help more if you provided a .love file we could run.)
(I'm assuming a lot here. It would help more if you provided a .love file we could run.)
Tools: Hot Particles, LuaPreprocess, InputField, (more) Games: Momento Temporis
"If each mistake being made is a new one, then progress is being made."
"If each mistake being made is a new one, then progress is being made."
Re: How should I implement physics using windshield?
Is it possible for me to post the file here? Sorry this is my first time using the forums
Re: How should I implement physics using windshield?
There should be a tab for attachments when you write a reply, next to the post options.
(Actually, it's possible newly registered accounts can't add attachments. I don't remember.)
(Actually, it's possible newly registered accounts can't add attachments. I don't remember.)
Tools: Hot Particles, LuaPreprocess, InputField, (more) Games: Momento Temporis
"If each mistake being made is a new one, then progress is being made."
"If each mistake being made is a new one, then progress is being made."
Re: How should I implement physics using windshield?
By the way I took your consideration. The collider and player are moving at the same time now, but I am still having issues with the animation issue.ReFreezed wrote: ↑Fri Oct 28, 2022 2:09 pm I assume the reason the y position changes is because of gravity. The code you posted omits how the World object is created, but I assume the gravity isn't set to zero. And the reason the animation is drawn in the wrong place (I assume that's the problem) is because you grab the player's x and y from the collider before running the physics simulation (World:update()), thus the rendering will be one frame behind the physics.
(I'm assuming a lot here. It would help more if you provided a .love file we could run.)
Re: How should I implement physics using windshield?
Ok, the tiny added y-velocity is probably caused by small rounding errors in the physics library. The reason the animation looks glitchy is because the detection for whether the player is grounded or not isn't very robust. Change the `if self.yVel ~= 0 then` code in Player:update() to something like this:
I.e. ask the library whether the player is colliding with anything or not. This code solves the immediate animation issue but has several other issues, but maybe you can figure out the rest from here.
Code: Select all
if self.collider:enter("Default") then
self.grounded = true
elseif self.collider:exit("Default") then
self.grounded = false
end
Tools: Hot Particles, LuaPreprocess, InputField, (more) Games: Momento Temporis
"If each mistake being made is a new one, then progress is being made."
"If each mistake being made is a new one, then progress is being made."
Who is online
Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 4 guests