Long time lurker, first time poster. I've been working on a beat-em-up game and I've been having trouble simulating a punch using physics bodies. So I have people represented as bodies with rectangle shapes of proportional mass. I tried to create a fist as a separate body with a circle shape that's attached to the player body with a prismatic joint as the arm. To initiate a punch, you press space which applies an impulse to the fist body. I'm not sure if I'm doing things wrong, but the player just disappears when I apply the impulse. If I comment out the arm joint, it works like I want it to, except that the fist doesn't return to the player.
Here's some code:
Code: Select all
function load()
player_dimensions = {50, 100}
player_start_position = {100, 100}
enemy_dimensions = {50, 100}
enemy_start_position = {250, 100}
fist_radius = 10
world = love.physics.newWorld(love.graphics.getWidth(), love.graphics.getHeight())
player_body = love.physics.newBody(world, unpack(player_start_position))
player_shape = love.physics.newRectangleShape(player_body, unpack(player_dimensions))
enemy_body = love.physics.newBody(world, unpack(enemy_start_position))
enemy_shape = love.physics.newRectangleShape(enemy_body, unpack(enemy_dimensions))
fist_body = love.physics.newBody(world, player_start_position[1] + 75, player_start_position[2] + 10)
fist_shape = love.physics.newCircleShape(fist_body, fist_radius)
arm = love.physics.newPrismaticJoint(player_body, fist_body, 50, 10, 125, 10)
end
function update(dt)
if love.keyboard.isDown(love.key_space) then
fist_body:applyImpulse(500, 0)
end
world:update(dt)
end
function draw()
love.graphics.rectangle(love.draw_fill, player_body:getX(), player_body:getY(), unpack(player_dimensions))
love.graphics.circle(love.draw_fill, fist_body:getX(), fist_body:getY(), fist_radius)
love.graphics.rectangle(love.draw_fill, enemy_body:getX(), enemy_body:getY(), unpack(enemy_dimensions))
end
function keypressed(key)
if key == love.key_tab then
love.system.restart()
end
end
Any suggestions? Is there a better way for me to model a punch?