Page 1 of 1

I want to create a swing traps like pendulum in platformer games. But can't code properly?

Posted: Tue Dec 13, 2022 4:47 am
by kiyo
My platformer games require a spikeball hanging from chain and swinging like pendulum. But, I'm no able to code it properly, specially the collision part. Though I able to animate like pendulum but the collision body still stays in its original position. Can anyone help me in this matter?

Re: I want to create a swing traps like pendulum in platformer games. But can't code properly?

Posted: Tue Dec 13, 2022 10:53 pm
by Bigfoot71
I shouldn't have but it kept me busy coding a bit, something like that?

Image

If this is the case for collisions you can use Kikito's bump library, but as you also want some "physical" effects you can use love.physics with or a higher level API like windfield (there is probably newer and better than winfield).

You can go to the Awesome Löve2d Github page, you will find what you are looking for there, there is no point in recreating the wheel unless you are doing it to learn, in which case there is probably plenty of documentation for what you want to do ;)

Edit: next time please share a code snippet, it would help us understand what you want to do or see where you are wrong :)

Re: I want to create a swing traps like pendulum in platformer games. But can't code properly?

Posted: Wed Dec 14, 2022 12:22 am
by togFox
kiyo wrote: Tue Dec 13, 2022 4:47 am Though I able to animate like pendulum but the collision body still stays in its original position. Can anyone help me in this matter?
Love2D needs to you to update the physical body using whatever motion (pendulum) and then the drawing operation is a different thing and you want them to of course line up on the screen.

It sounds like you're updating the drawing without physically moving the object so look into that if you haven't already.
@bigfoot your gif cycle above runs too long and triggers me. :ultrahappy:

Re: I want to create a swing traps like pendulum in platformer games. But can't code properly?

Posted: Wed Dec 14, 2022 1:12 am
by Bigfoot71
I'm thinking about it thanks to what togFox said and by rereading your post (which I had misinterpreted a little by the way) if you already use love.physics think about updating the world!

Code: Select all

world = love.physics.newWorld( 0,0 )

function love.update(dt)
    world:update(dt)
end
@togFox sorry, my will was not to hypnotize you :crazy:

Re: I want to create a swing traps like pendulum in platformer games. But can't code properly?

Posted: Wed Dec 14, 2022 7:08 pm
by pgimeno
Something like this? (space bar to release)

Code: Select all

local lp = love.physics
local lg = love.graphics

local world = lp.newWorld(0, 100)

local function newObject(body, shape)
  return {body = body, shape = shape, fixture = lp.newFixture(body, shape)}
end

local anchor = newObject(lp.newBody(world, 400, 250, "static"),
 lp.newRectangleShape(10, 10))

local ball = newObject(lp.newBody(world, 550, 250, "static"),
  lp.newCircleShape(10))

local joint = lp.newRopeJoint(anchor.body, ball.body, anchor.body:getX(),
  anchor.body:getY(), ball.body:getX(), ball.body:getY(), 150)

local platform = newObject(lp.newBody(world, 400, 435, "static"),
  lp.newRectangleShape(700, 10))

local object = newObject(lp.newBody(world, 400, 420, "dynamic"),
  lp.newRectangleShape(20, 30))
object.body:setMass(0.1)

function love.keypressed(k)
  if k == "escape" then return love.event.quit() end
  if k == "space" then ball.body:setType("dynamic") end
end

function love.update(dt)
  world:update(dt)
end

function love.draw()
  lg.polygon("fill", anchor.body:getWorldPoints(anchor.shape:getPoints()))
  lg.circle("fill", ball.body:getX(), ball.body:getY(), ball.shape:getRadius())
  lg.line(anchor.body:getX(), anchor.body:getY(), ball.body:getX(),
    ball.body:getY())
  lg.polygon("fill", platform.body:getWorldPoints(platform.shape:getPoints()))
  lg.polygon("line", object.body:getWorldPoints(object.shape:getPoints()))
end

Re: I want to create a swing traps like pendulum in platformer games. But can't code properly?

Posted: Mon Dec 19, 2022 4:32 am
by kiyo
Thank you all