Author Topic: Retriving objís position value  (Read 4472 times)

Anonymous

  • Guest
Retriving objís position value
« on: June 30, 2004, 06:26:59 pm »
Hi Egon,

using which method can I get the current axisís value of an object (for example the current x values in world units), and in which way can I set it to another value (for example x+10)?

Bye and thanks

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11777
    • View Profile
    • http://www.jpct.net
Retriving objís position value
« Reply #1 on: June 30, 2004, 07:37:49 pm »
You can get the position, i.e. the current translation from the object by calling getTranslation(). But thats the translation relative to the object's origin that can be set using setOrigin(). If you do so, you have to add your origin's coordinates to the value retrieved by getTranslation().
However, to move your object, this is not required at all. Just call translate() to move the object relative to it's current position.

Hope this helps.

Offline Tornado7

  • byte
  • *
  • Posts: 45
    • View Profile
Retriving objís position value
« Reply #2 on: July 01, 2004, 01:54:33 pm »
Ok, I've got it, but I'm experimenting two new problems; here follows the code used for to load the moving objs:

Code: [Select]

Object3D[] miscArray=Loader.load3DS(getDocumentBase(),"3ds/oggetti/"+name_obj, 20f);
         Object3D miscObj=miscArray[0];
            miscObj.setCenter(SimpleVector.ORIGIN);
            miscObj.translate (-462, -30, -186);
            miscObj.rotateX((float)-Math.PI/2);
            miscObj.rotateMesh();
            miscObj.setRotationMatrix(new Matrix());
     
            miscObj.createTriangleStrips(2);
            miscObj.setCollisionMode(Object3D.COLLISION_CHECK_OTHERS);
         miscObj.setCollisionOptimization(Object3D.COLLISION_DETECTION_OPTIMIZED);
           
            miscObj.build();
            theWorld.addObject(miscObj);



When I select it using the following in Main():

Code: [Select]

if (clicked) {
                   
                         SimpleVector ray=Interact2D.reproject2D3D(camera, buffer, mouseX, mouseY);
                         int[] res=Interact2D.pickPolygon(theWorld.getVisibilityList(), ray);

                         if (res!=null) {
                             Object3D pickedObjs=theWorld.getObject(Interact2D.getObjectID(res));
                             feritoID = pickedObjs.getName();
                         }
                                               
                         clicked=false;
                         
                      }  



and press 'w' key, the obj has to move on his x axis; so, I've used the following code in timer():

Code: [Select]

if ((obj_up) && (feritoID != "none")){
               
                Object3D oggetto;
                oggetto=theWorld.getObjectByName(feritoID);
                SimpleVector coordinate = oggetto.getTranslation();
                coordinate.x=-462.1f;
                oggetto.translate(coordinate);
                         
                }


even if I've changed the x value with a small value (0.1) it covers a long distance in the applet; another problem is collision detection doesn't work and the obj compenetrate the others. Any suggestion?  :?

Bye and thanks

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11777
    • View Profile
    • http://www.jpct.net
Retriving objís position value
« Reply #3 on: July 01, 2004, 06:17:28 pm »
NEVER do this:

Code: [Select]
feritoID != "none"

This will always be true, because Strings are immutable in Java, so even if the text of the Strings is the same, these are two different instances and therefor, your != will always be true. Always check Strings for equality by using equals(). But that aside, i obviously wasn't clear enough on how translate() works.
translate() translates RELATIVE to the current position, i.e. if you want the object to travel 0.1 units in x-direction, you simply do translate(0.1,0,0). In your code, you are translating by the current position+, i.e. if your current position is (10,0,0) and you add 0.1 to x, you'll translate by (10.1,0,0). That explains the jumps your are experiencing.
And collision detection can only work if applied...it's not implicit when doing a translation.
In your case, this will somehow look like this (the 5f and 10f are wild guessing from me here):

Code: [Select]
if ((obj_up) && (!feritoID.equals("none"))){
                   
   Object3D oggetto=theWorld.getObjectByName(feritoID);
   SimpleVector trsl= new SimpleVector(5f,0f,0f);
   trsl=oggetto.checkForCollisionSpherical(trls, 10f);
   oggetto.translate(trsl);
                         
}


Have a look at the documentation and/or the manual for the different collision detection methods jPCT offers and see what's best suited for your application. Make sure that your Object3Ds are having the appropriate collision modes set.
And last but not least: Don't do this in timer(). It's a bad habbit i had in the past and it sadly made it into the bounce sources and earlier fps-sources. Have a look at the refactored source of fps or the new car-example to see how to do it in a better way.

