How do I attach a circle physics object to a particle?

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.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: How do I attach a circle physics object to a particle?

Post by Robin »

Xoria wrote:Nice analogy.

Can someone lead me to a good sample of sprites being used? The wiki doesn't thoroughly show me how to implement them..
Pick any game from the Projects and Demos forum. ;) (Well, except Space -- that uses vector graphics)

It's basically image = love.graphics.newImage("filename") and in love.draw(): love.graphics.draw(image, x, y).
Help us help you: attach a .love.
Xoria
Citizen
Posts: 61
Joined: Sun Jan 31, 2010 1:24 am

Re: How do I attach a circle physics object to a particle?

Post by Xoria »

Good, but rather than sprite stuff, you showed me image stuff. One last thing. So there isn't a way to seriously manipulate images AND use physics? If so, that's a bit dull. :|
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: How do I attach a circle physics object to a particle?

Post by kikito »

Xoria wrote:So there isn't a way to seriously manipulate images AND use physics? If so, that's a bit dull.
There's a system called love.physics. It handles physics.
Then there's a system called love.graphics. It handles, appropriately, graphics.

They both allow you to "manipulate images and use physics". But you have to tell them what to do. For example:
  • you tell physics: "create a ball". This is done with love.physics.newBody and love.physics.newCircleShape.You will have to have created a "world" first.
  • You ask physics "where is the ball?" and it tells you "x and y". This is done with Body:getPosition
  • You tell graphics "draw a ball in x and y". This can be done in several ways, for example with love.graphics.circle or love.grapics.draw(image, ...)
  • You tell physics: "some time has passed". This is done by invoking world:update
  • You ask physics "where is the ball now?" and it tells you "x2 and y2". (The ball is falling)
  • You tell graphics "draw a ball in x2 and y2".
  • etc
It is a bit difficult at the beginning, but when you get the hang of it, it is quite fun!
When I write def I mean function.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: How do I attach a circle physics object to a particle?

Post by Robin »

Xoria wrote:Good, but rather than sprite stuff, you showed me image stuff.
What do you mean by "sprite"? AFAIK, Images in LÖVE are pretty much the same as sprites.
Help us help you: attach a .love.
treeturtle
Prole
Posts: 11
Joined: Wed Feb 24, 2010 9:45 pm
Location: Michigan

Re: How do I attach a circle physics object to a particle?

Post by treeturtle »

I'm pretty new to both love and lua, was also hoping to use particles with physics, but seeing as thats not possible...

Theoretically would it be possible to spawn "invisible" physics balls from the same place and with the same speed and direction as the particles, and have the balls interact physically with other objects but not other balls?
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: How do I attach a circle physics object to a particle?

Post by kikito »

It will be much easier if you just forget about the particles and use straight images instead.

However, since you are new to lua and LÖVE, maybe you should reconsider your goals; the physics module is kind of complicated. Why don't you start with something a bit simpler? I suggest that you try just pacing some images and control them with lua. When you are able to do so, you will probably understand better how everything works. And then physics will be easier to grasp.
When I write def I mean function.
treeturtle
Prole
Posts: 11
Joined: Wed Feb 24, 2010 9:45 pm
Location: Michigan

Re: How do I attach a circle physics object to a particle?

Post by treeturtle »

Since this is about particles, i though i'd put this here instead of making a new topic:

