Libgdx get a 3d model object 2D screen position - libgdx

Is there a way to get a 3d object position in the screen 2d? How?
I find unproject but it is the reverse that I want.

OK I find it:
Vector3 pos2D = decal.getPosition().cpy();
camera.project(pos2D);
Logger.E(pos2D.x+" "+pos2D.y);

Related

Converting Nose's 3D Position values to 2D values?

Just wondering if this has been attempted before.
I'm making a game where by 2D objects fall from the top and I use my nose to 'catch' those objects.
Currently I have it set up in such a way that if the X and Y values for the nose and 2D object are the same, it increases the counter.
However I've noticed that the position for 3D and 2D objects both reflect very different values.
I've tried using null objects to contain the 2D canvas, but it didn't work either.
Messing around with tolerance values didn't achieve the desired effect.
Equals Patch
First number is taken from the null object, second number is taken from the nose's position.
From nullobject: -0.09079
From nose: 0.00108
Is this something to do with limitations or am I doing something wrongly here? Thanks for taking your time to read this :(
Here is a project that translates 3D nose position to 2D screen space. I made a video on how to do it: video and here is a link to free download.
It requires a few lines of script and the Scene Module to project the 3D position to 2D screen space.
const Scene = require('Scene');
const Patches = require('Patches');
Promise.all([
// The 3D Object or 3D Point we want to track
Scene.root.findFirst('Nose3D'),
]).then(function (results) {
// Define variable names for items we found
const nose3D = results[0];
// This transforms the world coordinate of the 3D Object to a screen coordinate.
var nose2D = Scene.projectToScreen(nose3D.worldTransform.position)
// Get the Nose3D Position, then set the projectToScreen point Nose2D
Patches.outputs.getPoint("Nose3D").then(pointSignal => {
Patches.inputs.setPoint2D('Nose2D', nose2D);
});
});

libgdx: how do you position a 3d model instance with Othographic Camera?

I have a Screen class that uses Othographic Camera and want to put a 3d model on it.
#Override
public void show() {
....
mCamera = new OrthographicCamera();
mCamera.setToOrtho(false, width * sclWidth, height * sclWidth);
....
//3d instance initialization
modelBatch = new ModelBatch();
ModelBuilder modelBuilder = new ModelBuilder();
model = modelBuilder.createBox(0.5f,0.5f,0.5f, new Material(ColorAttribute.createDiffuse(Color.GREEN)), VertexAttributes.Usage.Position| VertexAttributes.Usage.Normal);
modelInstance = new ModelInstance(model, 128,128,128);
modelInstance.transform.set(mCamera.invProjectionView);
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(63 / 255f, 128 / 255f, 70 / 255f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
//mCamera.rotateAround(Vector3.Zero, new Vector3(0,1,0),1f);
mCamera.update();
mBatch.setProjectionMatrix(mCamera.combined);
mBatch.begin();
mBatch.draw(img, 128*10, 0);
mBatch.end();
modelBatch.begin(mCamera);
modelBatch.render(modelInstance);
modelBatch.end();
}
It is in 2d view and I can put any 2d sprite with x and y on the screen according to the screen width and height.
However when doing 3d models, it is completely different.
The 3d model is stretched according to the camera screen and rendered in the center of the screen. I couldn't find the 3d model setX or setY/SetZ functions.
How should the 3d model be positioned and what functions should I use? Any advice or direction to tutorials will be thankful.
update:
camera position: (768.0,192.0,0.0)
Camera projection: [0.0012019231|0.0|0.0|-0.0]
[0.0|0.0021378205|0.0|-0.0]
[0.0|0.0|-0.02|-1.0]
[0.0|0.0|0.0|1.0]
First of all, you instantiated your model instance at coordinates 128, 128, 128. I'm not sure where you have your camera positioned, but if you want a centered object, the X and Y of the model instance should match those of the camera position.
Also, if you want the entire model to be visible, you need to move it away in the Z direction. The camera looks down the -Z axis, so a model instance's position's Z must be less than the Z position of the camera to be visible.
Your main issue is this line, which should be removed:
modelInstance.transform.set(mCamera.invProjectionView);
You definitely do not want to apply the camera's projection matrix and use it as the model's transform matrix.
A vertex position is typically (as in ModelBatch's default shader) mapped from world coordinates to screen coordinates by multiplying its position by a series of matrices. The transform matrix describes the ModelInstance's position, rotation, and scale, so it translates the original model's vertex positions into world space. The view matrix then translates the vertex to it's camera-relative location (camera space, aka view space). And then the projection matrix translates the vertex to screen space (projects it to the rectangular screen). Since the view and projection matrices are both defined by the camera, the can be pre-multiplied and passed to the batch all at once (camera.combined).
In the shader, each vertex position is multiplied by the matrices to get it into screen space.
So to move a model instance around in the world, you perform actions on its transform matrix, such as modelInstance.transform.translate(x,y,z). You generally should never need to call set on it. The line modelBatch.begin(mCamera); takes care of the camera.combined matrix for you under the hood.
When working with SpriteBatch in 2D, you are placing sprites directly in world space, since there is no source Model with defined vertex positions. This is why there is normally no need to use a transform matrix when using SpriteBatch (although it can be used to move the entire plane of sprites into some place in 3D world space).

Libgdx rotating ellipse for collision detection

I try to use 2 ellipses to detect a collision if they overlap. I have to rotate the ellipses but I can't figure out how this works. I'm working with the "com.badlogic.gdx.math.Ellipse" class but it seems to have no method for rotating. Any ideas? Thx in advance!!
Unfortunately, LibGDX doesn't have in-built rotating functions for ellipses.
Instead, I'd either be resorting to a circle in which rotation does not matter, or use polygons to check intersection.
Polygons are formed through an array of float values (vertices), where, every even element of the array is the horizontal component (x) and the odd, the vertical component (y).
Polygon polygon1 = new Polygon(vertexSet1);
Polygon polygon2 = new Polygon(vertexSet2);
Then, by using an Intersector, you can then check whether these polygons have intersected. The more vertices, the more accurate your shape will be. Just remember to have 6 or more elements in your vertex array, as the 6 floats will give 3 (x, y) points which is the minimum required for a polygon.
if (intersector.overlapConvexPolygons(polygon1, polygon2) {
//do your intersection code
}
The polygons themselves have commands to translate, scale and rotate, allowing for the rotations you mentioned above.

AS3 - Finding an objects x and y position relative to the stage

I'm new to ActionScript 3 and I have a character which you can control, the screen scrolls right along the stage and he can fire missiles.
The problem I'm getting is the missiles are created via these co-ords:
bullet.x = hero.mc.x;
bullet.y = hero.mc.y
These work fine untill the screen has scrolled to the right. I assume it's because the bullets are being spawned as a result of them using the canvas x,y and not the stages x,y
So i'm wondering how to find out the x and y of my hero in relative to the canvas so i can spawn the missiles on top of him!
Thanks, and if you need any more information let me know, I'm new to all this. Thank you.
You can do that with localToGlobal and globalToLocal. Your solution would be something like:
bulletPos = bullet.parent.localToGlobal(new Point(bullet.x, bullet.y));
Beware, though, as those are last resort functions. Normally, you'd have all your elements using the same 'layer', so comparisons are easier and faster.

Java3D Zoon- Object starts disppearing az I zoom on it

First of all, I would like to say that I'm a newbie in Java3D. Please bear with my ignorance.
I have made an application with Java3D and I have the following problems with zooming.
It seems the MouseWheelZoom behaviour of Java3D moves the object along Z-axis. On the scene my Z-axis is not out of plane so by using MouseWheelZoom , the object doesn't get closer but it get out of screen. Is there a way to set the zoom direction to an arbitrary direction?
I have got around the problem by using MouseWheelListener and changing the viewing platform based on zoom steps. But there is another problem now. As the object gets closer than a certain distance, some parts of the object ( usually the corners) start disappearing so I can't zoom as much as I desire.
Could you please help ?
Regards,
Hassan
Question:
I guess you use an OrbitBehavior for MouseControl like:
orbit = new OrbitBehavior(canvas3d, OrbitBehavior.REVERSE_ALL);
If that is the case then try
orbit.setZoomFactor(-1d);
To reverse the zoom direction (the default zoom factor is +1d).
To your 2. Question:
You have to set a BoundingLeaf on your PlatformGeometry to encapsulate your "viewing area".
Try something like this
defaultBounds = new BoundingSphere(new Point3d(radiusGameMap, 0.0, radiusGameMap),
radiusGameMap * 6.0d);
BoundingLeaf boundingLeaf = new BoundingLeaf(defaultBounds);
PlatformGeometry platformGeom = new PlatformGeometry();
platformGeom.addChild(boundingLeaf);
where radiusGameMap is a double defining the radius of your whole map.