Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (406)
games submitted by our members
Games in WIP (293)
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  
  Trying to "attach" a Spatial to a Mesh  (Read 1142 times)
0 Members and 1 Guest are viewing this topic.
Offline CyanPrime
« Posted 2010-12-07 05:33:24 »

Alright, so I'm trying to "attach" a Spatial to a mesh, and by that I mean, and by that I mean I want it to "follow" the primitive it's "attached" to.

I'm doing this cause I've switched from .dae to .md2, and .md2 doesn't have bones, or joints to attach anything to.

This is what I got so far.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
public int getTopSelection(Mesh m){
        m.updateWorldBound(true);
        int c = m.getMeshData().getPrimitiveCount(0);
        double maxHeight = 0;
        int selection = 0;
        Vector3[][] v3 = new Vector3[c][3];
         for(int s = 0; s < c; s++){
            v3[s] = null;
            v3[s] = m.getMeshData().getPrimitive(s, 0, v3[s]);
           
            Vector3 min = new Vector3((float)Math.min((float) Math.min(v3[s][0].getXf(), v3[s][1].getXf()), v3[s][2].getXf()),
                  (float)Math.min((float)Math.min(v3[s][0].getYf(), v3[s][1].getYf()), v3[s][2].getYf()),
                  (float)Math.min((float)Math.min(v3[s][0].getZf(), v3[s][1].getZf()), v3[s][2].getZf()));
           
            Vector3 max = new Vector3((float) Math.max((float)Math.max(v3[s][0].getXf(), v3[s][1].getXf()), v3[s][2].getXf()),
                  (float)Math.max((float)Math.max(v3[s][0].getYf(), v3[s][1].getYf()), v3[s][2].getYf()),
                  (float)Math.max((float)Math.max(v3[s][0].getZf(), v3[s][1].getZf()), v3[s][2].getZf()));
           
            if(max.getY() < maxHeight){
               maxHeight = max.getY();
               selection = s;
            }  
         }
 
         return selection;
       
    }
   
   
    public Vector3[] getPrimitive(Mesh m, int selection){
         m.updateWorldBound(true);
         
     
         Vector3[] v3 = new Vector3[3];
         v3 = m.getMeshData().getPrimitive(selection, 0, v3);
         
         Vector3 min = new Vector3((float)Math.min((float) Math.min(v3[0].getXf(), v3[1].getXf()), v3[2].getXf()),
             (float)Math.min((float)Math.min(v3[0].getYf(), v3[1].getYf()), v3[2].getYf()),
             (float)Math.min((float)Math.min(v3[0].getZf(), v3[1].getZf()), v3[2].getZf()));
       
      Vector3 max = new Vector3((float) Math.max((float)Math.max(v3[0].getXf(), v3[1].getXf()), v3[2].getXf()),
         (float)Math.max((float)Math.max(v3[0].getYf(), v3[1].getYf()), v3[2].getYf()),
         (float)Math.max((float)Math.max(v3[0].getZf(), v3[1].getZf()), v3[2].getZf()));
       
       Vector3 middle = new Vector3(
       (max.getX() - min.getX())/2,
       (max.getY() - min.getY())/2,
       (max.getZ() - min.getZ())/2
       );
       
       Vector3[] send = {min, middle, max};
       
       return send;
       
   }


And this is how I implement it:

in init
1  
player.topSelection = getTopSelection(model);



in update
1  
2  
3  
Vector3[] top = getPrimitive(model, player.topSelection);
        staff.setTranslation(model.getTranslation().getX() + (top[0].getX() + top[2].getX()), model.getTranslation().getY() -(top[0].getY() + top[2].getY()), model.getTranslation().getZ() +(top[0].getZ() + top[2].getZ()));
        staff.setRotation(model.getRotation());


It follows the model's movements on the x axis, and the y axis, but when the primitive is moved down and left the spatial moves down and right, but if I reverse the x it shows up on the other side of the model. How can I fix this?
Offline gouessej

JGO Ninja


Medals: 33
Projects: 1


TUER


« Reply #1 - Posted 2010-12-07 17:16:33 »