Offline Tornado7

  • byte
  • *
  • Posts: 45
    • View Profile
Retriving objís position value
« Reply #4 on: July 02, 2004, 01:54:33 pm »
Ok, now it moves as I wish  :D , but I've still problems with collision detection :( : I've setted the "moving obj" using this code:

Code: [Select]

miscObj.setCollisionMode(Object3D.COLLISION_CHECK_SELF);
miscObj.setCollisionMode(Object3D.COLLISION_CHECK_OTHERS);
miscObj.setCollisionOptimization(Object3D.COLLISION_DETECTION_OPTIMIZED);


and the world's objs with the following:

Code: [Select]

mondo.setCollisionMode(Object3D.COLLISION_CHECK_OTHERS);
mondo.setCollisionOptimization(Object3D.COLLISION_DETECTION_OPTIMIZED);


but, even using your code, it still compenetrates the world's objs  :?:

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11777
    • View Profile
    • http://www.jpct.net
Retriving objís position value
« Reply #5 on: July 02, 2004, 02:49:05 pm »
Edit: Now i see...you are setting the collision mode wrong. You can't set SELF and OTHERS one after the other...you have to set them both at a time by doing a setCollisionMode(...SELF | ...OTHERS);

And my former posting:

It may also be a "problem" with the size of your objects. Try to set Config.collideOffset to higher values (try some very high value like 10000 first) and play around with the size of the sphere (the 10f in the example code that i gave you). The sphere is virtually positioned around the object's center. Maybe it just doesn't touch the obstacles with a radius of 10f from this position.

Offline Tornado7

  • byte
  • *
  • Posts: 45
    • View Profile
Retriving objís position value
« Reply #6 on: July 05, 2004, 07:13:16 pm »
ok, now collision detecyion works but, you guess..... I've still some problem.....  :D I'm experimenting using check types: checkForCollisionSpherical isn't good enough in my situation; I've tried checkForCollisionEllipsoid and I've an odd behaviuor; I've defined the following:
Code: [Select]

if ((obj_bw) && (!feritoID.equals("none"))){
                   
    Object3D oggetto=theWorld.getObjectByName(feritoID);
    SimpleVector collision= new SimpleVector(44f,15f,10f);
    SimpleVector translate= new SimpleVector(-5f,0f,0f);
    translate=oggetto.checkForCollisionEllipsoid(translate, collision, 5);
    oggetto.translate(translate);
                         
                }

using collision with the above SimpleVector's values the obj compenetrate a bit in the wall; if I change the collision values into:
Code: [Select]

SimpleVector collision= new SimpleVector(45f,15f,10f);

it's too much off the wall: why changing by just 1 unit I've a so different result?
I've even tried to use checkForCollision method:
Code: [Select]

if ((obj_bw) && (!feritoID.equals("none"))){
                   
    Object3D oggetto=theWorld.getObjectByName(feritoID);
    SimpleVector direction= new SimpleVector(1,0,0);
    SimpleVector translate= new SimpleVector(-5f,0f,0f);
    translate=oggetto.checkForCollision(direction, 10);
    oggetto.translate(translate);
                         
                }

but the compiler says:
Code: [Select]

ThreeDSimApplet.java:864: incompatible types
found   : int
required: com.threed.jpct.SimpleVector
    translate=oggetto.checkForCollision(direction, 10);
                                                           ^
Note: ThreeDSimApplet.java uses or overrides a deprecated API.
Note: Recompile with -deprecation for details.
1 error

any explaination. Bye and thanks  :)

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11777
    • View Profile
    • http://www.jpct.net
Retriving objís position value
« Reply #7 on: July 05, 2004, 07:24:31 pm »
checkForCollision is more or less a relic from the past of jPCT. It doesn't return a SimpleVector like the others do. It just returns an ObjectID. Anyway, i don't think that you'll get satisfying results with it, so don't bother with it too much. About the ellipsoid collision detection: That's hard to tell without knowing/seeing what you are actually doing. You may try to enable a different mode for ellipsoid collision detection by doing a call to
Code: [Select]
setEllipsoidMode(Object3D.ELLIPSOID_TRANSFORMED);
The default behaviour of ellipsoid collision detection is ELLIPSOID_ALLIGNED which may lead to unsatistfying results in your case.

Offline Tornado7

  • byte
  • *
  • Posts: 45
    • View Profile
Retriving objís position value
« Reply #8 on: July 07, 2004, 11:35:40 am »
No, it has no effect.....but, doing a better collision detection calibration I've got a good behaviour.... Bye and thanks :D