3D Problems

So… two problems I have with my game. In it, the player is a circle that moves around another big circle. I’m trying to make the player jump but it’s not working, and the collision detection isn’t working how it should. Is there anyway for someone to fix and revamp my code??

-- Odd
assert(OrbitViewer)
function setup()
    -- Create a new craft scene
    scene = craft.scene()
    scene.physics.gravity = vec3(0, -14, 0)
    scene.sky.material.sky = color(255, 72, 0)
    scene.sky.material.horizon = color(224, 147, 143) 
    cameraSettings = scene.camera:get(craft.camera)
    cameraSettings.nearPlane = 0.001
    viewer.mode = FULLSCREEN
    rotation = 0.15
    rotation2 = 0
    
    
    -- Create a new entity
    earth = scene:entity()
    earth.model = craft.model(asset.builtin.Primitives.Sphere)
    earth.position = vec3(0, 0, 0)
    earth.eulerAngles = vec3(0, 0, 0)
    earth.scale = vec3(1, 1, 1) / 8
    earth.material = craft.material(asset.builtin.Materials.Standard)
    earth.material.diffuse = color(186, 255, 0)
    
    player = scene:entity()
    body = player:add(craft.rigidbody, STATIC)
    player.model = craft.model(asset.builtin.Primitives.Sphere)
    player:add(craft.shape.model, player.model)
    player.position = vec3(0, 0, -0.4)
    player.eulerAngles = vec3(0, 0, 0)
    player.scale = vec3(0.1, 0.1, 0.1) / 8 
    
    makeSpike(vec3(0, player.y, -0.13), quat.eulerAngles(-90, 90, -90))
    
    scene.camera.z = -0.5
    
end

function makeSpike(p, e)
    spike = scene:entity()
    sb = spike:add(craft.rigidbody, STATIC)
    spike.model = craft.model(asset.builtin.CastleKit.towerTopRoof_obj)
    spike:add(craft.shape.model, spike.model)
    spike.position = p
    spike.rotation = e
    spike.scale = vec3(0.01, 0.01, 0.01) / 8
end

function touched(touch)  
    if touch.state == BEGAN and touch.tapCount == 2 then
        zoomOne = tween(0.25, cameraSettings, {fieldOfView = 35}, tween.easing.cubicIn)
        zoomTwo = tween(0.25, cameraSettings, {fieldOfView = 45}, tween.easing.cubicOut)
        tween.sequence(zoomOne, zoomTwo)
    end
    
    if touch.state == MOVING and touch.deltaX > 0 then
        rotation = rotation + 2
    end
    
    if touch.state == MOVING and touch.deltaX < 0 then
        rotation = rotation - 2
    end
    
    if touch.state == MOVING and touch.deltaY > 0 then
        rotation2 = rotation2 + 2
    end
    
    if touch.state == MOVING and touch.deltaY < 0 then
        rotation2 = rotation2 - 2
    end
end

function update(dt)
    -- Update the scene (physics, transforms etc)
    scene:update(dt)
    player.rotation = quat.eulerAngles(rotation2, rotation, 0)    
    player.position = player.forward * 0.068 * 2.0
end

-- Called automatically by codea 
function draw()
    update(DeltaTime)
    
    -- Draw the scene
    scene:draw()	
    
end

function calc()
    x = math.abs(player.x - spike.x)
    y = math.abs(player.y - spike.y)
    z = math.abs(player.z - spike.z)
    if x<2 and y<2 and z<2 then
        
    end
end

Thanks in advance :slight_smile:

Try this for collision. Not sure what you want jump to do.

