Spawning Entities Through Tables

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 :slight_smile:

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 :slight_smile:

@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:

if weaponRay~=nil then
        rx=weaponRay.point.x
        ry=weaponRay.point.y
        rz=weaponRay.point.z        
        for a,b in pairs(bobs) do
            if math.abs(bobs[a].sphere1.x-rx)>=1 and math.abs(bobs[a].sphere1.y-ry)<=5 and math.abs(bobs[a].sphere1.z-rz)<=1 then
                bobs[a].sphere1:destroy()
                table.remove(bobs, a)
                sound(SOUND_HIT, 8571)
            end
        end
    end     
function createFirstSphere()
    sphere1=scene:entity()
    s1=sphere1:add(craft.rigidbody,DYNAMIC)
    s1.sleepingAllowed=false
    sphere1.name="Red Enemy"
    sphere1.position=vec3(math.random(-3,3),0
    ,-1)
    sphere1.model = craft.model.icosphere(1,5)
    sphere1:add(craft.shape.sphere, 1.3)
    sphere1.scale=vec3(.13,.13,.13)
    sphere1.material = craft.material(asset.builtin.Materials.Specular)
    sphere1.material.diffuse=color(255,0,0)
    return({sphere1=sphere1,ss=s1})
end

@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 :slight_smile:

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 :slight_smile:

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 :slight_smile:

@Creator27 Looking thru some of your code at the top, I found this function.

function createGround(p)
    ground = scene:entity()
    g = ground:add(craft.rigidbody, STATIC)
    g.restitution = 1
    ground.model = craft.model.cube(vec3(4, 4, 4))
    ground:add(craft.shape.box, vec3(4, 4, 4))
    ground.position = p
    ground.material = craft.material(asset.builtin.Materials.Standard)
    ground.material.map = readImage(asset.builtin.Blocks.Sand)
end

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.