Node.collideWith problem

I’m not sure if jme3 developers are aware of this but Node.collideWith returns different (wrong) results in nightly build then it does in beta1.

If you need me to provide more info just specify it :slight_smile: (I’ve already checked the log, there’s nothing related)

Please make a test case if you think that is so, just you saying that isn’t really a proof of this, it could be all kinds of things e.g. you calling updateGeometricState or something in your application. If you can show the wrong behavior in a simple test case we’ll be happy to investigate, I for one don’t have issues with collision or picking?

uff, done.

If you run this app (res 1280x720 or any other with same ratio) and click in the green polygon the collision result differ between beta1 and nightly build.



[java]package cz.unkown.jme3.test;



import java.util.ArrayList;



import com.jme3.app.SimpleApplication;

import com.jme3.collision.CollisionResult;

import com.jme3.collision.CollisionResults;

import com.jme3.input.MouseInput;

import com.jme3.input.controls.ActionListener;

import com.jme3.input.controls.MouseButtonTrigger;

import com.jme3.material.Material;

import com.jme3.math.ColorRGBA;

import com.jme3.math.Ray;

import com.jme3.math.Vector2f;

import com.jme3.math.Vector3f;

import com.jme3.scene.Geometry;

import com.jme3.scene.Mesh;

import com.jme3.scene.VertexBuffer.Type;

import com.jme3.scene.shape.Box;

import com.jme3.util.BufferUtils;



