Question: How do you drag an object in a 3d environment with libgdx?
I manage to pick the object by using a Ray projected from the screen point that I touch and checking if intersects the object's bounding box but I don't understand how to actually update its position. What I'm trying to achieve is a movement on the xz plane, so that when I drag my finger towards the upper ( or lower) end of the screen, the objects move in depth on the plane. Anybody can help?
Related
I am trying to constrain a facemesh to a 3D object so that it will rotate around the surface of the 3D object in response to the face tracker instead of moving off to the side. Currently, the facemesh is a child of the cylinder, but is not attached to it in a way that would rotate around the cylinder surface.
Currently, I am attempting to use the bounding box patch to determine the area to clamp the face mesh to. So far, the mesh is related to the 3D object bounding box, but will not constrain to its surface. Adjustment to the clamp patch variables will move the face mesh within the space, but not constrain it either.
The answer is much simpler than I first attempted. Using the patch editor:
Unpack the 3D rotation from the facefinder.
take the from range from the X and Y positions
look at the maximum and minimum ranges of the face tracker rotation/position values to set To and From Ranges
create a null object and make it a child of the stationary 3D object
make the face mesh a child of the null object
connect the output of the face tracker's rotation/position to the rotation/position value of the null object.
When you're using the Flash/Animate IDE and you select something on the stage with the Free Transform Tool, then move the transformation point (the white circle), what is it actually doing in frame script? DisplayObjects only have x and y properties, there's no transformX or transformY.
I have a MovieClip place on the stage through the Flash IDE with various transformations applied to it, and I would like to be able to replicate those in Actionscript.
When you're creating a display object in IDE, moving transformation point makes IDE move everything inside that object in reverse direction coordinates wise. It's like if you're moving a zero point of a local coordinates system, leaving everything else intact, the contents will then have their coordinates altered.
In order to simulate this behavior, you can nest your display object being created into a container sprite, then when your user drags transformation center, you move the wrapper sprite to the new coordinates and alter its nested object's ( the one with all the contents) coordinates by negative delta between old and new positions of virtual zero.
I am trying to move a sprite to the mouse position on click.
However, the coordinates I am getting from Gdx.input.getX()and Gdx.input.getY() is relative to the top left corner, and the setPosition() method of Sprite is relative to the bottom left corner.
Why is this so, and how do I position my sprite where the mouse was clicked?
Screen coordinates come from Android and use a Y-down frame of reference. Cameras in libgdx by default use a Y-up frame of reference (because OpenGL by convention also uses a Y-up frame of reference).
If you prefer the Y-down frame of reference, you can use camera.setToOrtho(true); method to flip it upside down. You might prefer this if coming from a Flash background.
But in general, the safe way to translate screen coordinates from a touch into the camera's coordinate system is to do the following. This will work regardless of what platform you're on and whatever coordinate system you chose for the camera. For example, for some types of games, you wouldn't even be using a camera that matches the screen resolution, but you'd still want screen coordinates converted to camera coordinates. Also, if you have a camera that moves around the world, this will automatically change the touch point to world coordinates.
tempVector3.set(Gdx.input.getX(), Gdx.input.getY(), 0);
camera.unproject(tempVector3);
//now tempVector3 contains the touch point in camera coordinates.
It uses Vector3 because this also works for 3D cameras.
I've tried unsuccessfully (because of my poor 3D geometry understanding and unfortunate lack of time to dig in) to build a GoogleEarth-like controls for three.js. Maybe someone can help me, or might already have it. Anyways, i think it would be an excellent addition to three.js library.
Here's the specific functionality I am trying to build:
Zoom in with mouse wheel TO MOUSE CURSOR
Rotate around the scene by holding down Shift
Pan by pressing left mouse button.
As a bonus: show a little target icon during 1 and 2 operations above.
I have most trouble with 1, and haven't attempted 2. Panning is easy (there are lots of examples).
Right now I am unable to zoom into the scene so that it stays fixed under the cursor (so I can point at the top right corner of the screen, zoom-in and still see what I had under the cursor).
My thanks in advance,
Alex
I've implemented something similar in a past life. I assume here that you are interacting with a flat plane; conversion of these techniques to a plane tangent to a sphere is left as an exercise for the reader. ;)
Zoom in with mouse wheel TO MOUSE CURSOR
To do this, you'll want to cast a ray into the scene, and note where it hits. You'll then want to translate the eye point of the camera towards that intersection. To feel "correct", you'll want some kind of proportional zoom in instead of fixed steps--for example, each zoom step reduces the distance from the current eye point to the target by 20%, instead of just moving 20 units. This will automatically slow down the camera as it approaches.
Rotate around the scene by holding down Shift
One you hold shift, you'll want left and right mouse movements to orbit about your view point. To do this, you'll need to yaw about an axis perpendicular to your point of intersection. You'll cast a ray into the scene (once shift is held down), note the intersection point, and then rotate your camera's eye point about that axis. Note that you'll need also to reorient the camera to continually point towards that intersection as you rotate, or perhaps to have the eye direction rotate to keep a constant angle with the vector from the eye point to the intersection axis.
Pan by pressing left mouse button.
You simply need to get the eye right vector and eye up vector, and move in the appropriate direction (multiply the mouse dx/dy with the normalized eye right/eye up, and multiply by the timestep for framerate independent movement).
As a bonus: show a little target icon during 1 and 2 operations above.
At the intersection in the scene, add a little object. Once you exit a mode, remove the object.
for your first question you can use this program under mouse wheel
mousewheel = function (event) {
event.preventDefault();
var factor = 15;
var mX = (event.clientX / jQuery(THREE_STUFF.container).width()) * 2 - 1;
var mY = -(event.clientY / jQuery(THREE_STUFF.container).height()) * 2 + 1;
var vector = new THREE.Vector3(mX, mY, 0.1);
vector.unproject(camera);
vector.sub(camera.position);
if (event.deltaY < 0) {
camera.position.addVectors(camera.position, vector.setLength(factor)); trackBallControls.target.addVectors(trackBallControls.target, vector.setLength(factor));
} else {
camera.position.subVectors(camera.position, vector.setLength(factor)); trackBallControls.target.subVectors(trackBallControls.target, vector.setLength(factor));
}
}
I hope this will help you sure.
Same as the question:
I need to drag a component along a programmatically drawn path composed by different kinds of graphic, like lines, curves, etc.
I've already googled it, but with no success.
Any ideas? thanks!
The following is say for a linearly curved path drawn by you. You can use similar method for any direction.
Add an Event listener for click.
(That starts drag)
Track the user's mouse along x
direction for example.
Keep plotting the component's x & y as
Mouse x changes with respect to the
drawn path's x.
Stop relocating as user leaves the
mouse
Start with this if possible & be back with code if you get doubts.
If your drawing part is complete then You can use two dimensional ByteArray. The size of the ByteArray will be the size of your stage, this two-dimensional array will be set to zero, means all your stage locations are set to zero. When any curve or line is drawn , set those locations to one. Now you know at-least where your object can move, the valid locations are those which are set to one.
Now the second the part is how to move an object on the valid path or location using mouse or keyboard.
You will be using Event.EnterFrame for smooth and fast movement of the object,
1--using keyboard.
use up key to move object to upper location if that position or location is set to one else the object will not move Up, same for others.
2-- using mouse move event, detect MouseY position for moving UP or DOWN w.r.t current position of MouseY , and move it respectively if location is set to one.
Hope, this will guide you in right direction...
Will this work? http://www.pixeldigest.com/followthepath.html