Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • CreatiCode
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo

CreatiCode Scratch Forum

  1. CreatiCode Forum
  2. Knowledge Base
  3. 3D Blocks
  4. Checking for Objects Collision with Distances

Checking for Objects Collision with Distances

Scheduled Pinned Locked Moved 3D Blocks
1 Posts 1 Posters 1.1k Views
  • 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.
  • CreatiCodeI Offline
    CreatiCodeI Offline
    CreatiCode
    wrote on last edited by admin
    #1

     

    Prerequisite

    • Speed of 3D Objects
    • Names and Sprite Objects

     

    Introduction

    We often need to check if 2 objects are touching each other. For example, if an avatar is touching a box, we can make the avatar sit on the box.

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/dde4620a-5a2f-4103-81c5-5e0a0714cbf7.gif" width="400" style="border-radius: 5px; border: 1px solid #29622d;">

    The easiest way to check if 2 objects are touching is to calculate the distance between them.

     

    Calculating Distance Using X and Y Positions

    When both objects are on the ground, we just need to check their distance using X and Y positions:

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/628c58cd-b983-4bf3-b4b3-4d200cd9a28b.png" width="800" style="">

    For example, the direct 2D distance between the avatar and the box is shown below:

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/83aa8730-8b71-491d-9f61-3421ee8c9cc1.png" width="700" style="border-radius: 5px; border: 1px solid #29622d;">

     

    Demo Program

    In this example project, we put a box at Y of 100.

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/c08ef263-1a00-409d-ae73-376f22ca5aea.png" width="900" style="">

    Then we move the avatar a little bit forward every time we press the “w” key

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/909f2fa1-7678-4447-abe6-c5a7bd9d4c7f.png" width="900" style="">

    Try it yourself:

    https://play.creaticode.com/projects/836c92f886f791086cf57b1c

     

    Using 3D Distances

    Sometimes 2D distances are not accurate enough. For example, suppose an object is floating in the air, and the avatar is right below it. Their 2D distance is close to 0, but they are Not touching each other. To account for the vertical distance, we need to calculate 3D distances:

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/771eb776-c3bf-422d-bbe3-69281a97e8d6.png" width="300" style="border-radius: 5px; border: 1px solid #29622d;">

     

    Demo Program

    In this example project, we put the box at Z of 140, and we move the avatar up until its 3D distance to the box is less than 110.

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/719beff2-f9c1-44a9-b0f1-f647a7943bd5.gif" width="300" style="border-radius: 5px; border: 1px solid #29622d;">

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/f3ef59cb-5f9e-43ec-ae71-0c5899c29ce6.png" width="900" style="border-radius: 5px; border: 1px solid #29622d;">

    Try it yourself:

    https://play.creaticode.com/projects/32f4e2bffc79c532fbeba129

    1 Reply Last reply
    0
    • CreatiCodeI CreatiCode pinned this topic on

    Hello! It looks like you're interested in this conversation, but you don't have an account yet.

    Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

    With your input, this post could be even better 💗

    Register Login
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • CreatiCode