Page 1 of 1

How to make a object move to multiple position?

Posted: Fri Nov 30, 2012 5:38 pm
by SuperMeijin
Ive seen this thread and it works viewtopic.php?f=4&t=10822. But want to do this for multiple objects. Heres my code

Code: Select all

function love.keypressed(key)
    if (key == " ") and playerDead == false then
	    shoot()
	end
	if (key == "j") then
	    spawnEnemies()
    end
end

Code: Select all

function shoot() -- function for shooting
    local shot = {}
	shot.x = player.x + 50
	shot.y = player.y + 40
	table.insert(player.shots, shot)
end

Code: Select all

or i,v in ipairs(player.shots) do -- movement for shots
		v.x = v.x + 5
		if v.x > 800 then
		    table.insert(remShot, i) -- if shot is out of boundrys destroy them
		end
Sorry for indenting problem

So I need each of these shots to move to where I click when I press the left mouse button

Re: How to make a object move to multiple position?

Posted: Fri Nov 30, 2012 9:31 pm
by Roland_Yonaba
Hi,

You can achieve this in a simple manner. Store the start position and the target location as properties in the bullet table.
Then use a simple logic shared by all bullets to move from their start location to their target location, like this:

Code: Select all

 -- Assuming each bullet has : x,y, target_x, target_y and speed properties
local function moveBullet(bullet,dt)
  if bullet.x  ~= bullet.target_x then 
    local dx = (bullet.target_x-bullet.x) * bullet.speed * dt
    bullet.x = bullet.x + dx
  end
  -- same logic on y-axis
end
You may want to bring some slight changes to this, though, as it moves the bullet to the exact position of the target. Because you can't really control dx, dy (because of dt), you might have to add some tolerance parameter to stop updating the bullet position when it enters a certain range around the target.

Re: How to make a object move to multiple position?

Posted: Sat Dec 01, 2012 5:54 pm
by SuperMeijin
A few questions. What is a local table? And what does ~= do?

Re: How to make a object move to multiple position?

Posted: Sat Dec 01, 2012 6:04 pm
by Robin
SuperMeijin wrote:A few questions. What is a local table?
A local variable is one that is only accessible from the place it is defined. (Not sure how to explain it well. Maybe someone else can?)

This has many advantages, especially that you don't get pieces of code changing the meaning of things that happen somewhere completely different, which could have made reading, understanding and changing the code much harder.
SuperMeijin wrote:And what does ~= do?
It means "is not equal to". For example, 3 ~= 5 returns true and "same" ~= "same" returns false.

Re: How to make a object move to multiple position?

Posted: Sun Dec 02, 2012 8:48 pm
by SuperMeijin
I understand you code but im not sure where to put it?

Re: How to make a object move to multiple position?

Posted: Tue Dec 04, 2012 10:52 am
by miko
Robin wrote:
SuperMeijin wrote:A few questions. What is a local table?
A local variable is one that is only accessible from the place it is defined. (Not sure how to explain it well. Maybe someone else can?)
Yes, it is local to the current closure (scope), which can be:
- a file
- a function
- a do end block
- a for..end block
- a while...do...end block
So in this code:

Code: Select all

-- myfile.lua
1     local a=1
1     do
12      local a=2
1     end
1
1     function test()
1 3     local a=3
1 3     for a=4,5 do
1 34  
1 3     end
1 3     do
1 3 6     local a=6
1 3     end
1     end
there are 5 different instances of a variable named "a", and when you create one in local scope with the same name as one from the upper scope, then the one from the upper scope is not accessible within this local scope. Ie. You can access "file's" a(=1) only outside of function test(), because inside this function it is shadowed with its own local variable.

Re: How to make a object move to multiple position?

Posted: Tue Dec 04, 2012 11:38 am
by substitute541
SuperMeijin wrote:I understand you code but im not sure where to put it?
It's a logic operator. It's used for while loops, if, if-elseif, etc. statements. For example, "if (same ~= same) then dosomething end".

Re: How to make a object move to multiple position?

Posted: Tue Dec 04, 2012 6:54 pm
by SuperMeijin
Ive only got this far and theyre still moving as one

Code: Select all

function love.mousepressed(x, y, button)
    if button == "l" then
		startX = player.x + 80
		startY = player.y + 60
		destX = x
		destY = y
		angle = (math.atan2(destY - player.y, destX - player.x))
		bulletDy = shotspeed * math.sin(angle)
		bulletDx = shotspeed * math.cos(angle)
		shoot()
	end
end

Code: Select all

function shoot() -- function for shooting
    shot = {}
	shot.x = startX
	shot.y = startY
	dx = bulletDx
	dy = bulletDy
	table.insert(player.shots, shot)
end

Code: Select all

	for i,v in ipairs(player.shots) do -- movement for shots
		v.x = v.x + (dx*dt)
		v.y = v.y + (dy*dt)
		if v.x > 800 then

Re: How to make a object move to multiple position?

Posted: Thu Dec 06, 2012 10:21 am
by SuperMeijin
I have solved the problem now thanks for all the help.