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.
osuf oboys
Party member
Posts: 215
Joined: Sun Jan 18, 2009 8:03 pm

Re: newBody() *EDIT* And Collisions!

Post by osuf oboys »

lbhoward wrote: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.
Odd way for a physics engine to work but you can just set velocity to 0 or do as skofo suggested, but that's how damping works. What do you use for propelling the craft?
If I haven't written anything else, you may assume that my work is released under the LPC License - the LÖVE Community. See http://love2d.org/wiki/index.php?title=LPC_License.
lbhoward
Prole
Posts: 23
Joined: Tue Feb 17, 2009 6:12 pm

Re: newBody() *EDIT* And Collisions!

Post by lbhoward »

I use setVelocity to propel the craft, and when I call for setVelocity(0, 0) when the left/right key is released - the ship just carries on at the same speed.
osuf oboys
Party member
Posts: 215
Joined: Sun Jan 18, 2009 8:03 pm

Re: newBody() *EDIT* And Collisions!

Post by osuf oboys »

lbhoward wrote:I use setVelocity to propel the craft, and when I call for setVelocity(0, 0) when the left/right key is released - the ship just carries on at the same speed.
Impossible. Something else must be changing the velocity after that. Try disabling all other places changing velocity or applying impulses or forces. Make sure that the setting of the velocity from having the keys down cannot be applied after the setting of the velocity to 0.
If I haven't written anything else, you may assume that my work is released under the LPC License - the LÖVE Community. See http://love2d.org/wiki/index.php?title=LPC_License.
lbhoward
Prole
Posts: 23
Joined: Tue Feb 17, 2009 6:12 pm

Re: newBody() *EDIT* And Collisions!

Post by lbhoward »

This is all that is going on to effect the Velocity/Impulse - nowhere else in the code makes reference to the movement. No matter what it drifts off into space at the same speed.

Code: Select all

function keypressed(key)

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

end

function keyreleased(key)

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

end

Bah! And now collisions errors - LOVE's physics system utterly confuses this programmer O.o

Code: Select all

 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" 
	
	if a == Player and b == Vexxer_one then
	Vexxer_one:destroy()
	vexxer_one:destroy()
	vexxer_one_box:destroy()
	end
  
 end
Vexxer_one is the 'String' or so I understand.

vexxer_one is the body.
vexxer_one_box is the hitbox (shape attached to the body) yet when I collide into it (the collision is recognized) but the 'Vexxer' just stays there. Am I doing something wrong (Ha, I'm doing everything wrong today!)
User avatar
Skofo
Party member
Posts: 146
Joined: Mon Dec 22, 2008 10:55 pm

Re: newBody() *EDIT* And Collisions!

Post by Skofo »

It might be easier if you posted a LOVE file.
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 »

*EDIT* Linky to the file loaded up onto my Filefront http://files.filefront.com/starfoxlove/ ... einfo.html


Sorry it took so long to post this... I sort of... erm *hides*


Made it all case insensitive. Yup had to go through and replace every filename, all fixed and here is the file! Hope it helps - just hit ENTER on Start Game and you can use LEFT/RIGHT to move - SPACE just fires a harmless graphic up into the air which un-draws when it reaches the top.

I am very much unsure of how exactly to destroy the 'b' object (should be the enemy) on a collision - and to reduce lets say - a variable 'player_health' by say 10 on a collision with an enemy. In fact I am very much confused about the whole physics system. This is my first time working with any kind of real physics - I'm used to coding emulated stuff like. 'If player_x >= enemy_x + 30 and player_x <= enemy_x -30' creating sort of 'emulated' hit boxes, infact I may just give up on physics alltogether and do it this way >.<
User avatar
Skofo
Party member
Posts: 146
Joined: Mon Dec 22, 2008 10:55 pm

Re: newBody() *EDIT* And Collisions!

Post by Skofo »

I feex et! :ultraglee:
level_one.lua
(8.35 KiB) Downloaded 136 times
I removed setVelocity/applyImpulse and replaced it with applyForce, then removed the keypress/keyrelease functions and replaced em with a keydown check in the update function so it continuously applies the force when a key is down, as opposed to doing it only once when a key is pressed a la the keypress function. You can change the numbers in applyForce to adjust the speed and change the number in setDamping to adjust the friction.

Have fun! ^^

P.S. Neato game! :ultrashocked:: Looks like you already did a buncha work on it. Nice job! :neko:
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 »

Amazing! Thank you very much for the support you have given with this issue. I am always very cautious what I put in Update - as I once put a love.audio.play in there and it tried playing 1 instance of the sound every update >.< All I need to do now is get collisions to destroy 'b' and I can start building the level.

Thanks to everyone for their assistance!

And on one final note to this thread - if someone could give a very basic example of the destroy function it would be much appreciated.

I initiate vexxer_one the body.
I initiate vexxer_one_box the shape, with a setData of Vexxer_one .

I use a callback of collision(a, b ,c).

How do I use an if statement so that 'if something collides with vexxer_one_box - blow it up' my 'if b == Vexxer_one' doesn't seem to want to work - am I doing something wrong here =P I've tried 'if b == vexxer_one_box' too.

*EDIT* I've made a bit of progress with the destroy function - I call

Code: Select all

 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() 
	
	bCollided = b
	aCollided = a
  
    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
And in update(dt)

Code: Select all

   if bCollided == 'Vexxer_one' and aCollided == 'Player' then
   Vexxer_one:destroy()
   vexxer_one:destroy()
   vexxer_one_box:destroy()
   end
My only problem is I get a 'Attempt to index global' error. >.< This is the last aspect of LOVE I do not fully understand, so anyone who fills me in the correct usage gets a cookie and my LOVE :nyu:
Post Reply

Who is online

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