Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (577)
Games in Android Showcase (156)
games submitted by our members
Games in WIP (627)
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 794 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.

ClaasJG (41 views)
2015-04-30 20:33:25

Riven (24 views)
2015-04-30 13:25:12

ClaasJG (46 views)
2015-04-27 13:36:51

BurntPizza (51 views)
2015-04-23 03:42:11

theagentd (52 views)
2015-04-22 16:23:07

Riven (65 views)
2015-04-16 10:48:47

Duke0200 (77 views)
2015-04-16 01:59:01

Fairy Tailz (60 views)
2015-04-14 20:13:12

Riven (64 views)
2015-04-12 21:36:37

bus hotdog (90 views)
2015-04-10 02:39:32
List of Learning Resources
by SilverTiger
2015-05-05 10:20:32

How to: JGO Wiki
by Mac70
2015-02-17 20:56:16

2D Dynamic Lighting
by ThePixelPony
2015-01-01 20:25:42

How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21

Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00 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!