Alright, so I'm trying to "attach" a Spatial to a mesh, and by that I mean, and by that I mean I want it to "follow" the primitive it's "attached" to.
Why not using Spatial.getParent()?

1  
int selection = 0;

You should rather use another value as it is confusing. If the top selection is not found or if it is found in the first iteration of the loop, this variable will contain 0.

Why not using the picking feature? Look at my source code... com.ardor3d.intersection.PickingUtil.findPick(...).

Offline CyanPrime
« Reply #2 - Posted 2010-12-07 17:35:41 »

Why not using Spatial.getParent()?

I'm trying to, for example make a person hold a sword. When the person's hand moves the sword needs to move with the hand, not with the person. Using Spatial.getParent() would give me he entire model, and  not the primitive that is it's hand.

[/quote]

1  
int selection = 0;

You should rather use another value as it is confusing. If the top selection is not found or if it is found in the first iteration of the loop, this variable will contain 0.

Good point. I'll set it to -1.


Why not using the picking feature? Look at my source code... com.ardor3d.intersection.PickingUtil.findPick(...).
Well, I find PickingUtil to be slow, so I stay away from it, lol.
Games published by our own members! Check 'em out!
Play the free demo of Revenge of the Titans!
Offline gouessej

JGO Ninja


Medals: 33
Projects: 1


TUER


« Reply #3 - Posted 2010-12-07 19:07:05 »

Well, I find PickingUtil to be slow, so I stay away from it, lol.
It depends on the way you use it but if you have some information that allows at accelerate your computations, you're right by not using it. PickingUtil on triangles can be very slow...

Offline CyanPrime
« Reply #4 - Posted 2010-12-07 21:42:36 »

Alright, now I'm getting a error. (Note my game is still called TorusExample)

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
Throwable caught in MainThread - exiting
com.ardor3d.util.Ardor3dException: Transform is invalid
   at com.ardor3d.math.ValidatingTransform.validate(ValidatingTransform.java:26)
   at com.ardor3d.math.ValidatingTransform.setRotation(ValidatingTransform.java:52)
   at com.ardor3d.scenegraph.Spatial.setRotation(Spatial.java:553)
   at TorusExample.updateExample(TorusExample.java:627)
   at com.ardor3d.example.ExampleBase.update(ExampleBase.java:242)
   at com.ardor3d.framework.FrameHandler.updateFrame(FrameHandler.java:68)
   at com.ardor3d.example.ExampleBase.run(ExampleBase.java:145)
   at java.lang.Thread.run(Unknown Source)


On this line:
1  
staff.setRotation(new Quaternion().fromAngleAxis(Math.toDegrees(a3), Vector3.UNIT_X).multiplyLocal(new Quaternion().fromAngleAxis(Math.toDegrees(b3), Vector3.UNIT_Y)).multiplyLocal(new Quaternion().fromAngleAxis(Math.toDegrees(y3), Vector3.UNIT_Z)));


So This is the formula I'm using:
1  
2  
3  
4  
5  
6  
7  
8  
9  
Vector3[] top = getPrimitive(model, player.topSelection);

        double d3 = Math.sqrt(Math.pow(top[3].getX(), 2) + Math.pow(top[3].getY(), 2) + Math.pow(top[3].getZ(), 2));
       
        double a3 = Math.acos(top[3].getX()/d3);

        double b3 = Math.acos(top[3].getY()/d3);

        double y3 = Math.acos(top[3].getZ()/d3);


And this is my new get primitive.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
public Vector3[] getPrimitive(Mesh m, int selection){
         m.updateWorldBound(true);
         
     
         Vector3[] v3 = new Vector3[3];
         v3 = m.getMeshData().getPrimitive(selection, 0, v3);
         
         Vector3 min = new Vector3((float)Math.min((float) Math.min(v3[0].getXf(), v3[1].getXf()), v3[2].getXf()),
             (float)Math.min((float)Math.min(v3[0].getYf(), v3[1].getYf()), v3[2].getYf()),
             (float)Math.min((float)Math.min(v3[0].getZf(), v3[1].getZf()), v3[2].getZf()));
       
      Vector3 max = new Vector3((float) Math.max((float)Math.max(v3[0].getXf(), v3[1].getXf()), v3[2].getXf()),
         (float)Math.max((float)Math.max(v3[0].getYf(), v3[1].getYf()), v3[2].getYf()),
         (float)Math.max((float)Math.max(v3[0].getZf(), v3[1].getZf()), v3[2].getZf()));
       
       Vector3 middle = new Vector3(
       (max.getX() - min.getX())/2,
       (max.getY() - min.getY())/2,
       (max.getZ() - min.getZ())/2
       );
       
       Vector3 v = new Vector3();
       v = max.subtract(min, v);
       
       v.normalizeLocal();
       
       Vector3[] send = {min, middle, max, v};
       
       return send;
       
   }


