How to make characters(or possibly objects) to interact with each other when they come within a certain distance of each other

Hello, I am still learning how to use the Lua language 

I am currently working with a team and I have been tasked with making sure that characters and/or objects can interact with each other when it comes to using spells or talking to each other, I haven’t found anything that helps me because anything close is usually a tutorial for 2D Love or Unity. Some help would be much appreciated.

First, let me suggest that if you are super new to Corona, you would be well served to examine the samples that come with it, and to practice basics like Lua and fundamental stuff like creating display objects and setting up physics.

Jumping right into a game may be kind of hard.

Second, let me try to answer.  

Do you mean, “How do I calculate when two objects are within a certain distance of each other?”

If “No”, please add more details and better define what you mean by interact as well as how things in your game will be moving around.

If “Yes”, that is a 2d math problem.

You can read up on it, watch videos, and/or use a library to do the calculation.

SSK2 comes with a complete 2d math library.

For example you could test if two objects are within a known distance of each other like this.

(Assumes SSK is initialized already: https://roaminggamer.github.io/RGDocs/pages/SSK2/#installing-ssk2))

local objA = display.newCircle( 100, 100, 20) local objB = display.newCircle( 150, 100, 20) local objC = display.newCircle( 100, 200, 20) -- Check if B is within 75 pixels of A local vec = ssk.math2d.sub(objA,objB) if( ssk.math2d.length( vec ) \> 75 ) then objB:setFillColor(1,0,0) else objB:setFillColor(0,1,0) end -- Check if C is within 75 pixels of A local vec = ssk.math2d.sub(objA,objC) if( ssk.math2d.length( vec ) \> 75 ) then objC:setFillColor(1,0,0) else objC:setFillColor(0,1,0) end

By the end of this code, you would have three circles

A - White,

B - Green 

C - Red

PS - SSK’s 2d math lib also has a helper function that makes this calculation/test even easier to write:

local objA = display.newCircle( 100, 100, 20) local objB = display.newCircle( 150, 100, 20) local objC = display.newCircle( 100, 200, 20) -- Check if B is within 75 pixels of A if( ssk.math2d.isWithinDistance( objA, objB, 75 ) ) then objB:setFillColor(1,0,0) else objB:setFillColor(0,1,0) end -- Check if C is within 75 pixels of A if( ssk.math2d.isWithinDistance( objA, objC, 75 ) ) then objC:setFillColor(1,0,0) else objC:setFillColor(0,1,0) end

First, let me suggest that if you are super new to Corona, you would be well served to examine the samples that come with it, and to practice basics like Lua and fundamental stuff like creating display objects and setting up physics.

Jumping right into a game may be kind of hard.

Second, let me try to answer.  

Do you mean, “How do I calculate when two objects are within a certain distance of each other?”

If “No”, please add more details and better define what you mean by interact as well as how things in your game will be moving around.

If “Yes”, that is a 2d math problem.

You can read up on it, watch videos, and/or use a library to do the calculation.

SSK2 comes with a complete 2d math library.

For example you could test if two objects are within a known distance of each other like this.

(Assumes SSK is initialized already: https://roaminggamer.github.io/RGDocs/pages/SSK2/#installing-ssk2))

local objA = display.newCircle( 100, 100, 20) local objB = display.newCircle( 150, 100, 20) local objC = display.newCircle( 100, 200, 20) -- Check if B is within 75 pixels of A local vec = ssk.math2d.sub(objA,objB) if( ssk.math2d.length( vec ) \> 75 ) then objB:setFillColor(1,0,0) else objB:setFillColor(0,1,0) end -- Check if C is within 75 pixels of A local vec = ssk.math2d.sub(objA,objC) if( ssk.math2d.length( vec ) \> 75 ) then objC:setFillColor(1,0,0) else objC:setFillColor(0,1,0) end

By the end of this code, you would have three circles

A - White,

B - Green 

C - Red

PS - SSK’s 2d math lib also has a helper function that makes this calculation/test even easier to write:

local objA = display.newCircle( 100, 100, 20) local objB = display.newCircle( 150, 100, 20) local objC = display.newCircle( 100, 200, 20) -- Check if B is within 75 pixels of A if( ssk.math2d.isWithinDistance( objA, objB, 75 ) ) then objB:setFillColor(1,0,0) else objB:setFillColor(0,1,0) end -- Check if C is within 75 pixels of A if( ssk.math2d.isWithinDistance( objA, objC, 75 ) ) then objC:setFillColor(1,0,0) else objC:setFillColor(0,1,0) end