Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (533)
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 677 times)
0 Members and 1 Guest are viewing this topic.
Offline BrainPlug

Senior Newbie





« Posted 2005-08-31 15: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 xith.org 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

Marc

-----------------------------------

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();
        universe.addView(view);

        Locale locale = new Locale();
        universe.addLocale(locale);

        BranchGroup scene = new BranchGroup();
        locale.addBranchGraph(scene);
        scene.setPickable(true);

        displayedPlanes.set(0, 3);
        sw = new Switch(Switch.CHILD_MASK, displayedPlanes);       
        sw.setPickable(true);
        scene.addChild(sw);

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

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

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

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

        cp.getComponent().addMouseMotionListener(new MouseMotionAdapter()
        {
            public void mouseMoved(MouseEvent e)
            {
                synchronized (pickParamsMutex)
                {
                    xPos = e.getX();
                    yPos = e.getY();
                    mouseClicked = true;
                    counter++;
                    System.out.println(counter);
                }
            }
        });

        view.addCanvas3D(canvas);
        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

        while(true)
        {
            view.renderOnce();
            synchronized (pickParamsMutex)
            {
                if(mouseClicked)
                {
                    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"))
                                displayedPlanes.clear(0);
                            else if(nodeName.equals("middle plane"))
                                displayedPlanes.clear(1);
                            else if(nodeName.equals("right plane"))
                                displayedPlanes.clear(2);
                        }
                    }
                    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);
        g.setCoordinates(0,coords);
        g.setColors(0,colors);

        return g;
    }

}
Offline arne

Senior Member




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


« Reply #1 - Posted 2005-08-31 16: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

Arne

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

Senior Newbie





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

Hi arne,

I really thank you. Your picking code

http://192.18.37.44/forums/index.php?topic=5549.msg75762#msg75762

really helped me solving my problems.

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

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
if (geomArr instanceof IndexedTriangleArray) {
    //System.out.println("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;
                   r.setDistance(f);
                   nearest = r;
              }//if
         }//if
    }//for
}//if
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline arne

Senior Member




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


« Reply #3 - Posted 2005-09-05 18: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.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

pw (24 views)
2014-07-24 01:59:36

Riven (24 views)
2014-07-23 21:16:32

Riven (18 views)
2014-07-23 21:07:15

Riven (21 views)
2014-07-23 20:56:16

ctomni231 (50 views)
2014-07-18 06:55:21

Zero Volt (45 views)
2014-07-17 23:47:54

danieldean (36 views)
2014-07-17 23:41:23

MustardPeter (39 views)
2014-07-16 23:30:00

Cero (54 views)
2014-07-16 00:42:17

Riven (55 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org 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‑gaming.org
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!