If you see my code, I'm trying to create a particle system that will start and stop upon the player pressing "Z"
I looked through the particle demo to figure out how to create a particle system and was able to change the demo so that the arrow keys controlled the emitters location which is what i want to do in my current project. however, I now know how to get the particles to move with the player (which i haven't put in this code yet) but I can't seem to actually get the particle effect to show up in my game without using practically all of the particle demo's code? Could someone look this over and see what's wrong? I'm really clueless about all this, sorry :cry:

Code: Select all

function love.load()
	----------------------
	--IMAGES AND GLOBALS--
	----------------------
shooter_right = love.graphics.newImage("shooter.png")
shooter_left = love.graphics.newImage("shooterleft.png")
grass = love.graphics.newImage("grass.png")
cloud = love.graphics.newImage("cloud.png")
x = 300
y = 400
speed = 200

	-----------------
	--PARTICLE FAIL--
	-----------------
	Px =350 
	Py = 350
	
ParticleSystem = love.graphics.newParticleSystem(cloud, 100)
	ParticleSystem:setEmissionRate(100)
	ParticleSystem:setSpeed(200, 0)
	ParticleSystem:setGravity(300, 0)
	ParticleSystem:setSize(1, 1)
	ParticleSystem:setColor(16, 81, 229, 255, 176, 16, 229, 0)
	ParticleSystem:setPosition(Px, Py)
	ParticleSystem:setLifetime(1)
	ParticleSystem:setParticleLife(2)
	ParticleSystem:setDirection(0)
	ParticleSystem:setSpread(.25)
	
	
end

function love.update(dt)
	
	--------------------------
	--FIXED! PLAYER MOVEMENT--
	--------------------------
	
		if love.keyboard.isDown("right") then
			
			function love.draw()
				love.graphics.draw(shooter_right, x, y)
			end
				
				if x <= 660 then
				x = x + (speed*dt)
				end	
		elseif love.keyboard.isDown("left") then
			
			function love.draw()
				love.graphics.draw(shooter_left, x, y)
			end
			
				if x >=5 then
				x = x - (speed*dt)
				end
		end
	----------------------
	--MORE PARTICLE FAIL--
	----------------------
	if love.keyboard.isDown("z") then
		ParticleSystem:setPosition(Px, Py)
		ParticleSystem:start()
	end

	
end

function love.draw()
	love.graphics.draw(grass, 0, 300)                -- GRASS DRAW
	love.graphics.draw(shooter_right, x, y)          -- PLAYER DRAW
	love.graphics.draw(ParticleSystem, Px, Py)        -- PARTICLE DRAW FAIL
	
end
	


P.S. My "ground" image goes away once the player pressed anything, and I'm looking for a good way to make it stay. I've got it to work by slapping it in love.update with the drawing of the player image upon player movement but I'm guessing this is not the most professional method.
I've included the current product for kicks
Attachments
pointlessparticles.love.zip
(253.24 KiB) Downloaded 96 times
FirstTry.zip
Help me!
(241.16 KiB) Downloaded 95 times
Last edited by treeturtle on Mon Mar 01, 2010 1:26 am, edited 1 time in total.
User avatar
bmelts
Party member
Posts: 380
Joined: Fri Jan 30, 2009 3:16 am
Location: Wiscönsin
Contact:

Re: How do I attach a circle physics object to a particle?

Post by bmelts »

Okay, you've got two problems - the particle system isn't showing up, and the ground is going away. Fortunately, both of those are very solvable!

You're almost there with the particle system. You get it to start when the player presses 'z'. But starting a ParticleSystem just gets it ready to go. In order for it to keep track of where all the particles are, it needs to be updated every frame, just like the rest of your game. What you need to do is, inside love.update(dt), call ParticleSystem:update(dt). That way, every frame, your game will tell ParticleSystem to update itself, so that when it's drawn in love.draw(), the particles will be where they need to be.

However, even if you change this, the particles won't be drawn on screen if the player moves at all. Why? Because of your second problem. Take a look at your love.update - notice how you're redefining love.draw() when a key is down? That changes love.draw() from what it is originally to what you declare it as there. You're changing love.draw() dynamically, which is kinda cool, and a very useful trick. But in this case, it's overkill, and because the new love.draw()s only draw the shooter, and not the ground or the particle system, as soon as the player presses left or right, the game will only draw the shooter.

To fix that, remove the love.draw() stuff from inside love.update. Instead, just set a variable that determines whether the player should face right or left. How you do that is up to you - you could use a string, a boolean, or some other identifier that has at least two different states. Then, just check for whether it's in the "left" state or the "right" state inside love.draw(), and draw the appropriate image.

One last thing: love.keyboard.isDown() checks every frame, which is overkill for just starting/stopping a ParticleSystem. I'd recommend taking that part out of love.update, and moving it into a love.keypressed() callback, like so:

Code: Select all

function love.keypressed(key)
    if key == "z" then
        ParticleSystem:setPosition(Px, Py)
        ParticleSystem:start()
    end
end
Hope this helps!

EDIT: One last last thing that I noticed in testing out your code. The particles still won't be drawn on screen, even if you follow all of the above. Because you're trying to draw it outside of the boundaries. When love.graphics.draw() draws a ParticleSystem at a certain point, it offsets it by the ParticleSystem's position - which you've set already, to (Px, Py), or (350, 350). So, when you set its position to (350, 350), and then tell love.graphics.draw to draw it at (350, 350), the end result is you're drawing it at (700, 700) - a location which is outside of the window.

I'll leave it as an exercise to you how you go about fixing this.
treeturtle
Prole
Posts: 11
Joined: Wed Feb 24, 2010 9:45 pm
Location: Michigan

Re: How do I attach a circle physics object to a particle?

Post by treeturtle »

first off, thanks for your response. That was ridiculously more in depth then I'd expect in such a forum, usually people are pretty harsh to noobs :megagrin:
I've fixed my previous problems thanks to you, but I've got another about the particle system. I have it set so when Z is held down, particles will shoot from then end of the players gun (i realize this has no real use,this is just me learning love and lua) but when you let go of pressing Z the particles do not instantly stop like i want them to, they keep coming out for a few seconds before stopping. How do i get them to instantly stop when Z is no longer pressed?

Code: Select all

function love.load()
	----------------------
	--IMAGES AND GLOBALS--
	----------------------
shooter_right = love.graphics.newImage("shooter.png")
shooter_left = love.graphics.newImage("shooterleft.png")
grass = love.graphics.newImage("grass.png")
cloud = love.graphics.newImage("cloud.png")
x = 300
y = 400
speed = 200
fRight = true
	-------------------
	--PARTICLE SYSTEM--
	-------------------
	px = (x + 140)    --Horizontal Location of emitter relative to the player
	py = (y +70)      --Vertical " "
	pDirection = 0
	drawparticles = false
	
pS = love.graphics.newParticleSystem(cloud, 1000)
	pS:setEmissionRate(100)
	pS:setSpeed(300, 0)
	pS:setGravity(50, 0)
	pS:setSize(.5, 1)
	pS:setColor(10, 180, 220, 255, 250, 250, 250, 0)
	pS:setPosition(px, py)
	pS:setLifetime(1)
	pS:setParticleLife(2.5)
	pS:setDirection(pDirection)  --changed in movement controls
	pS:setSpread(0)
	
	
end

function love.update(dt)
	
	------------------------
	--MOVEMENT AND CONTROL--
	------------------------
	
		if love.keyboard.isDown("right") then
			fRight = true
			pDirection = 0     --emission direction set to 0 when facing right
			px = (x+140)
			if x <= 660 then
				x = x + (speed*dt)
			end	
		end
		if love.keyboard.isDown("left") then
			fRight = false
			pDirection = (math.rad(180))  --emission direction set to pi when facing right
			px = (x-10)
			if x >=5 then
				x = x - (speed*dt)
			end
		end
	
	pS:update(dt)
	pS:setDirection(pDirection)
	pS:setPosition(px, py)

	--press Z for particles
	if love.keyboard.isDown("z") then
		drawparticles = true
			if love.keyboard.isDown ("left") or love.keyboard.isDown ("right") then
				if x >= 5 and x <=660 then
					pS:setSpeed(500, 0)
				end
			else
				pS:setSpeed(300, 0)
			end
		pS:start()
	end

end

function love.draw()
	--draw grass
	love.graphics.draw(grass, 0, 300)               
	
	--draw player
	if fRight then
		love.graphics.draw(shooter_right, x, y)
	else
		love.graphics.draw(shooter_left, x, y)
	end
	
	--draw particles (granted z is pressed)
	if drawparticles then
		love.graphics.draw(pS, 0 , 0 )                    
	end
end


Attachments
makingprogress.love.zip
(243.74 KiB) Downloaded 95 times
User avatar
TechnoCat
Inner party member
Posts: 1611
Joined: Thu Jul 30, 2009 12:31 am
Location: Denver, CO
Contact:

Re: How do I attach a circle physics object to a particle?

Post by TechnoCat »

treeturtle wrote:How do i get them to instantly stop when Z is no longer pressed?

Code: Select all

pS:setLifetime(1)
Make that number smaller.

Code: Select all

pS:setLifetime(0.01)
seems to work.
Last edited by TechnoCat on Sat Feb 27, 2010 9:16 pm, edited 2 times in total.
Post Reply

Who is online

Users browsing this forum: No registered users and 63 guests