Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (536)
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 1504 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
« 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!
Legends of Yore - The Casual Retro Roguelike
Offline gouessej
« 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
« 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.

 

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

The first screenshot will be displayed as a thumbnail.

CogWheelz (14 views)
2014-07-30 21:08:39

Riven (21 views)
2014-07-29 18:09:19

Riven (14 views)
2014-07-29 18:08:52

Dwinin (12 views)
2014-07-29 10:59:34

E.R. Fleming (32 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

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

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

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

Riven (30 views)
2014-07-23 20:56:16
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!