public class CollisionBugApp extends SimpleApplication {



public static void main(String[] args) {

CollisionBugApp app = new CollisionBugApp();

app.start();

}



@Override

public void simpleInitApp() {

flyCam.setEnabled(false);

initCamera();

initScene();

registerInputListener();

}



@Override

public void simpleUpdate(float tpf) {

super.simpleUpdate(tpf);

}



private void initScene() {

// Background

Box b = new Box(Vector3f.ZERO, 640, 360, 1);

Geometry geom = new Geometry("Background", b);

geom.setLocalTranslation(640, 360, -999);



Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");

ColorRGBA color = new ColorRGBA(1, 0, 0, 0);

mat.setColor("Color", color);

geom.setMaterial(mat);



// Area node

Mesh m = new Mesh();

ArrayList<Vector3f> vertices = new ArrayList<>();

vertices.add(new Vector3f(1169f, 309f, -799f));

vertices.add(new Vector3f(1222f, 290f, -799f));

vertices.add(new Vector3f(1222f, 57f, -799f));

vertices.add(new Vector3f(1169f, 104f, -799f));

int[] indexes = {2, 1, 0, 3, 2, 0};

m.setBuffer(Type.Position, 3, BufferUtils.createFloatBuffer(vertices.toArray(new Vector3f[vertices.size()])));

m.setBuffer(Type.Index, 1, BufferUtils.createIntBuffer(indexes));

m.updateBound();

Geometry geom2 = new Geometry("Area", m);

Material mat2 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");

ColorRGBA color2 = new ColorRGBA(0, 1, 0, 0f);

mat2.setColor("Color", color2);

geom2.setMaterial(mat2);



rootNode.attachChild(geom);

rootNode.attachChild(geom2);

}



private void registerInputListener() {

inputManager.addMapping("click", new MouseButtonTrigger(MouseInput.BUTTON_LEFT));

inputManager.addListener(new ActionListener() {

@Override

public void onAction(String name, boolean isPressed, float tpf) {

if("click".equals(name) && isPressed) {

CollisionResults results = new CollisionResults();

Vector2f click2d = inputManager.getCursorPosition();

Vector3f click3d = cam.getWorldCoordinates(new Vector2f(click2d.x, click2d.y), 0f).clone();

Vector3f dir = cam.getWorldCoordinates(new Vector2f(click2d.x, click2d.y), 1f).subtractLocal(click3d).normalizeLocal();

Ray ray = new Ray(click3d, dir);

rootNode.collideWith(ray, results);



for (CollisionResult collisionResult : results) {

System.out.println(collisionResult.getGeometry().getName());

}

System.out.println("


");
}
}
}, "click");
}

private void initCamera() {
cam.setParallelProjection(true);

cam.setFrustum(0.1f, 1000, -640, 640, 360, -360);

Vector3f loc = new Vector3f(640, 360, 2.0f);
Vector3f left = new Vector3f(-1.0f, 0.0f, 0.0f);
Vector3f up = new Vector3f(0.0f, 1.0f, 0.0f);
Vector3f dir = new Vector3f(0.0f, 0.0f, -1.0f);
cam.setLocation(loc);
cam.setFrame(loc, left, up, dir);
}

}
[/java]

Its the cam frustum settings, honestly I don’t understand what you’re doing there? And how the big custom mesh is supposed to look/work? When I enable normal cam settings its all very strange but collision works as expected ^^ I added a box that is moved to the collision point and it also looks kind of ok…

I’m creating a point and click adventure which is using prerendered backgrounds and real 3d models for characters, items and so on…

The red box is background and the green polygon is an action area for door, if you click it the door opens :slight_smile:

I don’t want to offend anyone but shouldn’t this be considired a bug since it worked in previous version/build? :slight_smile:

Or if it’s not a bug what am I supposed to do to achieve exactly the same behaviour?

Well the thing is the background is in front of the green thingy, just the cam frumstum settings make it look like it wasn’t. Thats why I ask about the cam settings and why you did it this way.

Ok, maybe I’m missing something but the background’s z is -999 and green node’s z is -799 and cam’s location and direction is set to this:

[java]Vector3f loc = new Vector3f(640, 360, 2.0f);

Vector3f dir = new Vector3f(0.0f, 0.0f, -1.0f);[/java]



so the green node should be in front of the background shouldn’t it?

Set it up with only parallel projection and without inverting the camera, then it works. I wonder why you modify the cam settings this way, the picking code is obviously not working then as it works with the cam direction.

I feel stupid but I still don’t get it.



First:

This is cams location and direction:

[java]Vector3f loc = new Vector3f(640, 360, 2.0f);

Vector3f dir = new Vector3f(0.0f, 0.0f, -1.0f);[/java]



Background’s z is -999, green node’s z is -799.

When I click on the green node and print the click3d and dir vectors created in the action listener I see this:



click3d:

x: 1198.9999

y: 215.99998

z: 1.9000549



dir:

x: 0.0

y: 0.0

z: -0.99999994



so I think the ray should go through both the green node and the background as it does in beta.



Second:

I’m not sure what so strange about the way I modify the cam settings.

You invert the of the camera axes basically, you’d have to invert the clicking code as well… Why don’t you just set a rotation on the camera?

When I print the cam left, up and direction vectors before the modification they are exactly the same (so my code has no effect).



So I changed the initCamera to this:

[java]cam.setParallelProjection(true);

cam.setFrustum(0.1f, 1000, -640, 640, 360, -360);

Vector3f loc = new Vector3f(640, 360, 2.0f);

cam.setLocation(loc);[/java]



Where exactly I’m doing the inversion?

I don’t know, it works completely different with the cam settings commented out and I still don’t understand the test and the object locations. I ask again: why do you set the values like you do?

Ok, my english is not very good but I’ll try to explain.

As I said before I’m working on a point and click adventure with prerendered backgrounds. Size of every background is 1280x720 pixels.

So I create a box with XYZ dimensions like this 1280x720x2 and place its bottom left corner to 0/0/-999. It’s easier to do some calculations this way.

Other objects like characters or items will be placed in the space between the camera and the background (z range 0 → -1000).

Hence the frustum settings and cam location:

[java]cam.setFrustum(0.1f, 1000, -640, 640, 360, -360);

Vector3f loc = new Vector3f(640, 360, 2.0f);[/java]



The z coordinate of camera just has to be bigger than 0. It doesn’t matter if it is 2 or 10.



Hope this helps to understand my code.

I really think I’m not doing anything bad with the camera and that it’s a bug since it works in beta and not in nightly.

Maybe but to bugtrack this its important that what the testcase is supposed to do is clear ^^

I tried to explain the reasons for the settings but I understand that it’s clear to me because I work on it. Is there still something about the testcase that is not clear?

I don’t know, I don’t understand how you come up with your values for the background, e.g. the z-values of the the vertices, did you make them by trying?

No, as I said before I’m placing all objects between background (z: -999) and camera (z: 2).

It’s true that for the purpose of the testcase it could be changed to this (the bug remains):



The meshes:

[java] // Background

Box b = new Box(Vector3f.ZERO, 640, 360, 1);

Geometry geom = new Geometry(“Background”, b);

geom.setLocalTranslation(640, 360, -3);



Material mat = new Material(assetManager, “Common/MatDefs/Misc/Unshaded.j3md”);

ColorRGBA color = new ColorRGBA(1, 0, 0, 0);

mat.setColor(“Color”, color);

geom.setMaterial(mat);



// Area node

Mesh m = new Mesh();

ArrayList<Vector3f> vertices = new ArrayList<>();

vertices.add(new Vector3f(1169f, 309f, -1f));

vertices.add(new Vector3f(1222f, 290f, -1f));

vertices.add(new Vector3f(1222f, 57f, -1f));

vertices.add(new Vector3f(1169f, 104f, -1f));[/java]



Frustum and camera settings:

[java] cam.setParallelProjection(true);

cam.setFrustum(0.1f, 4, -640, 640, 360, -360);

Vector3f loc = new Vector3f(640, 360, 0.0f);

cam.setLocation(loc);[/java]



It’s important that z coord of the green node is lower z coord of the background.

So is this a bug or not?

I don’t know yet, else I’d have answered :roll:. The other devs have problems understanding your code and values too.