#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

# 3D Objects Concerning Tables

Posts: 154

Is there anyway that I can duplicate one particular 3D object and make the cloned objects follow other targeted objects?? For example, if I have three spheres in my game and I want to clone a cube so that it not only matches the amount of spheres in the game, but they all follow one sphere each using tables, how am I supposed to do this???

Thanks in advance • Posts: 10,052

I thought we already did that. You had spheres that followed another sphere. In order to do that, the objects information was kept in a table and it’s x,y,z positions were updated to follow another object. You just need to read the table and create cubes for each of the spheres.

• Posts: 154

@dave1707, the thing is that I don’t know yet how to individually make each healthbar go to each sphere without all of them bundling up around one sphere every time. Could you possibly show me a way to fix this?

Thanks • Posts: 10,052

@Creator27 So if you have x number of spheres, you want to create x number of cubes. Then you want 1 cube to follow 1 sphere for x number of spheres. Is that what I’m getting from your question.

• Posts: 154

@dave1707, yes that’s exactly what I want.

• Posts: 10,052

@Creator27 Do you remember what discussion I gave you an example of multiple spheres following another sphere. I can’t find it but the code in there should work.

• Posts: 10,052

@Creator27 Heres an example that creates a cube above multiple spheres. The next step is for the spheres to be moving and the cubes to follow. I can’t find the code I showed you where I was moving spheres to follow another sphere. Just need to take that code and use it for the cubes.

``````viewer.mode=FULLSCREEN

function setup()
tab={}
assert(OrbitViewer, "Please include Cameras as a dependency")
scene = craft.scene()
skyMaterial=scene.sky.material
skyMaterial.sky=color(0, 62, 255, 255)
skyMaterial.horizon=color(0, 255, 201)
scene.sun.rotation=quat.eulerAngles(20,45,-30)
for z=1,20 do
createSphere()
end
end

function createSphere(pos)
pos=vec3(math.random(-20,20),math.random(-20,20),math.random(-30,20))
sphere1=scene:entity()
sphere1.position=pos
sphere1.model = craft.model.icosphere(1,2)
sphere1.material = craft.material(asset.builtin.Materials.Specular)
sphere1.material.diffuse=color(255,0,0)
table.insert(tab,sphere1)
end

function createCube(pos)
cube1=scene:entity()
cube1.position=pos
cube1.model = craft.model.cube(vec3(1,1,1))
cube1.material = craft.material(asset.builtin.Materials.Specular)
cube1.material.diffuse=color(123, 255, 0)
end

function draw()
update(DeltaTime)
scene:draw()
end

function update(dt)
scene:update(dt)
end

function touched(t)
if t.state==BEGAN then
for a,b in pairs(tab) do
createCube(b.position+vec3(0,2,0))
end
end
end
``````
• edited October 2021 Posts: 154

@dave1707, I tried to add your following code to the healthbar code, and it didn’t seem to work. I’ve been messing around with everything but nothing works. Could you show me how to use the following code that you wrote, and make it so that works???

Here’s the code:

``````viewer.mode=FULLSCREEN

function setup()
tab={}
assert(OrbitViewer, "Please include Cameras as a dependency")
fill(255)
scene = craft.scene()
skyMaterial=scene.sky.material
skyMaterial.sky=color(0, 62, 255, 255)
skyMaterial.horizon=color(99, 255, 0, 255)
scene.sun.rotation=quat.eulerAngles(20,45,-30)
v.rx,v.ry=20,20
createRect()
for z=1,5 do
s1=createSphere(0,30,0,1)
end
for z=1,5 do
table.insert(tab,createSphere(math.random(-10,10),math.random(20,35),math.random(-10,10),0))
end
end

function draw()
update(DeltaTime)
scene:draw()
text("Drag your finger on the screen to rotate the cube",WIDTH/2,HEIGHT-100)
for z=#tab,1,-1 do
v1=s1.sphere1.position-tab[z].sphere1.position
tab[z].ss.linearVelocity=v1
end
end
end

function update(dt)
scene:update(dt)
end

function touched(t)
if t.state==BEGAN then
follow=not follow
end
end

function createRect()
diff=8
c1=scene:entity()
w.restitution=.6
c1.position=vec3(0,-10,0)
c1.model = craft.model.cube(vec3(25,10,10))
c1.model:position(5,-5,diff,5)
c1.model:position(18,-5,diff,5)
c1.model:position(10,-5,diff,5)
c1.model:position(9,-5,diff,-5)
c1.model:position(14,-5,diff,-5)
c1.model:position(19,-5,diff,-5)
c1.material = craft.material(asset.builtin.Materials.Standard)
end

function createSphere(x,y,z,c)
local sphere1=scene:entity()
s.restitution=1.5
sphere1.position=vec3(x,y,z)
sphere1.model = craft.model.icosphere(.5,5)
sphere1.material = craft.material(asset.builtin.Materials.Specular)
if c==1 then
sphere1.material.diffuse=color(0,0,255)
else
sphere1.material.diffuse=color(255,0,0)
end
return({sphere1=sphere1,ss=s})
end
``````
• Posts: 10,052

