{"_id":"5b4e90b9d9ea0b00031c6185","category":{"_id":"5b4e90b9d9ea0b00031c615e","project":"5a065a6134873d0010b396ab","version":"5b4e90b9d9ea0b00031c6194","__v":0,"sync":{"url":"","isSync":false},"reference":false,"createdAt":"2018-05-24T14:46:44.164Z","from_sync":false,"order":3,"slug":"enhanced-scenes","title":"Enhanced Scenes"},"project":"5a065a6134873d0010b396ab","user":"57bb7defafc18c0e00529cf1","parentDoc":null,"version":{"_id":"5b4e90b9d9ea0b00031c6194","project":"5a065a6134873d0010b396ab","__v":0,"forked_from":"5b3678bdbb84cb0003b1d8db","createdAt":"2018-04-18T18:19:34.288Z","releaseDate":"2018-04-18T18:19:34.288Z","categories":["5b4e90b9d9ea0b00031c6159","5b4e90b9d9ea0b00031c615a","5b4e90b9d9ea0b00031c615b","5b05923ea5a2f9000357b452","5b05f793c2c86c0003cbe414","5b4e90b9d9ea0b00031c615c","5b4e90b9d9ea0b00031c615d","5b4e90b9d9ea0b00031c615e"],"is_deprecated":false,"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"","version_clean":"1.8.2","version":"1.8.2"},"__v":0,"updates":[],"next":{"pages":[],"description":""},"createdAt":"2017-09-16T01:57:34.992Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":7,"body":"Viro contains a simple but powerful Physics engine. This engine enables developers to build interactive experiences that simulate real world forces. This development guide will go through the basics of creating physics objects and offer an overview of the experiences that can be created with them.\n\nIn Viro, almost every 3D control can be physics-enabled through the [PhysicsBody](https://developer.viromedia.com/virocore/reference/com/viro/core/PhysicsBody.html) object. For example, to make a Box fall due to gravity, simply initialize its parent Node's PhysicsBody with a mass and type, as shown below.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"Box box = new Box(1, 1, 1);\\n\\nNode boxNode = new Node();\\nboxNode.setGeometry(box);\\nboxNode.initPhysicsBody(PhysicsBody.RigidBodyType.DYNAMIC, 1, new PhysicsShapeBox(1, 1, 1));                    \",\n      \"language\": \"java\"\n    }\n  ]\n}\n[/block]\nEach PhyicsBody must have a [RigidBodyType](https://developer.viromedia.com/virocore/reference/com/viro/core/PhysicsBody.RigidBodyType.html) that dictates the behavior of the physics body. Physics bodies must also be given a [PhysicsShape](https://developer.viromedia.com/virocore/reference/com/viro/core/PhysicsShape.html) to represent the collision mesh of the body.\n\n##Physics Body Type\nThere are three types of physics bodies available in Viro: Dynamic, Kinematic, and Static. Each has unique movement and interaction behavior. Note also there is a mass requirement associated with each type.\n[block:parameters]\n{\n  \"data\": {\n    \"h-0\": \"Property\",\n    \"h-1\": \"Static RigidBody\",\n    \"h-2\": \"Kinematic RigidBody\",\n    \"h-3\": \"Dynamic RigidBody\",\n    \"0-0\": \"Mass (in kg)\",\n    \"1-0\": \"Movement Behavior\",\n    \"2-0\": \"Collision Behavior\",\n    \"0-1\": \"- Must have 0 mass.\",\n    \"0-2\": \"- Must have 0 Mass.\",\n    \"0-3\": \"- Positive Mass (cannot be negative).\",\n    \"1-1\": \"- Cannot move.\",\n    \"1-2\": \"- Can be moved under explicit developer/user control.\",\n    \"1-3\": \"- Designed to be moved under simulation (you should not be directly manipulating / animating this object's position). \\n- Can be moved by colliding against other objects, and in response to forces and impulse.\",\n    \"2-1\": \"- Can collide only with dynamic bodies. \\n- Least resource intensive. - Works well with static objects like floor, ground, etc.\",\n    \"2-2\": \"- Can collide with dynamic bodies.\\n- Does not collide with other other kinematic bodies or static bodies.\\n- Cannot be influenced by dynamic bodies. \\n- Behaves like a movable object with infinite mass during collisions (will always push objects away).\\n- Works well with objects you wish to directly move with animations with fine movement detail.\",\n    \"2-3\": \"- Can collide with any body type. \\n- Works well with objects that do not have applied animations, and that are reactive to forces applied in the world.\"\n  },\n  \"cols\": 4,\n  \"rows\": 3\n}\n[/block]\n##Physics Shape\nAfter specifying a type, Viro will need to determine the actual shape of the physics object representing the control's geometry. This can be specified through the [PhysicsShape](https://developer.viromedia.com/virocore/reference/com/viro/core/PhysicsShape.html). There are three kinds of shapes that the physics body accepts: [PhysicsShapeBox](https://developer.viromedia.com/virocore/reference/com/viro/core/PhysicsShapeBox.html), [PhysicsShapeSphere](https://developer.viromedia.com/virocore/reference/com/viro/core/PhysicsShapeSphere.html), and [PhysicsShapeAutoCompound](https://developer.viromedia.com/virocore/reference/com/viro/core/PhysicsShapeAutoCompound.html), as shown below. \n[block:image]\n{\n  \"images\": [\n    {\n      \"image\": [\n        \"https://files.readme.io/1fadf1a-Screen_Shot_2017-09-18_at_5.54.01_PM.png\",\n        \"Screen Shot 2017-09-18 at 5.54.01 PM.png\",\n        381,\n        100,\n        \"#e42720\"\n      ]\n    }\n  ]\n}\n[/block]\n\n[block:parameters]\n{\n  \"data\": {\n    \"h-0\": \"Shape\",\n    \"h-1\": \"Parameters\",\n    \"0-0\": \"PhysicsShapeBox\",\n    \"1-0\": \"PhysicsShapeSphere\",\n    \"2-0\": \"PhysicsShapeAutoCompound\",\n    \"0-1\": \"Accepts width, height, and length. Will be centered around the origin of the Node.\",\n    \"1-1\": \"Accepts a radius parameter. Creates a sphere that will be centered around the origin of the Node.\",\n    \"2-1\": \"No parameters. Viro automatically iterates through the Node's subtree and infers the physics shape.\"\n  },\n  \"cols\": 2,\n  \"rows\": 3\n}\n[/block]\nNote you can toggle the `setDebugDraw` flag on the PhysicsWorld to draw all meshes representing physics shapes. This is demonstrated in the image above, and is primarily used for debugging purposes (it comes at a significant performance cost).\n\nIt is important to note that physics objects cannot be recursively stacked. That is, a Node that is physics-enabled cannot have children that also have their own physics-enabled bodies. There is no hierarchy to physics bodies. Compound *shapes*, however -- where multiple geometric objects are compounded together to form a single physics body -- are supported in Viro.\n[block:api-header]\n{\n  \"title\": \"Physics Body Forces\"\n}\n[/block]\nVarious kinds of forces can be applied to physics-enabled Nodes. Viro automatically manages the reconciling of these forces on a Node's transform when they are applied. \n\n###Constant Forces\nConstant forces can be useful in situations for moving objects in constant motion; for example, when adding thrust to a spaceship being launched into the sky. To apply a constant directional force, invoke `applyForce(Vector magnitude, Vector location)` on the PhysicsBody. For each force, you can specify both its *magnitude* (in newtons) and its *location* (on the object).\n[block:image]\n{\n  \"images\": [\n    {\n      \"image\": [\n        \"https://files.readme.io/b455b72-ezgif.com-crop_3.gif\",\n        \"ezgif.com-crop (3).gif\",\n        250,\n        250,\n        \"#bba2a2\"\n      ]\n    }\n  ]\n}\n[/block]\nRotational forces are also supported. To apply a constant rotational force, use the `applyTorque(Vector torque)` method.\n[block:image]\n{\n  \"images\": [\n    {\n      \"image\": [\n        \"https://files.readme.io/6055046-ezgif.com-crop_4.gif\",\n        \"ezgif.com-crop (4).gif\",\n        250,\n        250,\n        \"#bba3a3\"\n      ]\n    }\n  ]\n}\n[/block]\n###Impulsive Forces and Motion\nImpulse forces are useful in situations where you would like to apply an instantaneous burst of force to an object. These are often useful for launching projectile objects. Rotational impulses are supported as well. The relevant methods are `applyImpulse(Vector impulse, Vector location)` and `applyTorqueImpulse(Vector torque)`.\n[block:image]\n{\n  \"images\": [\n    {\n      \"image\": [\n        \"https://files.readme.io/51bc233-ezgif.com-crop_3.gif\",\n        \"ezgif.com-crop (3).gif\",\n        250,\n        250,\n        \"#baa1a1\"\n      ]\n    }\n  ]\n}\n[/block]\n### Velocity \nDevelopers have access to finer control over object movement via the `velocity``` property on PhysicsBody. Setting this property will override any forces that are already applied to the object.\n\n## Forces and Drag Behavior\nWith Viro, the default behavior of drag events works with physics-enabled Nodes; users are still able to drag objects around the scene. Viro handles this by temporarily converting any dragged physics bodies into kinematic types during the drag, then switching back to the original types upon completion of the drag. This can be used to give users the ability to fling objects toward other dynamic-typed physics bodies. An example of this is shown below.\n[block:image]\n{\n  \"images\": [\n    {\n      \"image\": [\n        \"https://files.readme.io/4ba68fc-ezgif.com-video-to-gif_1.gif\",\n        \"ezgif.com-video-to-gif (1).gif\",\n        600,\n        338,\n        \"#7e8b98\"\n      ],\n      \"caption\": \"Drag and drop physics bodies.\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"title\": \"Physics Body Properties\"\n}\n[/block]\nPhysicsBodies have additional properties that the developer can tune to achieve different kinds of behavior. Two of these are friction and restitution. \n\n**Restitution** controls how \"bouncy\" an object or surface is. When two objects collide, some of the energy of the collision is used to deform the collided objects, some is used rebound the objects from one another, and some is lost to heat. Restitution is a measure of how much of that kinetic energy is used for objects to rebound from one another. To take a real-world example, a massive bowling bowl has a low coefficient of restitution -- it doesn't really bounce on the floor -- whereas a basketball has a high coefficient of restitution. \n\nNote the restitution factor is applied on both the objects involved in the collision. In the image below, that means both the floor and each colliding ball. Restitution of 1.0 implies a perfectly elastic collision: kinetic energy is conserved and the ball will bounce right back up to its initial height. A value of 0.0 represents a completely inelastic collision: the ball will stick to the floor after colliding and have zero velocity.\n[block:image]\n{\n  \"images\": [\n    {\n      \"image\": [\n        \"https://files.readme.io/f0e1dfc-ezgif.com-crop_7.gif\",\n        \"ezgif.com-crop (7).gif\",\n        250,\n        250,\n        \"#b4a4a1\"\n      ]\n    }\n  ]\n}\n[/block]\n**Friction** is used to calculate the force of resistance an object encounters when moving across another. A value of 0.0 implies no friction, whereas a value of 1.0 implies high friction. When two physics bodies are in contact, their respective friction values are used to create a friction coefficient, which is in turn used to compute the applied frictional force for the two colliding objects. In other words, the friction values of both objects are considered. For example, a metal sword has a different coefficient of friction when rubbing against cloth than against wood.\n[block:image]\n{\n  \"images\": [\n    {\n      \"image\": [\n        \"https://files.readme.io/2096b1c-ezgif.com-crop_8.gif\",\n        \"ezgif.com-crop (8).gif\",\n        250,\n        250,\n        \"#bba3a3\"\n      ]\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"title\": \"Physics World\"\n}\n[/block]\nWhile most physics parameters are set through the individual PhysicsBody properties of each object, there are some *global* physics properties that can be modified. These are accessed from the [PhysicsWorld](https://developer.viromedia.com/virocore/reference/com/viro/core/PhysicsWorld.html) object, which is retrieved directly from the Scene. PhysicsWorld has the following properties:\n\n```gravity``` globally accelerates physics bodies in a specific direction. To make objects float without falling, set ```gravity``` to 0.0. The default is -9.8 m/s in the negative Y direction, to simulate gravity on Earth.\n\n```debugDraw``` will render lines representing the physics shapes for all physics objects in the scene. This is a useful tool for debugging physics bodies. It provides developers visual feedback on where the physics objects are, and how they interact with one another.\n[block:api-header]\n{\n  \"title\": \"Collision Events\"\n}\n[/block]\n**Colliding Objects** \n\nDevelopers can register [CollisionListener](https://developer.viromedia.com/virocore/reference/com/viro/core/PhysicsBody.CollisionListener.html)s to receive collision events that occur for a given PhysicsBody. As shown below, upon collision the collided point and normal are both returned in world coordinates. The `tag` of the collided object is also returned, which can be used provide information to the callback about what the PhysicsBody has collided against. \n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"Node node = new Node();\\nPhysicsBody physicsBody = boxNode.initPhysicsBody(PhysicsBody.RigidBodyType.DYNAMIC, 1, new PhysicsShapeBox(1, 1, 1)); \\n\\nphysicsBody.setCollisionListener(new PhysicsBody.CollisionListener() {\\n    public void onCollided(String collidedTag, Vector position, Vector normal) {\\n\\t      // Respond to the collision\\n    }\\n});\",\n      \"language\": \"java\"\n    }\n  ]\n}\n[/block]\n**Physics Ray Hit Test** \n\nIn addition to registering for collision callbacks, developers can actively query the scene for collisions with ray intersection tests. This can be done in two ways:\n\n1) **findCollisionsWithRayAsync(from, to, closest, viroTag)**\nThis shoots a single ray into scene starting at ```from``` and ending at ```to```. The ```viroTag``` enables developers to identify this collisionRay event within the collisionCallback of all in-scene physics bodies. If only the nearest result is desired, set the ```closest``` flag to true. This function returns true if any objects have been hit.\n\n2) **findCollisionsWithShapeAsync(from, to, shapeString, shapeParam, viroTag)**\nThis method fires a physics shape into the scene and determines what physics objects collide with the shape. The ```shapeString``` is either \"box\" or \"sphere\", and ```shapeParam``` are shape parameters as described above for boxes and spheres. If ```from != to```, this method *only* invokes the callback of the closest object that has been hit. Otherwise, if ```from == to``` (if this isn't a collision test along a path), then the collision callback is triggered on all collided objects.\n\nYou can view those methods in greater detail [here](http://docs.viromedia.com/docs/viroscene#methods).","excerpt":"","slug":"physics","type":"basic","title":"Physics"}
Viro contains a simple but powerful Physics engine. This engine enables developers to build interactive experiences that simulate real world forces. This development guide will go through the basics of creating physics objects and offer an overview of the experiences that can be created with them. In Viro, almost every 3D control can be physics-enabled through the [PhysicsBody](https://developer.viromedia.com/virocore/reference/com/viro/core/PhysicsBody.html) object. For example, to make a Box fall due to gravity, simply initialize its parent Node's PhysicsBody with a mass and type, as shown below. [block:code] { "codes": [ { "code": "Box box = new Box(1, 1, 1);\n\nNode boxNode = new Node();\nboxNode.setGeometry(box);\nboxNode.initPhysicsBody(PhysicsBody.RigidBodyType.DYNAMIC, 1, new PhysicsShapeBox(1, 1, 1)); ", "language": "java" } ] } [/block] Each PhyicsBody must have a [RigidBodyType](https://developer.viromedia.com/virocore/reference/com/viro/core/PhysicsBody.RigidBodyType.html) that dictates the behavior of the physics body. Physics bodies must also be given a [PhysicsShape](https://developer.viromedia.com/virocore/reference/com/viro/core/PhysicsShape.html) to represent the collision mesh of the body. ##Physics Body Type There are three types of physics bodies available in Viro: Dynamic, Kinematic, and Static. Each has unique movement and interaction behavior. Note also there is a mass requirement associated with each type. [block:parameters] { "data": { "h-0": "Property", "h-1": "Static RigidBody", "h-2": "Kinematic RigidBody", "h-3": "Dynamic RigidBody", "0-0": "Mass (in kg)", "1-0": "Movement Behavior", "2-0": "Collision Behavior", "0-1": "- Must have 0 mass.", "0-2": "- Must have 0 Mass.", "0-3": "- Positive Mass (cannot be negative).", "1-1": "- Cannot move.", "1-2": "- Can be moved under explicit developer/user control.", "1-3": "- Designed to be moved under simulation (you should not be directly manipulating / animating this object's position). \n- Can be moved by colliding against other objects, and in response to forces and impulse.", "2-1": "- Can collide only with dynamic bodies. \n- Least resource intensive. - Works well with static objects like floor, ground, etc.", "2-2": "- Can collide with dynamic bodies.\n- Does not collide with other other kinematic bodies or static bodies.\n- Cannot be influenced by dynamic bodies. \n- Behaves like a movable object with infinite mass during collisions (will always push objects away).\n- Works well with objects you wish to directly move with animations with fine movement detail.", "2-3": "- Can collide with any body type. \n- Works well with objects that do not have applied animations, and that are reactive to forces applied in the world." }, "cols": 4, "rows": 3 } [/block] ##Physics Shape After specifying a type, Viro will need to determine the actual shape of the physics object representing the control's geometry. This can be specified through the [PhysicsShape](https://developer.viromedia.com/virocore/reference/com/viro/core/PhysicsShape.html). There are three kinds of shapes that the physics body accepts: [PhysicsShapeBox](https://developer.viromedia.com/virocore/reference/com/viro/core/PhysicsShapeBox.html), [PhysicsShapeSphere](https://developer.viromedia.com/virocore/reference/com/viro/core/PhysicsShapeSphere.html), and [PhysicsShapeAutoCompound](https://developer.viromedia.com/virocore/reference/com/viro/core/PhysicsShapeAutoCompound.html), as shown below. [block:image] { "images": [ { "image": [ "https://files.readme.io/1fadf1a-Screen_Shot_2017-09-18_at_5.54.01_PM.png", "Screen Shot 2017-09-18 at 5.54.01 PM.png", 381, 100, "#e42720" ] } ] } [/block] [block:parameters] { "data": { "h-0": "Shape", "h-1": "Parameters", "0-0": "PhysicsShapeBox", "1-0": "PhysicsShapeSphere", "2-0": "PhysicsShapeAutoCompound", "0-1": "Accepts width, height, and length. Will be centered around the origin of the Node.", "1-1": "Accepts a radius parameter. Creates a sphere that will be centered around the origin of the Node.", "2-1": "No parameters. Viro automatically iterates through the Node's subtree and infers the physics shape." }, "cols": 2, "rows": 3 } [/block] Note you can toggle the `setDebugDraw` flag on the PhysicsWorld to draw all meshes representing physics shapes. This is demonstrated in the image above, and is primarily used for debugging purposes (it comes at a significant performance cost). It is important to note that physics objects cannot be recursively stacked. That is, a Node that is physics-enabled cannot have children that also have their own physics-enabled bodies. There is no hierarchy to physics bodies. Compound *shapes*, however -- where multiple geometric objects are compounded together to form a single physics body -- are supported in Viro. [block:api-header] { "title": "Physics Body Forces" } [/block] Various kinds of forces can be applied to physics-enabled Nodes. Viro automatically manages the reconciling of these forces on a Node's transform when they are applied. ###Constant Forces Constant forces can be useful in situations for moving objects in constant motion; for example, when adding thrust to a spaceship being launched into the sky. To apply a constant directional force, invoke `applyForce(Vector magnitude, Vector location)` on the PhysicsBody. For each force, you can specify both its *magnitude* (in newtons) and its *location* (on the object). [block:image] { "images": [ { "image": [ "https://files.readme.io/b455b72-ezgif.com-crop_3.gif", "ezgif.com-crop (3).gif", 250, 250, "#bba2a2" ] } ] } [/block] Rotational forces are also supported. To apply a constant rotational force, use the `applyTorque(Vector torque)` method. [block:image] { "images": [ { "image": [ "https://files.readme.io/6055046-ezgif.com-crop_4.gif", "ezgif.com-crop (4).gif", 250, 250, "#bba3a3" ] } ] } [/block] ###Impulsive Forces and Motion Impulse forces are useful in situations where you would like to apply an instantaneous burst of force to an object. These are often useful for launching projectile objects. Rotational impulses are supported as well. The relevant methods are `applyImpulse(Vector impulse, Vector location)` and `applyTorqueImpulse(Vector torque)`. [block:image] { "images": [ { "image": [ "https://files.readme.io/51bc233-ezgif.com-crop_3.gif", "ezgif.com-crop (3).gif", 250, 250, "#baa1a1" ] } ] } [/block] ### Velocity Developers have access to finer control over object movement via the `velocity``` property on PhysicsBody. Setting this property will override any forces that are already applied to the object. ## Forces and Drag Behavior With Viro, the default behavior of drag events works with physics-enabled Nodes; users are still able to drag objects around the scene. Viro handles this by temporarily converting any dragged physics bodies into kinematic types during the drag, then switching back to the original types upon completion of the drag. This can be used to give users the ability to fling objects toward other dynamic-typed physics bodies. An example of this is shown below. [block:image] { "images": [ { "image": [ "https://files.readme.io/4ba68fc-ezgif.com-video-to-gif_1.gif", "ezgif.com-video-to-gif (1).gif", 600, 338, "#7e8b98" ], "caption": "Drag and drop physics bodies." } ] } [/block] [block:api-header] { "title": "Physics Body Properties" } [/block] PhysicsBodies have additional properties that the developer can tune to achieve different kinds of behavior. Two of these are friction and restitution. **Restitution** controls how "bouncy" an object or surface is. When two objects collide, some of the energy of the collision is used to deform the collided objects, some is used rebound the objects from one another, and some is lost to heat. Restitution is a measure of how much of that kinetic energy is used for objects to rebound from one another. To take a real-world example, a massive bowling bowl has a low coefficient of restitution -- it doesn't really bounce on the floor -- whereas a basketball has a high coefficient of restitution. Note the restitution factor is applied on both the objects involved in the collision. In the image below, that means both the floor and each colliding ball. Restitution of 1.0 implies a perfectly elastic collision: kinetic energy is conserved and the ball will bounce right back up to its initial height. A value of 0.0 represents a completely inelastic collision: the ball will stick to the floor after colliding and have zero velocity. [block:image] { "images": [ { "image": [ "https://files.readme.io/f0e1dfc-ezgif.com-crop_7.gif", "ezgif.com-crop (7).gif", 250, 250, "#b4a4a1" ] } ] } [/block] **Friction** is used to calculate the force of resistance an object encounters when moving across another. A value of 0.0 implies no friction, whereas a value of 1.0 implies high friction. When two physics bodies are in contact, their respective friction values are used to create a friction coefficient, which is in turn used to compute the applied frictional force for the two colliding objects. In other words, the friction values of both objects are considered. For example, a metal sword has a different coefficient of friction when rubbing against cloth than against wood. [block:image] { "images": [ { "image": [ "https://files.readme.io/2096b1c-ezgif.com-crop_8.gif", "ezgif.com-crop (8).gif", 250, 250, "#bba3a3" ] } ] } [/block] [block:api-header] { "title": "Physics World" } [/block] While most physics parameters are set through the individual PhysicsBody properties of each object, there are some *global* physics properties that can be modified. These are accessed from the [PhysicsWorld](https://developer.viromedia.com/virocore/reference/com/viro/core/PhysicsWorld.html) object, which is retrieved directly from the Scene. PhysicsWorld has the following properties: ```gravity``` globally accelerates physics bodies in a specific direction. To make objects float without falling, set ```gravity``` to 0.0. The default is -9.8 m/s in the negative Y direction, to simulate gravity on Earth. ```debugDraw``` will render lines representing the physics shapes for all physics objects in the scene. This is a useful tool for debugging physics bodies. It provides developers visual feedback on where the physics objects are, and how they interact with one another. [block:api-header] { "title": "Collision Events" } [/block] **Colliding Objects** Developers can register [CollisionListener](https://developer.viromedia.com/virocore/reference/com/viro/core/PhysicsBody.CollisionListener.html)s to receive collision events that occur for a given PhysicsBody. As shown below, upon collision the collided point and normal are both returned in world coordinates. The `tag` of the collided object is also returned, which can be used provide information to the callback about what the PhysicsBody has collided against. [block:code] { "codes": [ { "code": "Node node = new Node();\nPhysicsBody physicsBody = boxNode.initPhysicsBody(PhysicsBody.RigidBodyType.DYNAMIC, 1, new PhysicsShapeBox(1, 1, 1)); \n\nphysicsBody.setCollisionListener(new PhysicsBody.CollisionListener() {\n public void onCollided(String collidedTag, Vector position, Vector normal) {\n\t // Respond to the collision\n }\n});", "language": "java" } ] } [/block] **Physics Ray Hit Test** In addition to registering for collision callbacks, developers can actively query the scene for collisions with ray intersection tests. This can be done in two ways: 1) **findCollisionsWithRayAsync(from, to, closest, viroTag)** This shoots a single ray into scene starting at ```from``` and ending at ```to```. The ```viroTag``` enables developers to identify this collisionRay event within the collisionCallback of all in-scene physics bodies. If only the nearest result is desired, set the ```closest``` flag to true. This function returns true if any objects have been hit. 2) **findCollisionsWithShapeAsync(from, to, shapeString, shapeParam, viroTag)** This method fires a physics shape into the scene and determines what physics objects collide with the shape. The ```shapeString``` is either "box" or "sphere", and ```shapeParam``` are shape parameters as described above for boxes and spheres. If ```from != to```, this method *only* invokes the callback of the closest object that has been hit. Otherwise, if ```from == to``` (if this isn't a collision test along a path), then the collision callback is triggered on all collided objects. You can view those methods in greater detail [here](http://docs.viromedia.com/docs/viroscene#methods).