Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (682)
Games in Android Showcase (196)
games submitted by our members
Games in WIP (750)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Problems with picking (need help)  (Read 1107 times)
0 Members and 1 Guest are viewing this topic.
Offline BrainPlug

Senior Newbie

« Posted 2005-08-31 13:42:34 »

Hello everyone,

I have a scene and want to highlight some objects when the mouse is over them. So I took the simple "picking demo" from the page and modified it in that way, that I changed the mousePressed() method to a mouseMoved() method. But running that demo and moving the mouse over the window made the complete application hang after a short time. There is no exeption or anything else that could help me to determine where the failure comes from.

So I need some help. Please try my code and to tell me weather the application hangs too on your machine or not. I am thankful for any suggestions that could help me to solve that problem.

Thanks a lot



import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.util.BitSet;

import javax.vecmath.Color3f;
import javax.vecmath.Point3f;
import javax.vecmath.Vector3f;

import com.xith3d.picking.PickRenderResult;
import com.xith3d.render.CanvasPeer;
import com.xith3d.render.RenderPeer;
import com.xith3d.render.jogl.RenderPeerImpl;
import com.xith3d.scenegraph.Appearance;
import com.xith3d.scenegraph.BranchGroup;
import com.xith3d.scenegraph.Canvas3D;
import com.xith3d.scenegraph.Geometry;
import com.xith3d.scenegraph.GeometryArray;
import com.xith3d.scenegraph.Locale;
import com.xith3d.scenegraph.Node;
import com.xith3d.scenegraph.QuadArray;
import com.xith3d.scenegraph.Shape3D;
import com.xith3d.scenegraph.Switch;
import com.xith3d.scenegraph.View;
import com.xith3d.scenegraph.VirtualUniverse;

public class Picking
    private boolean mouseClicked;
    private int xPos;
    private int yPos;
    private int counter = 0;

    private Switch sw;
    private BitSet displayedPlanes = new BitSet(3);

    public Integer pickParamsMutex = new Integer(0);

    public static void main(String[] args)
        new Picking();

    public Picking()
        VirtualUniverse universe = new VirtualUniverse();

        View view = new View();

        Locale locale = new Locale();

        BranchGroup scene = new BranchGroup();

        displayedPlanes.set(0, 3);
        sw = new Switch(Switch.CHILD_MASK, displayedPlanes);       

        Geometry geo1 = createColouredPlane(-.5f, 0, .2f, .5f, .5f, 0, 1, 0);
        Shape3D sh1 = new Shape3D(geo1, new Appearance());
        sh1.setName("left plane");

        Geometry geo2 = createColouredPlane(0, 0, 0, 1, 1, 1, 0, 0);
        Shape3D sh2 = new Shape3D(geo2, new Appearance());
        sh2.setName("middle plane");

        Geometry geo3 = createColouredPlane(.5f, 0, .2f, .5f, .5f, 0, 0, 1);
        Shape3D sh3 = new Shape3D(geo3, new Appearance());
        sh3.setName("right plane");

        RenderPeer rp = new RenderPeerImpl();
        CanvasPeer cp = rp.makeCanvas(null, 640, 480, 32, false);
        Canvas3D canvas = new Canvas3D();

        cp.getComponent().addMouseMotionListener(new MouseMotionAdapter()
            public void mouseMoved(MouseEvent e)
                synchronized (pickParamsMutex)
                    xPos = e.getX();
                    yPos = e.getY();
                    mouseClicked = true;

        view.getTransform().lookAt(new Vector3f( 0, 0, 1.5f),   // location of eye
                                   new Vector3f( 0, 0, 0),   // center of view
                                   new Vector3f( 0, 1, 0));  // vector pointing up

            synchronized (pickParamsMutex)
                    PickRenderResult[] results = view.pick(canvas, xPos, yPos, 1, 1);
                    if(results != null)
                        displayedPlanes.set(0, 3);
                        for(int i=0; i<results.length; i++)
                            Node node = results.getNodes()[0];
                            String nodeName = node.getName();

                            if(nodeName.equals("left plane"))
                            else if(nodeName.equals("middle plane"))
                            else if(nodeName.equals("right plane"))
                    mouseClicked = false;
    private Geometry createColouredPlane(float xPos,
                                         float yPos,
                                         float zPos,
                                         float xSize,
                                         float ySize,
                                         float redValue,
                                         float greenValue,
                                         float blueValue)
        float xHalf = xSize/2f;
        float yHalf = ySize/2f;

        Point3f[] coords = new Point3f[]
            new Point3f(xPos+xHalf, yPos-yHalf, zPos),
            new Point3f(xPos+xHalf, yPos+yHalf, zPos),
            new Point3f(xPos-xHalf, yPos+yHalf, zPos),
            new Point3f(xPos-xHalf, yPos-yHalf, zPos),

        Color3f[] colors = new Color3f[]
            new Color3f(redValue, greenValue, blueValue),
            new Color3f(redValue, greenValue, blueValue),
            new Color3f(redValue, greenValue, blueValue),
            new Color3f(redValue, greenValue, blueValue)

        QuadArray g = new QuadArray(coords.length, GeometryArray.COORDINATES
                                                 | GeometryArray.COLOR_3);

        return g;

Offline arne

Senior Devvie

money is the worst drug- we should not let it rule

« Reply #1 - Posted 2005-08-31 14:32:23 »

The problem is this: It creates loads of garbage, so after some time the memory is full and the garbage collector has to be run. This will result in a very low frame rate, or it will even hang (if there is a memory leak). To solve the problem use my code (it should be around here somewhere (do a search)) you'll probably also have to modify it to fit your needs more (I believe there's only the version out there that creates much garbage) To reduce garbage, you can simply use global private variables, so you don't have to recreate them every time you perform a pick. My code should work (with the appropriate modification) without creating any garbage !! Smiley