@Creator27 Heres some code that creates 15 random spheres and 15 tall rects. The rects will find the spheres they’re supposed to follow and follow them around.

``````viewer.mode=FULLSCREEN

function setup()
spTab,rcTab={},{}
assert(OrbitViewer, "Please include Cameras as a dependency")
scene = craft.scene()
skyMaterial=scene.sky.material
skyMaterial.sky=color(0, 62, 255, 255)
skyMaterial.horizon=color(99, 255, 0, 255)
scene.sun.rotation=quat.eulerAngles(20,45,-30)
v.rx,v.ry=10,20
createFloor()

-- create 10 spheres and rects
for z=1,15 do
table.insert(spTab,
createSphere(math.random(-20,20),math.random(30,40),math.random(-20,20)))
table.insert(rcTab,
createRect(math.random(-20,20),math.random(30,40),math.random(-20,20)))
end
end

function draw()
update(DeltaTime)
scene:draw()

for z=1,#spTab do
v1=spTab[z].sphere1.position-rcTab[z].rec.position
rcTab[z].rr.linearVelocity=v1
end
end

function update(dt)
scene:update(dt)
end

function createFloor()
floor=scene:entity()
f.restitution=.8
floor.position=vec3(0,-20,0)
floor.model = craft.model.cube(vec3(100,.1,100))
floor.material = craft.material(asset.builtin.Materials.Standard)
end

function createRect(x,y,z)
local rec=scene:entity()
rc.restitution=1
rec.position=vec3(x,y,z)
rec.model = craft.model.cube(vec3(.5,3,.5))
rec.material = craft.material(asset.builtin.Materials.Standard)
return({rec=rec,rr=rc})
end

function createSphere(x,y,z)
local sphere1=scene:entity()
sph.restitution=1
sphere1.position=vec3(x,y,z)
sphere1.model = craft.model.icosphere(1,2)
sphere1.material = craft.material(asset.builtin.Materials.Specular)
sphere1.material.diffuse=color(0,0,255)
return({sphere1=sphere1,ss=sph})
end
``````
• Posts: 31

Note also that you can create a global variable to specify the number of circles and rectangles. Like this:

numSpheres = 15

You would place the “ numSpheres = 15” in the setup() function BEFORE the for loop. Right above the line: “ —create 10 spheres and recs”
would be fine

Since the rectangle, in our minds, is an add on to the sphere:
-because: the SPHERE is the UNIT
and
-the rectangle is IT’S healthbar.
Then: numSpheres is the name and not numRecs

A reason you may want to do this is the numSpheres can be referenced by other functions down the road as an answer to: “how many spheres are there?”

When you start adding and removing spheres you can either use that same “ numSpheres” or add a new variable named “currentSpheres” that represent how many spheres are alive.

Assuming the sphere is a stand in for a future UNIT in a game, you can create the variable as :

numUnits = 15

Hope I’ve been at all helpful
-Timmy_theBarbarian

• Posts: 31

P.s. @Creator27 : the reason your most recent code didn’t work was because you only ever created 1 rectangle and then never told it to move.

Observe:

The reason @dave1707’s code works is because:

STEP 1:
- he creates the rectangles at the same time as the circles.

———————————

``````-- create 10 spheres and rects
for z=1,15 do
table.insert(spTab,
createSphere(math.random(-20,20),math.random(30,40),math.random(-20,20)))
table.insert(rcTab,
createRect(math.random(-20,20),math.random(30,40),math.random(-20,20)))
end
``````

———————————

He adds them to their own individual tables yes, and they are at the same index position in each. The index is the current “z” in the for loop (1-15)

STEP 2:

Then he references them with a single for loop in the follow code:

————————————

for z=1,#spTab do
v1=spTab[z].sphere1.position-rcTab[z].rec.position
rcTab[z].rr.linearVelocity=v1
end

————————————

Note how he references both tables with the same index “z”
using: spTab[z] and rcTab[z]

TAKE SPECIAL NOTE:

1: He places the CREATION of the spheres and rectangles in the setup code so it only runs ONCE

2: He places the FOLLOW code in the draw() function so that it continues to call the movement thus creating the animation

Thank you for listening,
-Timmy_theBarbarian

• Posts: 10,052

@Timmy_theBarbarian After you’re here awhile, you’ll notice my examples are mostly just hack code to give the person an idea of how to do something. It’s not meant to be correct programming, so there’s a lot of things that can be done to my code to make it better. It’s up to the person using my code to add/modify what they want.

• Posts: 31

Agreed. That’s the best approach IMO. Never met two programmers who had the same methods. Not once.

My helping approach is to try and piggy back on an example with a more in depth explanation for the step by step of how and why it works.

I do this because stack overflow examples have always needed to be broken down and explained to me piece by piece in order to understand and move forward. I don’t ALWAYS clarify. But if I notice an aspiring coder asking questions consistently I try tohelp.

• edited October 2021 Posts: 154

@dave1707, how can I make the health bars track the red spheres health every frame (the health is a custom variable)??? For example, the width of the health bar determines how much health each red sphere has. Is there anyway that I can continuously update the width of the healthbar based on the health of the red sphere???

Thanks • Posts: 154

@dave1707, it’s okay I figured out an alternative solution.