Windfield applyLinearImpulse causes error???

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
ThatBoiNavy
Prole
Posts: 15
Joined: Fri Jul 05, 2024 6:39 pm

Windfield applyLinearImpulse causes error???

Post by ThatBoiNavy »

I am using windfield (which is a popular Love2d physics library) for my game everything about it works except applyLinearImpulse. applyLinearImpulse makes an object move in a direction but also applying gravity and velocity. But for me, when ever I press W(The button that calls applyLinearImpulse) to make my character jump. It comes up with this error:

Error

main.lua:124 attempt to call method 'applyLinearImpulse' (A nil value)


Traceback

[love "callbacks.lua"]:228: in function 'handler'
main.lua:124: in function <main.lua:124>
[love "callbacks.lua"]:154: in function <[love "callbacks.lua"]:144>
[C]: in function 'xpcall'

I don't have a "callbacks.lua" in my workspace and i'm not sure why this only happens for applyLinearImpulse. Also here is my code for main.lua:

function love.load()

anim8 = require 'libraries/anim8'
camera = require 'libraries...camera'
wf = require 'libraries/windfield'
world = wf.newWorld(0, 5000)
-- Declare open source code above this line /\
love.graphics.setDefaultFilter('nearest', 'nearest') -- Makes the images look crisper and not blurry
cam = camera()
british = {}
british.x = 400
british.y = 200
british.speed = 140
british.collider = world:newBSGRectangleCollider(british.x, british.y, 50, 39, 5)
british.collider:setFixedRotation(true)

map = love.graphics.newImage("Maps/Plains map.png")

british.spriteSheet = love.graphics.newImage("sprites/British musketeer1-sheet-sheet.png")
british.grid = anim8.newGrid(50, 40, british.spriteSheet:getWidth(), british.spriteSheet:getHeight())
british.animations = {}

british.animations.walkRight = anim8.newAnimation(british.grid('1-10', 1), 0.15)

british.animations.walkLeft = anim8.newAnimation(british.grid('50-40', 1), 0.15)

british.animations.jumpRight = anim8.newAnimation(british.grid('12-16', 1), 0.15)

british.animations.jumpLeft = anim8.newAnimation(british.grid('36-40', 1), 0.15)

british.animations.idleRight = anim8.newAnimation(british.grid('1-1', 1), 0.15)
british.animations.idleLeft = anim8.newAnimation(british.grid('50-50', 1), 0.15)

british.animations.fireRight = anim8.newAnimation(british.grid('17-18', 1), 0.15)

british.anim = british.animations.walkRight

local ground = world:newRectangleCollider(0, 480, 800, 120)
ground:setType('static')
ground.y = 120
end

function love.update(dt)
local ismoving = false
local vx = 0 -- velocity of x
local vy = 0 -- velocity of y

if love.keyboard.isDown("d") then
vx = british.speed
british.anim = british.animations.walkRight
ismoving = true
end
if love.keyboard.isDown("a") then
vx = british.speed * -1 --Turns the velocity negitive to move left
british.anim = british.animations.walkLeft
ismoving = true
end
if love.keyboard.isDown("s") then
vy = british.speed
end
-- if love.keyboard.isDown("w") and canjump == true then
--vy = british.speed * -3 --Turns the velocity negitive to jump up
-- british:applyForce(0, -british.speed * 3)
-- british.anim = british.animations.jumpRight
-- end
if love.keyboard.isDown("space") then
british.anim = british.animations.fireRight
end

british.collider:setLinearVelocity(vx, vy)

if ismoving == false then
british.anim:gotoFrame(1)
end

if british.y < 400 then
canjump = false
elseif british.y > 400 then
canjump = true
end

if canjump == false then
vy = british.speed * 4
end

british.anim:update(dt)
world:update(dt)
british.x = british.collider:getX()
british.y = british.collider:getY()

cam:lookAt(british.x, british.y)

local w = love.graphics.getWidth()
local h = love.graphics.getHeight()

if cam.x < w/2 then
cam.x = w/2
end
if cam.y < h/2 then
cam.y = h/2
end

local mapW = 800
local mapH = 600

if cam.x > (mapW - w/2) then
cam.x = (mapW - w/2)
end
if cam.y > (mapH - h/2) then
cam.y = (mapH - h/2)
end
end

function love.draw()
cam:attach()
love.graphics.draw(map, 0, 0)
british.anim:draw(british.spriteSheet, british.x, british.y, nil, nil, nil, 16.5, 19.5)
world:draw()
cam:detach()
end

function love.keypressed(key)
if key == 'w' then
british:applyLinearImpulse(1000, 0)
end
end
User avatar
pgimeno
Party member
Posts: 3655
Joined: Sun Oct 18, 2015 2:58 pm

Re: Windfield applyLinearImpulse causes error???

Post by pgimeno »

I don't know the Windfield library, but is it possible that you have to use british.collider:applyLinearImpulse instead of british:applyLinearImpulse?
User avatar
dusoft
Party member
Posts: 634
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

Re: Windfield applyLinearImpulse causes error???

Post by dusoft »

Windfield is outdated, the repo was archived in 2021 and unmaintained.

This is a possible replacement or you can use built-in love.physics:
https://github.com/HDictus/breezefield
ThatBoiNavy
Prole
Posts: 15
Joined: Fri Jul 05, 2024 6:39 pm

Re: Windfield applyLinearImpulse causes error???

Post by ThatBoiNavy »

dusoft wrote: Wed Jul 24, 2024 4:17 pm Windfield is outdated, the repo was archived in 2021 and unmaintained.

This is a possible replacement or you can use built-in love.physics:
https://github.com/HDictus/breezefield
Thanks I will try breezefield!
Rigachupe
Party member
Posts: 100
Joined: Fri Jun 18, 2021 11:21 am

Re: Windfield applyLinearImpulse causes error???

Post by Rigachupe »

From the quick glance the applyLinearImpulse is not defined in windfield. So that means it is maybe just inside the love.physics here : https://www.love2d.org/wiki/Body:applyLinearImpulse

From that i deducted that you miss a body definition in the code of yours? Not much fan of physics in love2D so far.

Let's look at this example from wiki page:
love.physics.setMeter(40) -- meter is 40 px/units
world = love.physics.newWorld(0, 9.81*40, true) -- new world with gravity
body1 = love.physics.newBody(world, 250, 300, "static") -- new static body at x=250, y=300
body2 = love.physics.newBody(world, 251, 100, "dynamic") -- new dynamic body at x=251, y=100

A body is something that has weight, density and so on.
A shape is something that describes how the thing looks like a circle, rectangle.
Here is a good example : https://www.love2d.org/wiki/Tutorial:Physics
Post Reply

Who is online

Users browsing this forum: Amazon [Bot], Bing [Bot], Bondrusiek, Google [Bot] and 0 guests