Navigation

    Fuze Arena Logo
    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Popular
    • Users
    • Groups
    • Help
    • Discord

    Simple 3d collision?

    Help
    3
    6
    357
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • poof92
      poof92 last edited by

      Is there a simple way of doing 3d collision?
      And if so how?

      1 Reply Last reply Reply Quote 2
      • Jongjungbu
        Jongjungbu F last edited by Jongjungbu

        Simple as in getObjectCollision(ObjectA,ObjectB) ?
        Not yet.
        For now you have to manually check for collisions using old school methods such as distance( ) between the two object’s positions or use a bounding box and check if a point is inside the box’s dimensions. I use the latter.

        1 Reply Last reply Reply Quote 1
        • poof92
          poof92 last edited by

          How would you go about doing that?

          1 Reply Last reply Reply Quote 0
          • Jongjungbu
            Jongjungbu F last edited by Jongjungbu

            So, the easiest way again is the distance method. Since it's the easiest, that's the one I will type. It's also the most crude; you don't have as much precision in how you handle the collision detection. But it's pretty easy to understand and implement.

            If you have an asteroid and the player's ship, and you use the distance method, you are just saying, for example, that you want a collision event to occur if the ship and asteroid's centers are a certain distance apart. Let us say if their centers are 1 "meter" or 1 unit apart, we want to consider that collided.

            In Fuze, you need to store locations of your 3D objects yourself. There's currently no getObjectLocation yet.
            Note, this code below is not complete. I'm just writing the portion related to collision using distance. So, don't expect to copy/paste it and then it runs like magic. :D
            I didn't even set any camera or lighting! I'll come back later perhaps and add a bounding box example if I get a chance.

            asteroidModel = loadModel("path to the asset")
            spaceshipModel = loadModel("path to the spaceship asset")
            
            // Next lets pick where in 3d space we want them.
            asteroidPosition = {10,0,0} // This position is a vector of coordinates {x, y, z}. So x and z are "horizontal" plane, and the y is the "vertical" plane or elevation.
            spaceshipPosition = {0,0,0} // The player's ship is at the center
            
            // How far apart do we want to allow objects to be before collision?
            collisionRange = 1.0
            spaceshipHull = 1 // hull of the spaceship, to determine if we will keep checking for collisions or not.
            
            // Now place these 3D objects into the world
            asteroid = placeObject(asteroidModel, asteroidPosition, 1.0) // This puts the 3d model into the world, at the position we recorded earlier, with a size of how big we want the object to be as 1.0 (so the original size the artist created it as)
            spaceship = spaceshipObject(spaceshipModel, spaceshipPosition, 1.0) // What gets returned by this function is a handle for the object in 3D space. If we want to manipulate or move the object spaceship, we will call its handle "spaceship" not "spaceshipModel".
            
            // Function to check whether the two objects have collided due to distance from each other
            function checkCollisionDistance( objectA, objectB )
                 results = false // by default let's say that there's no collision
                 if distance( objectA, objectB ) <= collisionRange then // if the distance between the 2 objects is less than our decided amount, consider them collided
                      // Here you could place your collision event code, such as explosions, sound effects etc.
                      spaceshipHull-=1 // damage the ship
                      results = true // change the returning variable to true
                 endif
            return results
            
            // now run our main loop
            loop
                clear()
                // run your joycon controls and object movement
                 spaceshipPosition.x += 1 // move spaceship by 1 in the x-axis. eventually he will reach the asteroid
                 setObjectPos(spaceship, spaceshipPosition)
            
                // check collision event
                 // I would recommend placing some kind of check so that you do not repeatedly check this collision event 60 times per second before you're done blowing up the ship or asteroid. Maybe like this:
                 if spaceshipHull >0 then 
                                  // collision event can happen because spaceship is still alive 
                             if checkCollisionDistance( asteroidPosition, spaceshipPosition) then
                                   // if this returns true, that means spaceship and asteroid are too close to each other, so do something now.
                                   // in this example, the code inside this IF-THEN will only run once.
                             endif 
                 endif
                 if spaceshipHull <=0 then
                       // spaceship is destroyed. maybe run some animations here or end the game
                 endif
                drawObjects()
                update()
            repeat
            
            1 Reply Last reply Reply Quote 7
            • mixaal
              mixaal F last edited by mixaal

              Here's what I do for multiple 3D Objects collision detection:

              function updatePlayerToGameObjectCollision(newPosition)
                  updatePlayerPosition = true
                  for i = 0 to len(gameObjects) loop
                       obj = gameObjects[i]
                       if abs(newPosition.x - obj.position.x)<obj.collisionRadius.x and abs(newPosition.y - obj.position.y)<obj.collisionRadius.y and abs(newPosition.z - obj.position.z) < obj.collisionRadius.z then
                        updatePlayerPosition = false
                       endif
                  repeat
              return updatePlayerPosition
              
              function updateControls(player)
                 c = controls(0)
                 if c.up then
                    newPosition = // whatever
                 endif
                 if updatePlayerToGameObjectCollision(newPosition) then
                    player.position = newPosition
                 endif
              return void
              

              and game objects look like this:

              tavern = [
                .model = loadModel(....),
                .position = {10, 10, 10},
                .rotation = {....},
                .collisionRadius = { 5, 5, 5},
                .handle = 0
              ]
              gameObjects = [ tavern, ....]
              
              1 Reply Last reply Reply Quote 0
              • mixaal
                mixaal F last edited by mixaal

                When I'm looking at it, wondering if I can write :

                if abs(newPosition - obj.position < obj.collisionRadius) then
                ...
                endif
                

                Have to try it out ;-)

                1 Reply Last reply Reply Quote 0
                • First post
                  Last post