Page 1 of 2

Help with Bow&Arrow

Posted: Mon Dec 24, 2012 12:48 pm
by ixjackinthebox
Well I am trying to think how I would make the Arrow move

I don't really know how to go about making it but I guess I would need to use

Code: Select all

table.insert
table.remove
to add in the new arrows
and I would also need to get the direction between the player and the mouse

So how can I go about making this ANY info is appreciated

Also I am using love.physics for pretty much everything

My love file

Re: Help with Bow&Arrow

Posted: Mon Dec 24, 2012 12:59 pm
by Roland_Yonaba
ixjackinthebox wrote: I don't really know how to go about making it but I guess I would need to use

Code: Select all

table.insert
table.remove
This might help.
ixjackinthebox wrote: and I would also need to get the direction between the player and the mouse
My love file
Let mx, my be the actual mouse position
let x,y be the player position.
This will return the (clockwise) angle between the player and the mouse, in radians

Code: Select all

local angle = math.atan2(my - y, mx - x)
Seen in this very helpful angle visualizer, from BlackBulletIV.
Also featured on the wiki.

Re: Help with Bow&Arrow

Posted: Mon Dec 24, 2012 9:23 pm
by ixjackinthebox
Can someone tell me why this is wrong

Code: Select all

function love.mousepressed( MX, MY, button )
	--Arrow
	if button == "l" then
		Table_pos = Table_pos + 1
		table.insert(Objects.Arrow.body, Table_pos, love.physics.newBody( World, ArrowX, ArrowY, "dynamic"))
		table.insert(Objects.Arrow.shape, Table_pos, love.physics.newRectangleShape( 10, 3 ))
		table.insert(Objects.Arrow.fixture, Table_pos, love.physics.newFixture( Objects.Arrow.body , Objects.Arrow.shape, 1 ))
		--ArrowDraw = 1
			--Objects.Arrow.body:applyForce( MX, MY )
	
	end
end
I don't really know to set up the tables so it will create a new arrow and be able to show and hide the arrows individually

Edit:Can someone please tell me how to set the tables up properly.

Re: Help with Bow&Arrow

Posted: Fri Dec 28, 2012 1:09 am
by ixjackinthebox
Anyone...?

Re: Help with Bow&Arrow

Posted: Fri Dec 28, 2012 10:40 am
by Ubermann
You really no need a variable to save the number of elements in the table. You can use table.getn to obtain it.

I even recommend using this method because a variable could be modified wrong in your code and would lead to errors very hard to detect.

Other than that, I can't see anything wrong with your code, assuming that every variable is working as expected.

Re: Help with Bow&Arrow

Posted: Fri Dec 28, 2012 11:24 am
by bartbes
Ubermann wrote:You can use table.getn to obtain it.
getn is deprecated and should not be used, I think it's even been removed for 5.2. Use the # operator instead.

Re: Help with Bow&Arrow

Posted: Fri Dec 28, 2012 1:40 pm
by buhb11
This will not help you solving the problem you got but you may want to look into your code cuz i get only 7fps on my leptop :(

Re: Help with Bow&Arrow

Posted: Fri Dec 28, 2012 2:38 pm
by Ubermann
bartbes wrote:
Ubermann wrote:You can use table.getn to obtain it.
getn is deprecated and should not be used, I think it's even been removed for 5.2. Use the # operator instead.
Oh, didn't know, Thanks for the tip.

Re: Help with Bow&Arrow

Posted: Sat Dec 29, 2012 11:53 am
by ixjackinthebox
What I want to try and do is on mouse press create a table like this one.

Code: Select all

        Objects.Arrow = {}
	Objects.Arrow.body = love.physics.newBody( World, ArrowX, ArrowY, "dynamic")
	Objects.Arrow.shape = love.physics.newRectangleShape( 10, 3 )
	Objects.Arrow.fixture = love.physics.newFixture( Objects.Arrow.body , Objects.Arrow.shape, 1 )
	Objects.Arrow.fixture:setUserData("Arrow")
But I would also need to be able to draw the arrow only after it has been shot and then remove the arrow when it goes out the games area.
But the thing I need help with how the tables should be done
This will not help you solving the problem you got but you may want to look into your code cuz i get only 7fps on my leptop :(
That might be because it uses love.physics for all of the controls

Re: Help with Bow&Arrow

Posted: Sat Dec 29, 2012 12:43 pm
by Santos
Maybe something like this?

A table to store the arrows.

Code: Select all

arrows = {}
Creating a local arrow table, and then inserting it into the table of arrows.

Code: Select all

function create_arrow(x, y)
	local arrow = {}
	arrow.body = love.physics.newBody( World, x, y, "dynamic")
	arrow.shape = love.physics.newRectangleShape( 10, 3 )
	arrow.fixture = love.physics.newFixture( arrow.body , arrow.shape, 1 )
	arrow.fixture:setUserData("Arrow")

	table.insert(arrows, arrow)
end
And looping over all of the arrows to check if they're offscreen, and if they are, removing them from the table. (Note that this for loop uses pairs, not ipairs.)

Code: Select all

function remove_offscreen_arrows()
	for key, arrow in pairs(arrows) do
		if is_offscreen(arrow) then
			arrows[key] = nil
		end
	end
end
And calling them.

Code: Select all

function love.update(dt)
	remove_offscreen_arrows()
end

function love.mousepressed(x, y)
	create_arrow(x, y)
end
I hope this helps! :)