How do I fix this?
Offline CyanPrime
« Reply #5 - Posted 2010-12-08 00:53:25 »

Alright, I think I fixed it, but it's still moving down left when the primitive goes down right on translation.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
Vector3[] top = getPrimitive(model, player.topSelection);

        double d3 = Math.sqrt(Math.pow(top[1].getX(), 2) + Math.pow(top[1].getY(), 2) + Math.pow(top[1].getZ(), 2));
       
        double a3 = Math.acos(top[1].getX()/d3);
       
        double b3 = Math.acos(top[1].getY()/d3);
       
        double y3 = Math.acos(top[1].getZ()/d3);

       
        staff.setRotation(new Quaternion().fromAngleAxis(a3, Vector3.UNIT_X).multiplyLocal(new Quaternion().fromAngleAxis(b3, Vector3.UNIT_Y)).multiplyLocal(new Quaternion().fromAngleAxis(y3, Vector3.UNIT_Z)).multiplyLocal(model.getRotation()));
       staff.setTranslation(model.getTranslation().getX() + (top[0].getX() - top[1].getX()), model.getTranslation().getY() -(top[0].getY() + top[2].getY()), model.getTranslation().getZ() +(top[0].getZ() + top[1].getZ()));


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
public Vector3[] getPrimitive(Mesh m, int selection){
         m.updateWorldBound(true);
         
     
         Vector3[] v3 = new Vector3[3];
         v3 = m.getMeshData().getPrimitive(selection, 0, v3);
         
         Vector3 min = new Vector3((float)Math.min((float) Math.min(v3[0].getXf(), v3[1].getXf()), v3[2].getXf()),
             (float)Math.min((float)Math.min(v3[0].getYf(), v3[1].getYf()), v3[2].getYf()),
             (float)Math.min((float)Math.min(v3[0].getZf(), v3[1].getZf()), v3[2].getZf()));
       
      Vector3 max = new Vector3((float) Math.max((float)Math.max(v3[0].getXf(), v3[1].getXf()), v3[2].getXf()),
         (float)Math.max((float)Math.max(v3[0].getYf(), v3[1].getYf()), v3[2].getYf()),
         (float)Math.max((float)Math.max(v3[0].getZf(), v3[1].getZf()), v3[2].getZf()));
       

       Vector3 v2 = new Vector3();
       v2 = max.add(min, v2);
       v2.divideLocal(2);
       v2.normalizeLocal();
       
       Vector3[] send = {min, v2, max};
       
       return send;
       
   }
Offline gouessej

JGO Ninja


Medals: 33
Projects: 1


TUER


« Reply #6 - Posted 2010-12-17 11:09:07 »

If you need a faster solution to perform picking, use geometry shaders:
http://www.opengl.org/discussion_boards/ubbthreads.php?ubb=showflat&Number=268766

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

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars and Titan!

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

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (77 views)
2013-05-17 21:29:12

alaslipknot (89 views)
2013-05-16 21:24:48

gouessej (119 views)
2013-05-16 00:53:38

gouessej (113 views)
2013-05-16 00:17:58

theagentd (126 views)
2013-05-15 15:01:13

theagentd (113 views)
2013-05-15 15:00:54

StreetDoggy (156 views)
2013-05-14 15:56:26

kutucuk (179 views)
2013-05-12 17:10:36

kutucuk (179 views)
2013-05-12 15:36:09

UnluckyDevil (186 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
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!
Page created in 0.097 seconds with 21 queries.