newBody() *EDIT* And Collisions! *EDIT* And destroy()!

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.
lbhoward
Prole
Posts: 23
Joined: Tue Feb 17, 2009 6:12 pm

newBody() *EDIT* And Collisions! *EDIT* And destroy()!

Post by lbhoward »

Can anybody see any reason for this not to work? I'm certain it's just another silly rookie error on my part. But I get
No matching function for overloaded 'newBody()'
it refers to the second newBody().

Code: Select all

function load()
   world = love.physics.newWorld(800, 500)
   player = love.physics.newBody(world, arwing_x, arwing_y)
   player_box = love.physics.newRectangleShape(player, 60, 60)
   player_box:setData("Player")

   vexxer_one = love.physics.newBody(world, vexxer_one_x, vexxer_one_y)
   vexxer_one_box = love.physics.newRectangleShape(vexxer_one, 60, 60)
   vexxer_one_box:setData("Vexxer_one")

   vexxer_one_x = 300
   vexxer_one_y = 300

   arwing_x = 388
   arwing_y = 400

end
I'm also noticing that it is messing with my first body and forcing it up into the top left corner.
Last edited by lbhoward on Sun Feb 22, 2009 1:00 pm, edited 2 times in total.
User avatar
Xcmd
Party member
Posts: 211
Joined: Fri Feb 13, 2009 10:45 pm

Re: newBody()

Post by Xcmd »

Code: Select all

function load()
   vexxer_one_x = 300
   vexxer_one_y = 300

   arwing_x = 388
   arwing_y = 400
   
   world = love.physics.newWorld(800, 500)
   
   player = love.physics.newBody(world, arwing_x, arwing_y)
   player_box = love.physics.newRectangleShape(player, 60, 60)
   player_box:setData("Player")

   vexxer_one = love.physics.newBody(world, vexxer_one_x, vexxer_one_y)
   vexxer_one_box = love.physics.newRectangleShape(vexxer_one, 60, 60)
   vexxer_one_box:setData("Vexxer_one")
end
Never try to call variables that don't already exist... or, rather, that don't already have values.
We don't borrow, we don't read, we don't rent, we don't lease, we take the minds!
lbhoward
Prole
Posts: 23
Joined: Tue Feb 17, 2009 6:12 pm

Re: newBody()

Post by lbhoward »

Doh! Another silly mistake on my part - thanks very much for pointing that out to me.

EDIT: I have a more 'Collision' based issue that I will just add to this thread.

I have set up my world - it all runs ok with no errors. I use a 'body:setX(body:getX + 5)' on keyDown.love.key_right etc to move my bodies/shapes. However collisions just will not detect. I am missing something? I tried adding mass and gravity but to no prevail. Just a quick note - I've left some Variables out of the code posted here so you can see just what I have effecting the world - rest assured that any variable you see has been initiliased.

Code: Select all

 text = "No collision yet." 
function load()
   world = love.physics.newWorld(800, 500)

   player = love.physics.newBody(world, 388, 400, 5)
   player_box = love.physics.newRectangleShape(player, 100, 100)
   player_box:setData("Player")
   arwing_eng_emit_x = player:getX()
   arwing_eng_emit_y = player:getY() + 30

   vexxer_one = love.physics.newBody(world, vexxer_one_x, vexxer_one_y, 5)
   vexxer_one_box = love.physics.newRectangleShape(vexxer_one, 100, 100)
   vexxer_one_box:setData("Vexxer_one")

  world:setCallback(collision) 

end


function draw()
--- Just moves my 'Engine Wash' emitter with the ship
   arwing_eng_emit_x = player:getX()
   arwing_eng_emit_y = player:getY() + 30
--- handles the ships movement
   if love.keyboard.isDown(love.key_left) and canmoveleft == 1 and level_one == 1 then
   player:setX(player:getX() - 5)
   arwing_eng_emit_x = arwing_eng_emit_x -5
   elseif love.keyboard.isDown(love.key_right) and canmoveright == 1 and level_one == 1 then
   player:setX(player:getX() + 5)
   arwing_eng_emit_x  = arwing_eng_emit_x + 5
   end
end

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


 function collision(a, b, c) 
     
    local f, r = c:getFriction(), c:getRestitution() 
    local s = c:getSeparation() 
    local px, py = c:getPosition() 
    local vx, vy = c:getVelocity() 
    local nx, ny = c:getNormal() 
  
    text = "Last Collision:\n" 
    text = text .. "Shapes: " .. a .. " and " .. b .. "\n" 
    text = text .. "Position: " .. px .. "," .. py .. "\n" 
    text = text .. "Velocity: " .. vx .. "," .. vy .. "\n" 
    text = text .. "Normal: " .. nx .. "," .. ny .. "\n" 
    text = text .. "Friction: " .. f .. "\n" 
    text = text .. "Restitution: " .. r .. "\n" 
    text = text .. "Separation: " .. s .. "\n" 
  
 end