-- Odd
--assert(OrbitViewer)
function setup()
    -- Create a new craft scene
    scene = craft.scene()
    scene.physics.gravity = vec3(0, -14, 0)
    scene.sky.material.sky = color(255, 72, 0)
    scene.sky.material.horizon = color(224, 147, 143) 
    cameraSettings = scene.camera:get(craft.camera)
    cameraSettings.nearPlane = 0.001
    viewer.mode = STANDARD
    rotation = 0.15
    rotation2 = 0
    
    
    -- Create a new entity
    earth = scene:entity()
    earth.model = craft.model(asset.builtin.Primitives.Sphere)
    earth.position = vec3(0, 0, 0)
    earth.eulerAngles = vec3(0, 0, 0)
    earth.scale = vec3(1, 1, 1) / 8
    earth.material = craft.material(asset.builtin.Materials.Standard)
    earth.material.diffuse = color(186, 255, 0)
    
    player = scene:entity()
    body = player:add(craft.rigidbody, STATIC)
    player.model = craft.model(asset.builtin.Primitives.Sphere)
    player:add(craft.shape.model, player.model)
    player.position = vec3(0, 0, -0.4)
    player.eulerAngles = vec3(0, 0, 0)
    player.scale = vec3(0.1, 0.1, 0.1) / 8 
    
    makeSpike(vec3(0, player.y, -0.13), quat.eulerAngles(-90, 90, -90))
    
    scene.camera.z = -.5
    
end

function makeSpike(p, e)
    spike = scene:entity()
    sb = spike:add(craft.rigidbody, STATIC)
    spike.model = craft.model(asset.builtin.CastleKit.towerTopRoof_obj)
    spike:add(craft.shape.model, spike.model)
    spike.position = p
    spike.rotation = e
    spike.scale = vec3(0.01, 0.01, 0.01) / 8
end

function touched(touch)  
    if touch.state == BEGAN and touch.tapCount == 2 then
        zoomOne = tween(0.25, cameraSettings, {fieldOfView = 35}, tween.easing.cubicIn)
        zoomTwo = tween(0.25, cameraSettings, {fieldOfView = 45}, tween.easing.cubicOut)
        tween.sequence(zoomOne, zoomTwo)
    end
    
    if touch.state == MOVING and touch.deltaX > 0 then
        rotation = rotation + 2
    end
    
    if touch.state == MOVING and touch.deltaX < 0 then
        rotation = rotation - 2
    end
    
    if touch.state == MOVING and touch.deltaY > 0 then
        rotation2 = rotation2 + 2
    end
    
    if touch.state == MOVING and touch.deltaY < 0 then
        rotation2 = rotation2 - 2
    end
end

function update(dt)
    -- Update the scene (physics, transforms etc)
    scene:update(dt)
    player.rotation = quat.eulerAngles(rotation2, rotation, 0)    
    player.position = player.forward * 0.068 * 2.0
end

-- Called automatically by codea 
function draw()
    update(DeltaTime)
    
    -- Draw the scene
    scene:draw() 
    calc()   
    
end

function calc()
    x = math.abs(player.position.x - spike.position.x)
    y = math.abs(player.position.y - spike.position.y)
    z = math.abs(player.position.z - spike.position.z)
    if x<.014 and y<.014 and z<.014 then
        print("hit")
    end
end

@dave1707, thank you for your code. What I want the player to do when jumping, is to jump away from the planet, depending on where it’s position is and it’s rotation.

Depending on the position angle of the player, you calculate values to add to the x,y,z position of the player.

I have tried doing that, but since it’s position is updated every frame to be exactly where it is, I have no control of where it can go except for player movement.

Is there anyway to fix this problem or do I have to completely rewrite the code?

I haven’t tried this yet, but multiple the position by some small value, like 1.1 . That should move it away from the surface each draw cycle. You’ll have to play around with the value.

@dave1707, what what I try to write in order to push it from it’s position depending on it’s angle?

You probably don’t need to worry about the angle, just it’s x,y,z values. Those will be different based on the angle of the player around the sphere. To move it away from the surface, you need to increase the x,y,z values. By increasing those 3 at the same rate, the player should move directly away from the surface.

@Creator27 I guess whatever works the best. That would be up to you since you know your code and what else you what to do during the jump.

And also, what would I put that in? Like would I write it in a “tween” or a “body:applyForce” or something?