Bullet's not working

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
avrame
Prole
Posts: 2
Joined: Sat Jun 06, 2009 2:07 am

Bullet's not working

Post by avrame »

I started playing with löve a few days ago and was surprised how easy and intuitive it is!

So I decided to learn more about it by programming an asteroids clone. Everything has gone smoothly until I tried creating the bullets - they don't move away from the ship, even when I turn the initial velocity on them way up. The bullets do start moving, however, when something bumps into them.

I'm sure I'm just making some obvious and stupid mistake, but I thought I'd post my code up here for help. The bullet code is in the /classes/ship.lua file, and I think the pertinent code is in the "Ship:shoot" function on line 58:

Code: Select all

function Ship:shoot(dt)
    local bullet_shape = love.physics.newCircleShape(self.bullet.body, 5)
    bullet_shape:setData("bullet")
    self.bullet.body:setBullet(true)
    self.bullet.body:setMassFromShapes()

    local radAngle = math.rad(self.body:getAngle())

    local xSpeed = math.sin(radAngle) * 5000
    local ySpeed = -math.cos(radAngle) * 5000

    self.bullet.body:setPosition(self.body:getX() + math.sin(radAngle)*40, self.body:getY() - math.cos(radAngle)*40)
    --self.bullet.body:setAngle(self.body:getAngle())
    self.bullet.body:setVelocity(xSpeed, ySpeed)
end
Here's the full code
loveroids.love.zip
löveroids - an asteroids clone!
(23.54 KiB) Downloaded 223 times
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Bullet's not working

Post by Robin »

Do you know how to make .love files? They are much easier to use for us.

Basically, if you just put the files directly into the zipfile, and rename it .love, we can all directly run it.

I uploaded the .love for you:
Attachments
loveroids.love
(15.99 KiB) Downloaded 206 times
Help us help you: attach a .love.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Bullet's not working

Post by bartbes »

I know why it keeps moving after you've collided with it, it's because you continue to use the same body, while adding more and more shapes. The problem with that makes sense.. the body goes to sleep, either use setSleep when shooting, setAllowSleep when creating or create a body everytime you shoot.
avrame
Prole
Posts: 2
Joined: Sat Jun 06, 2009 2:07 am

Re: Bullet's not working

Post by avrame »

@Robin - sorry 'bout that. I think OS X's built-in archive function keeps the .zip file format invisible until I upload it. I'll try avoiding that in the future.

@bartbes - thanks! That makes sense. I got the bullets working by just making a separate bullet class that creates a new body each time. I'm not sure I understand the setSleep or setAllowSleep concept though.

I'll probably post the game in a few weeks!
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 10 guests