How should I implement physics using windshield?

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.
MaxGamz
Party member
Posts: 107
Joined: Fri Oct 28, 2022 3:09 am

How should I implement physics using windshield?

Post by MaxGamz »

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?
User avatar
ReFreezed
Party member
Posts: 612
Joined: Sun Oct 25, 2015 11:32 pm
Location: Sweden
Contact:

Re: How should I implement physics using windshield?

Post by ReFreezed »

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."
MaxGamz
Party member
Posts: 107
Joined: Fri Oct 28, 2022 3:09 am

Re: How should I implement physics using windshield?

Post by MaxGamz »

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
And this is in the main file
Last edited by MaxGamz on Fri Oct 28, 2022 1:35 pm, edited 1 time in total.
Andlac028
Party member
Posts: 174
Joined: Fri Dec 14, 2018 2:27 pm
Location: Slovakia

Re: How should I implement physics using windshield?

Post by Andlac028 »

This forum doesn't support markdown styling for code, please use:
[code]
-- your code here
[/code]
User avatar
ReFreezed
Party member
Posts: 612
Joined: Sun Oct 25, 2015 11:32 pm
Location: Sweden
Contact:

Re: How should I implement physics using windshield?

Post by ReFreezed »

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.)
Tools: Hot Particles, LuaPreprocess, InputField, (more) Games: Momento Temporis
"If each mistake being made is a new one, then progress is being made."
MaxGamz
Party member
Posts: 107
Joined: Fri Oct 28, 2022 3:09 am

Re: How should I implement physics using windshield?

Post by MaxGamz »

Is it possible for me to post the file here? Sorry this is my first time using the forums
User avatar
ReFreezed
Party member
Posts: 612
Joined: Sun Oct 25, 2015 11:32 pm
Location: Sweden
Contact:

Re: How should I implement physics using windshield?

Post by ReFreezed »

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.)
Tools: Hot Particles, LuaPreprocess, InputField, (more) Games: Momento Temporis
"If each mistake being made is a new one, then progress is being made."
MaxGamz
Party member
Posts: 107
Joined: Fri Oct 28, 2022 3:09 am

Re: How should I implement physics using windshield?

Post by MaxGamz »

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.)
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.
MaxGamz
Party member
Posts: 107
Joined: Fri Oct 28, 2022 3:09 am

Re: How should I implement physics using windshield?

Post by MaxGamz »

ReFreezed wrote: Fri Oct 28, 2022 2:49 pm 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.)
Ok I think I'm able to add attachments
Game.love
(75.86 KiB) Downloaded 301 times
User avatar
ReFreezed
Party member
Posts: 612
Joined: Sun Oct 25, 2015 11:32 pm
Location: Sweden
Contact:

Re: How should I implement physics using windshield?

Post by ReFreezed »

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:

Code: Select all

    if self.collider:enter("Default") then
        self.grounded = true
    elseif self.collider:exit("Default") then
        self.grounded = false
    end
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.
Tools: Hot Particles, LuaPreprocess, InputField, (more) Games: Momento Temporis
"If each mistake being made is a new one, then progress is being made."
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot] and 6 guests