trying to simulate a punch with physics

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.
clofresh
Citizen
Posts: 87
Joined: Sun Jul 26, 2009 4:21 pm
Contact:

trying to simulate a punch with physics

Hi,

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}

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?
Last edited by clofresh on Mon Aug 10, 2009 3:24 pm, edited 1 time in total.
----------------------------------------
Sluicer Games

TechnoCat
Inner party member
Posts: 1611
Joined: Thu Jul 30, 2009 12:31 am
Location: Denver, CO
Contact:

Re: trying to simulate a punch with physics

I don't quite understand PrismaticJoint either, but I was able to get a buggy result by changing

Code: Select all

arm = love.physics.newPrismaticJoint(player_body, fist_body, 50, 10, 125, 10)
into

Code: Select all

arm = love.physics.newPrismaticJoint(player_body, fist_body, 50, 10, 90, 0)

TechnoCat
Inner party member
Posts: 1611
Joined: Thu Jul 30, 2009 12:31 am
Location: Denver, CO
Contact:

Re: trying to simulate a punch with physics

Okay, so apparently ax and ay are floats to enable x-axis translations and y-axis translations.
Try this out

Code: Select all

arm = love.physics.newPrismaticJoint(player_body, fist_body, 0, 0, 1, 0)
-- arm:setMotorEnabled(true)
-- arm:setMaxMotorForce(200)
-- arm:setMotorSpeed(100)
arm:setLimitsEnabled(true)
arm:setLimits(0,20)
EDIT: as a bonus add this to your update for a punch effect. It doesn't really work in any situation except this simple punch example.

Code: Select all

local fistTx = arm:getTranslation()
if fistTx>0 then
local fistVx, fistVy=fist_body:getVelocity()
fist_body:setVelocity(fistVx-500*dt,fistVy)
end

clofresh
Citizen
Posts: 87
Joined: Sun Jul 26, 2009 4:21 pm
Contact:

Re: trying to simulate a punch with physics

Sweet! That works great. Thanks for your help.
----------------------------------------
Sluicer Games

Pliskin09
Citizen
Posts: 89
Joined: Fri Jul 24, 2009 8:30 am

Re: trying to simulate a punch with physics

the doc needs way better explanations for these things. its whats stopping me from attempting the physics part of love. not sure how to start etc. i can look at basic examples but the more advanced functions arent explained.

TechnoCat
Inner party member
Posts: 1611
Joined: Thu Jul 30, 2009 12:31 am
Location: Denver, CO
Contact:

Re: trying to simulate a punch with physics

Pliskin09 wrote:the doc needs way better explanations for these things. its whats stopping me from attempting the physics part of love. not sure how to start etc. i can look at basic examples but the more advanced functions arent explained.
Yeah, I know what you mean. It ends up being a lot of trial and error. And often times seems like just error.

clofresh
Citizen
Posts: 87
Joined: Sun Jul 26, 2009 4:21 pm
Contact:

Re: trying to simulate a punch with physics

Maybe if the docs were part of the wiki, we could all contribute to making them better, bit by bit.
----------------------------------------
Sluicer Games

Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: trying to simulate a punch with physics

clofresh wrote:Maybe if the docs were part of the wiki, we could all contribute to making them better, bit by bit.
This came up in another thread as well (that was some time ago, though). rude didn't really like the idea, and wanted to auto-generate documentation. Of course, for examples, tutorials and detailed information the wiki might be perfect. If we would actually start using the wiki, that is.
Help us help you: attach a .love.

Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: trying to simulate a punch with physics

All right, I started a page at the wiki: Tutorial:Physics

Not much at the moment, but if we all would contribute our two cents, it might actually, you know, useful.
Help us help you: attach a .love.

Pliskin09
Citizen
Posts: 89
Joined: Fri Jul 24, 2009 8:30 am

Re: trying to simulate a punch with physics

rude should look at thecodeigniter framework's "user guide" (pretty much the same as our documentation). its so basic but it just tells you everything you need to know. love how they give all those small examples, regardless of how 'pointless' they might be.

Who is online

Users browsing this forum: Bing [Bot] and 31 guests