windfield physics help for enter and destroy method
Posted: Mon Nov 06, 2023 5:35 am
I am a newbie.I want to make a simple coin .where ball can have coins.
_G.love = require("love")
function love.load()
w, h = love.graphics.getWidth(), love.graphics.getHeight()
local wf = require("lib.windfield.windfield")
world = wf.newWorld(0, 0, true)
world:setGravity(0, 100)
-- Create the ground
ground = world:newRectangleCollider(0, h - 100, 500, 200)
ground:setType("static")
ball = world:newCircleCollider(100, 0, 20)
coins = {}
score = 0
-- Create initial coins
createCoin(300, h - 150)
createCoin(600, h - 150)
end
function createCoin(x, y)
local coin = world:newCircleCollider(x, y, 10, { "Coin" })
coin.type = "coin"
table.insert(coins, coin)
end
function love.update(dt)
world:update(dt)
local ball_x, ball_y = ball:getPosition()
for i = #coins, 1, -1 do
local coin = coins
if coin:enter("ball") then
-- Coin has been collected
coin:destroy()
table.remove(coins, i)
score = score + 1
end
end
if love.keyboard.isDown("s") then
ball:applyForce(0, 50000)
end
if love.keyboard.isDown("d") then
ball:setLinearVelocity(100, ball_y)
end
if love.keyboard.isDown("w") then
ball:setLinearVelocity(0, -100)
end
end
function love.draw()
world:draw()
love.graphics.print("Score: " .. score, 10, 10)
end
_G.love = require("love")
function love.load()
w, h = love.graphics.getWidth(), love.graphics.getHeight()
local wf = require("lib.windfield.windfield")
world = wf.newWorld(0, 0, true)
world:setGravity(0, 100)
-- Create the ground
ground = world:newRectangleCollider(0, h - 100, 500, 200)
ground:setType("static")
ball = world:newCircleCollider(100, 0, 20)
coins = {}
score = 0
-- Create initial coins
createCoin(300, h - 150)
createCoin(600, h - 150)
end
function createCoin(x, y)
local coin = world:newCircleCollider(x, y, 10, { "Coin" })
coin.type = "coin"
table.insert(coins, coin)
end
function love.update(dt)
world:update(dt)
local ball_x, ball_y = ball:getPosition()
for i = #coins, 1, -1 do
local coin = coins
if coin:enter("ball") then
-- Coin has been collected
coin:destroy()
table.remove(coins, i)
score = score + 1
end
end
if love.keyboard.isDown("s") then
ball:applyForce(0, 50000)
end
if love.keyboard.isDown("d") then
ball:setLinearVelocity(100, ball_y)
end
if love.keyboard.isDown("w") then
ball:setLinearVelocity(0, -100)
end
end
function love.draw()
world:draw()
love.graphics.print("Score: " .. score, 10, 10)
end