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

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
User avatar
kiyo
Prole
Posts: 19
Joined: Thu Dec 08, 2022 12:49 pm

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

Post 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?
User avatar
Bigfoot71
Party member
Posts: 287
Joined: Fri Mar 11, 2022 11:07 am

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

Post 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 :)
Attachments
main.lua
I did not integrate the collisions there for lack of time but I told you how to do it simply.
(2.45 KiB) Downloaded 64 times
My avatar code for the curious :D V1, V2, V3.
User avatar
togFox
Party member
Posts: 835
Joined: Sat Jan 30, 2021 9:46 am
Location: Brisbane, Oztralia

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

Post 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:
Last project:
https://togfox.itch.io/hwarang
A card game that brings sword fighting to life.
Current project:
Idle gridiron. Set team orders then idle and watch: https://togfox.itch.io/pad-and-pencil-gridiron
User avatar
Bigfoot71
Party member
Posts: 287
Joined: Fri Mar 11, 2022 11:07 am

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

Post 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:
My avatar code for the curious :D V1, V2, V3.
User avatar
pgimeno
Party member
Posts: 3691
Joined: Sun Oct 18, 2015 2:58 pm

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

Post 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
User avatar
kiyo
Prole
Posts: 19
Joined: Thu Dec 08, 2022 12:49 pm

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

Post by kiyo »

Thank you all
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 1 guest