Lighters pause in midpoints

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.
HedgeHog builder
Prole
Posts: 15
Joined: Sat Aug 19, 2017 9:13 am

Lighters pause in midpoints

Post by HedgeHog builder »

So when you left click; a beacon spawns, they are square and very small
When you right click; a spawner appears, spawners are circles

The lighters, which are the products of the spawners, will then go to the beacons.

However, if you place the beacons in a line like this;
The lighters will end up pausing in the middle.
Attachments
main.lua
(4.09 KiB) Downloaded 182 times
Can someone help me make it so that the lighters will then go to the closest beacon?
Can someone help me make it so that the lighters will then go to the closest beacon?
Sad.PNG (11.18 KiB) Viewed 7647 times
User avatar
Azhukar
Party member
Posts: 478
Joined: Fri Oct 26, 2012 11:54 am

Re: Lighters pause in midpoints

Post by Azhukar »

I've changed the indentation to make your file more readable. It seems the code you posted does not behave as you describe, you probably posted a different version.

I recommend you read a guide on how to properly format your code to be readable and maintainable. Here's a start http://lua-users.org/wiki/LuaStyleGuide
Attachments
main.lua
(3.86 KiB) Downloaded 144 times
HedgeHog builder
Prole
Posts: 15
Joined: Sat Aug 19, 2017 9:13 am

Re: Lighters pause in midpoints

Post by HedgeHog builder »

It actually still does the pause in midpoints. I would like it to go from first beacon placed to second, then third.

Does anyone know how to do that?
Attachments
main.lua
(3.86 KiB) Downloaded 152 times
Still does.PNG
Still does.PNG (13.9 KiB) Viewed 7551 times
User avatar
Azhukar
Party member
Posts: 478
Joined: Fri Oct 26, 2012 11:54 am

Re: Lighters pause in midpoints

Post by Azhukar »

HedgeHog builder wrote: Thu Oct 19, 2017 7:01 am It actually still does the pause in midpoints. I would like it to go from first beacon placed to second, then third.

Does anyone know how to do that?
Sorry I missed the part where you needed to press the button "a" for the units to spawn. The reason your units stop in the middle of even number of spawners is because you're moving them once for each target, so when there's an equal amount of targets on each side their movements cancel out.

Here's a very crude example of how you could determine which target the units should go to next.
Attachments
main.lua
(4.03 KiB) Downloaded 154 times
HedgeHog builder
Prole
Posts: 15
Joined: Sat Aug 19, 2017 9:13 am

Re: Lighters pause in midpoints

Post by HedgeHog builder »

Code: Select all

		for i,v in ipairs(Opposing_Pens) do
			for k,Beacon in ipairs(Beacons) do
				if (v.target == nil) then
					v.target = k
				elseif (v.target ~= k) then
					break
				end
So line by line:

For each index and value of opposing pens do;
for each index and value of beacons do;
if the target of opposing pens is non existent; then target is set to index of a beacon
else if the target is not k, then finish the loop

Correct?
User avatar
Azhukar
Party member
Posts: 478
Joined: Fri Oct 26, 2012 11:54 am

Re: Lighters pause in midpoints

Post by Azhukar »

HedgeHog builder wrote: Fri Oct 20, 2017 7:33 amSo line by line:

For each index and value of opposing pens do;
for each index and value of beacons do;
if the target of opposing pens is non existent; then target is set to index of a beacon
else if the target is not k, then finish the loop

Correct?
Yes. 'break' always ends only one loop, the one it is called from. The outer loop keeps going.

The target is then removed once collision is detected and target is destroyed. If you want to be fancy you can determine the target by calculating the distance from the unit to all the targets and picking the one with the one with shortest distance.
HedgeHog builder
Prole
Posts: 15
Joined: Sat Aug 19, 2017 9:13 am

Re: Lighters pause in midpoints

Post by HedgeHog builder »

So if I wanted to do comparison on distance between 2 points from the unit would I do

Code: Select all

unitdist 1 = unit.x - k.x
unit 2 = unit.x - k.x

if unitdist1 < unit2 then
target = unitdist1
else 
target = unit2
User avatar
Azhukar
Party member
Posts: 478
Joined: Fri Oct 26, 2012 11:54 am

Re: Lighters pause in midpoints

Post by Azhukar »

HedgeHog builder wrote: Sun Oct 22, 2017 3:07 amSo if I wanted to do comparison on distance between 2 points from the unit would I do
Distance between 2 points would be

Code: Select all

local distance = math.sqrt((unit.x-k.x)^2 + (unit.y-k.y)^2)
Based on the https://en.wikipedia.org/wiki/Pythagorean_theorem
HedgeHog builder
Prole
Posts: 15
Joined: Sat Aug 19, 2017 9:13 am

Re: Lighters pause in midpoints

Post by HedgeHog builder »

How would I select the closest one?

if distance < k.x,k.y?
User avatar
Azhukar
Party member
Posts: 478
Joined: Fri Oct 26, 2012 11:54 am

Re: Lighters pause in midpoints

Post by Azhukar »

HedgeHog builder wrote: Mon Oct 23, 2017 7:51 pmHow would I select the closest one?
Here is a tutorial for a minimum value algorithm: http://www.programming4beginners.com/tu ... -algorithm

You'll have to iterate through all potential targets and apply that to pick the one with the shortest distance.
Post Reply

Who is online

Users browsing this forum: targetcreature and 48 guests