:: JOODE :: Xith3d :: OdeJava ::
Offline BrainPlug

Senior Newbie

« Reply #2 - Posted 2005-09-05 11:11:56 »

Hi arne,

I really thank you. Your picking code

really helped me solving my problems.

I added some lines so the code can now handle IndexedTriangleArrays too (that was what I needed).

if (geomArr instanceof IndexedTriangleArray) {
    IndexedTriangleArray geomArr1 = (IndexedTriangleArray) geomArr;
    int[] indizes = geomArr1.getIndex();
    for (int j = 0; j < indizes.length; j += 3) {
        geomArr.getVertex(indizes[j], pos1);
        geomArr.getVertex(indizes[j + 1], pos2);
        geomArr.getVertex(indizes[j + 2], pos3);
        t.set(pos1, pos2, pos3);
        float f = t.test(transOrigin, transDirection,closestIntersect);
        if (f != -1) { // there is a collision
             f = f / transDirection.length();
             if (f < closestIntersect) {
                   closestIntersect = f;
                   nearest = r;
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline arne

Senior Devvie

money is the worst drug- we should not let it rule

« Reply #3 - Posted 2005-09-05 16:25:28 »

glad I could help you Smiley

:: JOODE :: Xith3d :: OdeJava ::
Pages: [1]
  ignore  |  Print  
You cannot reply to this message, because it is very, very old.

orrenravid (146 views)
2016-07-16 03:57:23

theagentd (215 views)
2016-07-11 14:28:54

Hydroque (298 views)
2016-07-06 05:56:57

Hydroque (446 views)
2016-07-03 08:52:54

GrandCastle (333 views)
2016-07-01 09:13:47

GrandCastle (325 views)
2016-07-01 09:09:45

CopyableCougar4 (381 views)
2016-06-25 16:56:52

Hydroque (362 views)
2016-06-22 02:17:53

SwampChicken (333 views)
2016-06-20 13:22:57

SwampChicken (287 views)
2016-06-20 13:22:49
Making a Dynamic Plugin System
by Hydroque
2016-06-25 00:13:25

Java Data structures
by BinaryMonkL
2016-06-13 21:22:09

Java Data structures
by BinaryMonkL
2016-06-13 21:20:42

FPS Camera Tutorial
by Hydroque
2016-05-22 05:40:58

Website offering 3D Models specifically for games for free
by vusman
2016-05-18 17:23:09

Website offering 3D Models specifically for games for free
by vusman
2016-05-09 08:50:56

Website offering 3D Models specifically for games for free
by vusman
2016-05-06 11:10:21

Website offering 3D Models specifically for games for free
by vusman
2016-04-29 12:56:17 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!