Dragging Objects
-
Introduction
You can allow the user to drag any 3D object in the scene, such as placing an object at a particular position. Also, you can listen to dragging events, so that you can run certain blocks when the dragging has just started, is in progress, or has just stopped.
Set Dragging Mode
You can make the currently selected sprite object draggable using this block:
There are 4 dragging mode options. Let’s go through each of them:
Dragging Mode = “Camera”
In this mode, the object can be dragged based on the camera view. In other words, you can drag it up/down/left/right from the camera’s point of view, but you can not drag it closer to the camera or further away from the camera. The other inputs (X/Y/Z) will be ignored, since the dragging direction is controlled by the camera.
For example, for the sphere, we set its dragging mode to “Camera”. As we rotate the camera to view the sphere from different angles, we can always drag the ball across our screen.
Dragging Mode = “Line”
When the dragging mode is “Line”, the object can only be dragging along a line, no matter what’s the camera’s view angle.
The direction of the line can be set using the next 3 inputs (X/Y/Z).
For example, in this program, the sphere can only be dragged along the direction specified, which is the same direction as the line that points from the origin point (x=0, y=0, z=0) to the specified point (x=0, y=0, z=1).
For another example, if the direction is changed to (x=1, y=1, z=0), then the sphere can only move along a diagonal line on the ground:
Note that the direction works the same way if the sphere is not at the world’s center, since we are not saying the sphere can only be dragged along a line, but it can be dragged along a direction.
Dragging Mode = “Plane”
In this mode, you can make the object move freely on a 2D plane, and you can set the “normal” of this plane using the X/Y/Z inputs. The “normal” of a plane is the direction of a line that goes through the plane with a right angle.
For example, the Z-axis is in the normal direction of the ground, so when we set the direction to (x=0, y=0, z=1), the sphere will be draggable on the ground, no matter what’s the camera’s view angle.
For another example, if we set the normal direction to be the X-axis’s direction, then the sphere will only be draggable along the Y or Z direction, and its X position will never change when we drag it.
Dragging Mode = “None”
When you set the dragging mode to “None”, the sphere will not be draggable any more.
Set Dragging Limits
You can set a box-shaped region, so that the user can only drag an object within this region.
This block takes 6 inputs, which are the minimum X/Y/Z position values, and also maximum X/Y/Z position values. The position of the object being dragged can not go below the minimum or above the maximum.
Here is an example. We set the limits for both X and Y positions to be between -500 and 500, as indicated by the orange box. We can no longer drag the 6-colored box outside this region:
Dragged Object Name
When an object is dragged, you can find out its name using this reporter block:
You can also use the value monitor in the stage window to read this block’s value. For example, in this program, we have 2 spheres named “1” and “2”. Both are draggable along the Z direction. You can observe how the “dragged object name” changes.
Dragging Events
There are 3 hat blocks for you to handle dragging-related events. All of them are only triggered by dragging events for objects created by the current sprite.
As the text indicates, this block is only triggered once when we press down the pointer on that object to start dragging.
This block will be triggered repeatedly, whenever the pointer drags the object to a slightly different position.
This block is triggered once when we have finished dragging and released the pointer.
An Example
In this example, we set the sphere to yellow when we start to drag the sphere, blue while we drag it, and purple when we are done dragging it.
This is the result of the program. Note that to make it yellow, you have to keep the pointer still after pressing it down, otherwise the sphere will turn to blue right away. -
-
-
-
-
-
-
-
-
-
-
-
-
-
-