Ideally I just want to check postions - if the hitboxes overlap etc, I don't really want them to block movement or check speeds. No Collision is detected - none that I can tell. Part of me is concerned I've haven't set the hitboxes up, as I'm aware there should be 4 variables for a shape? the x,y of where it is draw, and then the size of the box in x,y - I did try

Code: Select all

   player_box = love.physics.newRectangleShape(player, player:getX(), player:getY(), 100, 100)
But then everything just stopped moving.
User avatar
Skofo
Party member
Posts: 146
Joined: Mon Dec 22, 2008 10:55 pm

Re: newBody() *EDIT* And Collisions!

Post by Skofo »

If you just reset the position of your bodies when you want them to move, they're essentially teleporting, so they don't check for collisions and stuff.

If you want your objects to move and check for collisions, better things to use would be applyForce, applyImpulse or setVelocity. Which of these you should use depends on what you want to do.

Have fun! ^^
Working on: Viator
Need a 64-bit Debian package for LÖVE? Here it is!
lbhoward
Prole
Posts: 23
Joined: Tue Feb 17, 2009 6:12 pm

Re: newBody() *EDIT* And Collisions!

Post by lbhoward »

Colisions now work! Or they are atleast detected - which I can work from - I just have a bit of an issue with the new method of making my ships move - it works ok (though the numbers are strange to work with :P

I've implemented this

Code: Select all

function keypressed(key)
   if key == love.key_left and canmoveleft == 1 and level_one == 1 then
   player:applyImpulse(-5000, 0)
   end

end

function keyreleased(key)
   if key == love.key_left and level_one == 1 then
   player:applyImpulse(5000, 0)
   end
end
Oddly my keyrelease doesn't 'reset' the impulse - and so my ship just carries on drifting in space, I am going about this the wrong way?
User avatar
Skofo
Party member
Posts: 146
Joined: Mon Dec 22, 2008 10:55 pm

Re: newBody() *EDIT* And Collisions!

Post by Skofo »

Perhaps you could use setDamping on your bodies.

Explanation here: http://www.box2d.org/manual.html#d0e635
Working on: Viator
Need a 64-bit Debian package for LÖVE? Here it is!
lbhoward
Prole
Posts: 23
Joined: Tue Feb 17, 2009 6:12 pm

Re: newBody() *EDIT* And Collisions!

Post by lbhoward »

Latest attempt, to no prevail. The ship still carries on drifting into nowhere. Could my keyrelease function perhaps not be working correctly?

Code: Select all

function keypressed(key)

   if key == love.key_left and level_one == 1 then
   player:setVelocity(-5000, 0)
   player:setDamping(0)
   end
   if key == love.key_right and level_one == 1 then
   player:setVelocity(5000, 0)
   player:setDamping(0)
   end

end

function keyreleased(key)

   if key == love.key_left and level_one == 1 then
   player:setDamping(5000)
   end
   if key == love.key_right and level_one == 1 then
   player:setDamping(5000)
   end

end
User avatar
Skofo
Party member
Posts: 146
Joined: Mon Dec 22, 2008 10:55 pm

Re: newBody() *EDIT* And Collisions!

Post by Skofo »

I think you're required to set a body's damping only once, like you would with weight or size.

P.S. I'm sorry if anything I say is wrong, I haven't done that much work with Box2D. :? Someone really needs to make some examples...
Working on: Viator
Need a 64-bit Debian package for LÖVE? Here it is!
lbhoward
Prole
Posts: 23
Joined: Tue Feb 17, 2009 6:12 pm

Re: newBody() *EDIT* And Collisions!

Post by lbhoward »

It appears Damping needs a value between 0.0 and 1.0 and it seems to sort of apply a 'slow brake' effect, where it's speed decreases slightly. By declaring that 'player:setDamping(0.5)' on load - we get a nice 'decrease in speed' but by the time my ship comes to a hault it's already at the otherside of the map.

When I put it to 0.7 - it just refuses to move - 0.6 is the maximum it seems to take and even that causes it to drift to the other side of the map - perhaps Damping isn't the answer =/

Is there anyone out there who succesfully implemented a keypressed feature where on release the velocity resets perfectly and their ship comes to a standstill.
User avatar
Skofo
Party member
Posts: 146
Joined: Mon Dec 22, 2008 10:55 pm

Re: newBody() *EDIT* And Collisions!

Post by Skofo »

Hmm, sorry.

If you're able to open FLA files, perhaps you could look at the source code of this: http://www.box2d.org/forum/viewtopic.php?f=8&t=1547

Another idea is to perhaps divide the velocity every frame, like:

Code: Select all

function update()
local vx, vy = player:getVelocity()
player:setVelocity(vx/6, vy/6)
end
But other than that I don't know how to help you, sorry. :( I wish you the best of luck!
Working on: Viator
Need a 64-bit Debian package for LÖVE? Here it is!
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 4 guests