Page 1 of 2

trying to simulate a punch with physics

Posted: Sun Aug 09, 2009 10:20 pm
by clofresh
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}
  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?

Re: trying to simulate a punch with physics

Posted: Mon Aug 10, 2009 2:02 am
by TechnoCat
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)

Re: trying to simulate a punch with physics

Posted: Mon Aug 10, 2009 2:15 am
by TechnoCat
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

Re: trying to simulate a punch with physics

Posted: Mon Aug 10, 2009 2:36 am
by clofresh
Sweet! That works great. Thanks for your help.

Re: trying to simulate a punch with physics

Posted: Mon Aug 10, 2009 5:36 am
by Pliskin09
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.

Re: trying to simulate a punch with physics

Posted: Mon Aug 10, 2009 7:22 am
by TechnoCat
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.

Re: trying to simulate a punch with physics

Posted: Mon Aug 10, 2009 11:35 am
by clofresh
Maybe if the docs were part of the wiki, we could all contribute to making them better, bit by bit.

Re: trying to simulate a punch with physics

Posted: Mon Aug 10, 2009 12:14 pm
by Robin
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.

Re: trying to simulate a punch with physics

Posted: Mon Aug 10, 2009 12:57 pm
by Robin
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.

Re: trying to simulate a punch with physics

Posted: Mon Aug 10, 2009 1:32 pm
by Pliskin09
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.