It looks like you're new here. If you want to get involved, click one of these buttons!
In my game, I want to spawn as many entities as I ask, but I don’t exactly know how to spawn multiple entities in the scene without having to write big chunk a of code and wasting a lot of time. I have made a simple table function that inserts an entity into the scene, but I don’t know how to tell the table to spawn more than one entity at a time. Could somebody please shed some light on this??
Here’s my code:
-- Inserting Tables
function setup()
-- Create a new craft scene
scene = craft.scene()
bobs = {}
end
function createSubjects(n)
for i=0, #bobs do
bob = scene:entity()
bob.model = craft.model(asset.builtin.Primitives.Sphere)
bob.position = vec3(0, -1, 4)
bob.scale = vec3(1, 1, 1) / 8
bob.material = craft.material(asset.builtin.Materials.Standard)
end
table.insert(bobs, n)
end
function touched(touch)
if touch.state == BEGAN then
createSubjects(5) — The number represents how many entities I want to spawn
end
end
function update(dt)
-- Update the scene (physics, transforms etc)
scene:update(dt)
end
-- Called automatically by codea
function draw()
update(DeltaTime)
-- Draw the scene
scene:draw()
end
Thanks in advance
Comments
Will this do.
@dave1707, thank you for that code. I did, however, run into one problem. Say that I wanted to generate 3 spheres into the scene. I go into the scene and double tap as I’m supposed to, but instead of spawning 3 spheres, it only spawns 2. I found out you have to double tap one more time for the third sphere to spawn into the scene. I know it has something to do with the index count of the “for loop”, but I don’t know how to fix it. Is there anyway to fix this problem??
Thanks
@Creator27 Here's the above code modified to add objects each double tap. It starts out adding 1 object. It adds 1 to the nbr each double tap.
@dave1707, thank you for that code. There was one other problem that I did encounter. Every time I want to destroy one of the spheres by hitting an object, it sometimes doesn’t remove any of the spheres. Is there anyway to fix this problem??
Here’s my code:
Thanks
@Creator27 Here’s an example showing how to destroy an entity and remove it from a table. When the spheres hit the rectangle, I destroy them and remove them from the table.
@dave1707, thank you for that code. It really helped out a lot!
@dave1707, just one more question. If I want to destroy one of the spheres when I collide with a particular sphere, all it does is destroy them all at once. Is there anyway to fix this???
Thanks
@Creator27 You should have all your spheres in the table and just destroy the ones that collide.
@dave1707, the thing is, I don’t exactly know how to do that. Like I know what you mean, but I don’t know what to write.
@Creator27 In the example I have above, I show how to put the spheres in a table (createSphere). I also show how to destroy them and remove them from the table (checkCollision). If your checking if two spheres in the table collide, you have to go through the table for each sphere and for each sphere go thru the table again. That’s a for loop inside a for loop.
@dave1707, it’s all good actually. I figured out what to do and it works like a charm. Apologies if I’m sometimes a bit hard to work with.
@dave1707, when I want to make an object’s position be the player’s through a duplicated destroy table, it will stop following the player if he/she moves at a certain speed. Is there anyway I can fix this??
Here’s the code I’m trying to fix:
Thanks
@Creator - don’t see the rest of your code but, daft question, what happens if you use:
@Bri_G, that doesn’t really work. It just does the exact same things as before.
All good. Figured out how to fix it, and now it works fine!
@dave1707, there is actually one more thing that I need help with. How can I use a raycast to destroy a sphere?
Here’s the code I’ve written:
Thanks
@Creator27 Here’s a 3D raycast example. Move the sliders to move the raycast line. When the raycast line intersects a sphere, it will turn green. You can add whatever code you want at that point. The raycast only finds the first intersect since there’s not a raycastAll for 3D. You can rotate the screen to get a better view of the intersects.
@dave1707, thank you for that. After a bit of tinkering, it works like a charm!
@dave1707, thank you for that code. Another thing, do you know how I can make the raycast rotate around as another object?? For example, if I have a gun that rotates around the centre of the player, is there anyway that I can get the raycast to do the same thing???
Thanks
The first vec3 in raycast is the starting point of the raycast. Just set that to where you want it to start.
@dave1707, I understand what you mean, but I’ve been trying to change the first vec3() as you said for quite a while a now, but with no luck. Are you able to give me some sort of coordinates for me to put in??
Thanks
I don’t know anything about your code so I can’t give to specifics. The first vec3 is the starting point of the raycast and the second vec3 is the direction that the raycast is going. The first value is easy, the second value will be harder to figure out based on where you want it to go.
@dave1707, I have another with your raycast example. The first one being that your collision detection inside the pairs function says that the position of the red sphere is a nil value. Is there anyway to fix this???
Thanks
@Creator27 Are you referring to the latest example above. I’m not sure where you’re getting a nil value.
@dave1707, I did actually manage to fix the position thing. But I have another problem. Even though your code now works in my game, I can’t get each of the x,y,z values in the collision detection to like only come into effect when the raycast actually hits a red sphere, as sometimes it will be almost perfect, and other times, the red sphere will be destroyed when it’s like a mile away from the raycast. And yes, I am referring to the example above. Is there anyway that I can change the collision detection values to only be surrounding the raycast itself???
For reference, the red spheres in my game are scaled at .13 by .13 by .13. And here’s my code for the raycasting:
@Creator27 I don’t have time right now to see what’s going on, but just looking at your code above, why is your first compare to rx >=1. Shouldn’t that be <=1.
@dave1707, that was because when I tried <2, it didn’t work at all. That’s why I used > instead of < and changed the numbers.
@Creator27 The raycast returns a table that has info about the sphere that is hit by the ray. The only usable value is the x,y,z position. Because it doesn’t give the center coordinates of the sphere, but where it hit the sphere, you have to compare the hit x,y,z values with the sphere center x,y,z. If the difference between them is less than the size of the sphere, then that sphere was the one hit by the ray. Since you’re doing scale on the sphere, you’ll have to determine the final size and use that in the compare. All three compares should use the <= .
@dave1707, is there anyway to spawn one red sphere every second using ElapsedTime???
Thanks
Yes. Just save ElapsedTime to a value then compare the two. If the difference is >= 1 then save ElapsedTime to the value again and spawn the sphere. Just keep checking.
@dave1707, is there anyway that I can make the red spheres repel themselves in the opposite direction if they collide with one another???
Thanks
If you’re using the physics engine, they should repel when they collide. Set the restitution to greater or less than 1 depending on how fast you want them to repel.
@dave1707, how exactly would I check if two red spheres collide with each other so that I can set the restitution of both the red spheres???
Thanks
@Creator27 Looking thru some of your code at the top, I found this function.
You’re setting g.restitution=1 there. You would also set the .restitution for your spheres. The physics engine should be doing all the collision calculations and movement for you. Any non moving body should be STATIC, and moving ones should be DYNAMIC.
I’m really not sure how your spheres are moving. Are you moving them or is the physics engine moving them.
@dave1707, I’m pretty sure that it’s half n half, like the physics engine is moving them alongside with me moving them. The g.restitution thing was for another feature that I added in my project.
So are you moving them using linearVelocity.
@dave1707, yes I am.
And also, I’m still having trouble with raycasting collision detection, it’s just an absolute nightmare to figure out the values that work best. The raycast keeps on destroying the red spheres that are not touching it, and it’s driving me insane.