0
votes

Alright, so I'm working on collision detection for a 3d game, this is what I got so far:

public void mapCol(Spatial map, Node model2){
             Mesh m = (Mesh) ((Node) map).getChild("obj_mesh0");
              int c = 0;
              m.updateWorldBound(true);
              boolean col = false;
              c = m.getMeshData().getPrimitiveCount(0);
             // System.out.println(c);
              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()));


                 Vector3 v2 = new Vector3();
                v2 = max.add(min, v2);
                v2.divideLocal(2);            

                 if(max.getXf() > model2.getTranslation().getXf() -  sp1.getRadius()&&
                    min.getXf() < model2.getTranslation().getXf() +  sp1.getRadius()  &&
                    max.getZf() > model2.getTranslation().getZf()  -  sp1.getRadius()  &&
                    min.getZf() < model2.getTranslation().getZf()  + sp1.getRadius() &&
                    max.getYf() > model2.getTranslation().getYf() + sp1.getRadius()&&
                    !col){


                    float cosine = (float) v2.dot(v2);
                      float angle = (float) Math.toDegrees(Math.acos( cosine ));
                    float pangle = (float) Math.toDegrees(Math.atan2((min.getX() + ((max.getX() - min.getX())/2)) - model2.getTranslation().getX(), (min.getZ() + ((max.getZ() - min.getZ())/2) - model2.getTranslation().getZ())));



                    if(min.getY() < max.getY()){   

                       System.out.println("pangle:" + pangle + " angle:" + angle);


                       model2.setTranslation(
                             (min.getX() + ((max.getX() - min.getX())/2)) - (Math.sin(Math.toRadians(pangle)) * (sp1.getRadius())),
                                   model2.getTranslation().getYf(),
                                   (min.getZ() + ((max.getZ() - min.getZ())/2)) -  (-Math.cos(Math.toRadians(pangle)) * (sp1.getRadius()))
                             );
                       col = true;
}
                    }
                 }     
          }

Now the part to really look at is right here:

model2.setTranslation(
                             (min.getX() + ((max.getX() - min.getX())/2)) - (Math.sin(Math.toRadians(pangle)) * (sp1.getRadius())),
                                   model2.getTranslation().getYf(),
                                   (min.getZ() + ((max.getZ() - min.getZ())/2)) -  (-Math.cos(Math.toRadians(pangle)) * (sp1.getRadius()))
                             );

Any idea why it wouldn't set model2 modle2's radius away from the wall? (making it stop at the way and able to go no further)

1
If you want someone to try to solve this, tell us 1) what that piece of code is supposed to do, 2) what it actually does, and 3) that you have tested those five lines and not just guessed. - Beta

1 Answers

0
votes
float cosine = v2.dot(v2)

is intentional ?

Because it just gives you length of v2, squared.

Probably that should be

float cosine = velocity.dot(normalVector)/(velocity.length()*normalVector.length())

, if you wanted cosine of angle between them, but I don't fully understand your code, so I don't know.