00001 package org.hfbk.vis.visnode;
00002
00003 import org.dronus.gl.GLSphereRenderer;
00004 import org.dronus.graph.Node;
00005 import org.lwjgl.opengl.GL11;
00006 import org.lwjgl.util.vector.Vector3f;
00007
00016 class VisBalistic extends VisNode{
00017
00018 public Vector3f target=new Vector3f(50,-20,0);
00019
00020 VisBalistic(Node node, Vector3f pos) {
00021 super(node, pos);
00022
00023 radius=Float.MAX_VALUE;
00024 }
00025
00026 float heading;
00027 float elevation;
00028
00029 float G=9.81f, v0=20, y0;
00030
00035 double wurfweite (double a){
00036 double s=Math.sin(a);
00037 double r= v0*Math.cos(a)*( v0*s + Math.sqrt(v0*v0*s*s+2*G*y0 )) / G;
00038 return r;
00039 }
00040
00048 float invWurfweite(double X){
00049 double d=1E-12f,
00050 e=1E-6f;
00051 double a=0,
00052 f;
00053 int i=0;
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064 do{
00065 if (i++>10)
00066 return Float.NaN;
00067 double df=0;
00068 f= wurfweite(a)-X;
00069 df=(wurfweite(a)-wurfweite(a-d))/d;
00070
00071
00072
00073 a=a-f/df;
00074
00075
00076 }while(Math.abs(f)>e);
00077
00078 return (float)a;
00079 }
00080
00081 double bestAngle(double y){
00082 return Math.asin(v0/Math.sqrt(2*v0*v0+2*G*y));
00083 }
00084
00085 void renderSelf() {
00086
00087
00088 GL11.glDisable(GL11.GL_TEXTURE_2D);
00089 GLSphereRenderer.renderSphere(1);
00090
00091 if (target!=null)
00092 y0=-target.y;
00093
00094
00095
00096 drawRanges();
00097
00098
00099
00100 GL11.glColor3f(1,1,1);
00101
00102 if (target==null) return;
00103
00104 Vector3f v=new Vector3f(target);
00105 v.y=0;
00106 float wurfweite=v.length();
00107 heading=(float) (Math.atan(v.z/v.x)+(v.x<0?Math.PI:0));
00108
00109
00110
00111
00112 v.normalise();
00113 elevation=invWurfweite(wurfweite);
00114
00115
00116
00117 float s=(float)Math.sin(elevation), c=(float)Math.cos(elevation);
00118 v.x*=c; v.z*=c; v.y=s;
00119
00120 v.scale(v0);
00121
00122
00123 GL11.glBegin(GL11.GL_LINE_STRIP);
00124 Vector3f p=new Vector3f();
00125
00126 while(p.y>-y0){
00127 float dt = .001f;
00128
00129 GL11.glVertex3f(p.x,p.y,p.z);
00130
00131 Vector3f dp=new Vector3f(v);
00132 dp.scale(dt);
00133
00134 Vector3f.add(p,dp,p);
00135 v.y+=-G*dt;
00136 }
00137 GL11.glEnd();
00138
00139 }
00140
00141 float drawRanges() {
00142 float r=0;
00143 GL11.glColor4f(1,1,1,.3f);
00144 for (float y=-100; y<y0; y+=3){
00145 r=(float)wurfweite(bestAngle(y));
00146 if (r!=Float.NaN){
00147 GL11.glBegin(GL11.GL_LINE_STRIP);
00148 for (float i=0; i<2*Math.PI; i+=.01f)
00149 GL11.glVertex3f((float)Math.cos(i)*r, -y, (float)Math.sin(i)*r);
00150 GL11.glEnd();
00151 }
00152 }
00153 return r;
00154 }
00155 }