Click here to Skip to main content
15,884,472 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
See more:
Ok, so basically I have this code in javascript it's math but I can not seem to get the same result in vb.net iv'e tried lots of ways but can't get it to work I was hoping to get some help here's the code:
JavaScript
function doTheMaths() {
                var o = data[$("#origin").find(":selected").text()];
                var d = data[$("#destination").find(":selected").text()];
                var p = data[o.parent];
               
                // phase angle:
                var t_h = Math.PI * Math.sqrt(Math.pow(o.alt+d.alt, 3)/(8*p.mu));
                var phase = (180 - Math.sqrt(p.mu/d.alt) * (t_h/d.alt) * (180/Math.PI)) % 360;
                $("#phase").val("" + Math.round(phase*100)/100 + "°");
               
                // velocity:
                var exitAlt = o.alt + o.soi; // approximation for exiting on the "outside"
                var v2 = Math.sqrt(p.mu/exitAlt) * (Math.sqrt((2*d.alt)/(exitAlt+d.alt)) - 1);
                var r = o.radius + parseInt($("#orbit").val());
                var v = Math.sqrt( (r* (o.soi*v2*v2 - 2*o.mu) + 2*o.soi*o.mu) / (r*o.soi) );
                $("#velocity").val("" + Math.round(v*100000)/100 + " m/s");
				
				// delta-v:
				var v_o = Math.sqrt(o.mu/r);
				var delta_v = v - v_o;
				$("#deltav").val("" + Math.round(delta_v*100000)/100 + " m/s");
               
                // ejection angle:
                var eta = v*v/2 - o.mu/r;
                var h = r * v;
                var e = Math.sqrt(1+((2*eta*h*h)/(o.mu*o.mu)));
                var eject = (180 - (Math.acos(1/e) * (180/Math.PI))) % 360;
               
                if (e < 1) {
                        // maltesh's solution for elliptical transfers
                        var a = -o.mu/(2*eta);
                        var l = a*(1-e*e);
                        var nu = Math.acos((l-o.soi)/(e*o.soi));
                        var phi = Math.atan2((e*Math.sin(nu)), (1+e*Math.cos(nu)));
                        //eject = (270 - (phi*180/Math.PI)) % 360;
                       
                        // Kosmo-nots fix to maltesh's solution
                        eject = (90 - (phi*180/Math.PI) + (nu*180/Math.PI)) % 360;
                }
               
                $("#ejection").val("" + Math.round(eject*100)/100 + "°");             
               
                // warning for different inclination
                if (o.inclination != d.inclination)
                        $("#inclination-warning").show();      
                       
                if (o.speculated || d.speculated)
                        $("#speculation-warning").show();      
 
                draw(o,d,p,Math.round(phase*100)/100,Math.round(eject*100)/100);
        }


I'm not sure if you need anything else but thanks for your time.
EDIT
Ok, for the phase angle I tried:
VB
Dim t_h = Math.PI * Math.Sqrt(Math.Pow(originAlt + destAlt, 3) / (8 * parentMu))
Dim phase = (180 - Math.Sqrt(parentMu / destAlt) * (t_h / destAlt) * (180 / Math.PI))
phaseAngle.Text = ("" & Math.Round(phase * 100) / 100 & "°")

Which is this part of the javascript:
JavaScript
var t_h = Math.PI * Math.sqrt(Math.pow(o.alt+d.alt, 3)/(8*p.mu));
var phase = (180 - Math.sqrt(p.mu/d.alt) * (t_h/d.alt) * (180/Math.PI)) % 360;
$("#phase").val("" + Math.round(phase*100)/100 + "°");

I should get 44.36° but I get -478.82° instead.
Btw phaseAngle is the tex box showing the result angle

I get my data from an xml file:
XML
<?xml version="1.0" encoding="UTF-8"?>
<dataroot xmlns:od="urn:schemas-microsoft-com:officedata" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:noNamespaceSchemaLocation="PlanetData.xsd" generated="2014-11-02T16:24:22">
  <Planets>
    <ID>1</ID>
    <planet>Kerbol</planet>
    <color>yellow</color>
    <mu>1167922000</mu>
    <radius>65400</radius>
  </Planets>
  <Planets>
    <ID>2</ID>
    <planet>Moho</planet>
    <parent>Kerbol</parent>
    <color>brown</color>
    <inclination>7</inclination>
    <mu>245</mu>
    <soi>11206.449</soi>
    <radius>250</radius>
    <alt>5263138.3</alt>
  </Planets>
  <Planets>
    <ID>3</ID>
    <planet>Eve</planet>
    <parent>Kerbol</parent>
    <color>purple</color>
    <inclination>2.1</inclination>
    <mu>8172</mu>
    <soi>85109.364</soi>
    <radius>700</radius>
    <alt>9832684.544</alt>
  </Planets>
  <Planets>
    <ID>4</ID>
    <planet>Gilly</planet>
    <parent>Eve</parent>
    <color>brown</color>
    <inclination>12</inclination>
    <mu>0.008289450</mu>
    <soi>126.123</soi>
    <radius>13</radius>
    <alt>31500</alt>
  </Planets>
  <Planets>
    <ID>5</ID>
    <planet>Kerbin</planet>
    <parent>Kerbol</parent>
    <color>skyblue</color>
    <inclination>0</inclination>
    <mu>3531.6</mu>
    <soi>84159.2865</soi>
    <radius>600</radius>
    <alt>13599840.256</alt>
  </Planets>
  <Planets>
    <ID>6</ID>
    <planet>Mun</planet>
    <parent>Kerbin</parent>
    <color>gray</color>
    <inclination>0</inclination>
    <mu>65.138</mu>
    <soi>2430</soi>
    <radius>200</radius>
    <alt>12000</alt>
  </Planets>
  <Planets>
    <ID>7</ID>
    <planet>Minmus</planet>
    <parent>Kerbin</parent>
    <color>#97d0a9</color>
    <inclination>6</inclination>
    <mu>1.7658</mu>
    <soi>2247.428</soi>
    <radius>60</radius>
    <alt>47000</alt>
  </Planets>
  <Planets>
    <ID>8</ID>
    <planet>Duna</planet>
    <parent>Kerbol</parent>
    <color>orange</color>
    <inclination>1.85</inclination>
    <mu>301.363</mu>
    <soi>47921.949</soi>
    <radius>320</radius>
    <alt>20726155.264</alt>
  </Planets>
  <Planets>
    <ID>9</ID>
    <planet>Ike</planet>
    <parent>Duna</parent>
    <color>silver</color>
    <inclination>0.2</inclination>
    <mu>18.56837</mu>
    <soi>1049.599</soi>
    <radius>130</radius>
    <alt>3200</alt>
  </Planets>
  <Planets>
    <ID>10</ID>
    <planet>Dres</planet>
    <parent>Kerbol</parent>
    <color>silver</color>
    <inclination>5</inclination>
    <mu>21.4845</mu>
    <soi>32832.84</soi>
    <radius>138</radius>
    <alt>40839348.203</alt>
  </Planets>
  <Planets>
    <ID>11</ID>
    <planet>Jool</planet>
    <parent>Kerbol</parent>
    <color>green</color>
    <inclination>1.3</inclination>
    <mu>282528.0042</mu>
    <soi>2455985.185</soi>
    <radius>6000</radius>
    <alt>68773560.320</alt>
  </Planets>
  <Planets>
    <ID>12</ID>
    <planet>Laythe</planet>
    <parent>Jool</parent>
    <color>darkblue</color>
    <inclination>0</inclination>
    <mu>1962</mu>
    <soi>3723.646</soi>
    <radius>500</radius>
    <alt>27184</alt>
  </Planets>
  <Planets>
    <ID>13</ID>
    <planet>Vall</planet>
    <parent>Jool</parent>
    <color>skyblue</color>
    <inclination>0</inclination>
    <mu>207.4815</mu>
    <soi>2406.401</soi>
    <radius>300</radius>
    <alt>43152</alt>
  </Planets>
  <Planets>
    <ID>14</ID>
    <planet>Tylo</planet>
    <parent>Jool</parent>
    <color>beige</color>
    <inclination>0.025</inclination>
    <mu>2825.28</mu>
    <soi>10856.51837</soi>
    <radius>600</radius>
    <alt>68500</alt>
  </Planets>
  <Planets>
    <ID>15</ID>
    <planet>Bop</planet>
    <parent>Jool</parent>
    <color>brown</color>
    <inclination>15</inclination>
    <mu>2.486835</mu>
    <soi>993.0028</soi>
    <radius>65</radius>
    <alt>104500</alt>
  </Planets>
  <Planets>
    <ID>16</ID>
    <planet>Pol</planet>
    <parent>Jool</parent>
    <color>orange</color>
    <inclination>1.304</inclination>
    <mu>0.227</mu>
    <soi>2455985.185</soi>
    <radius>44</radius>
    <alt>129890</alt>
  </Planets>
  <Planets>
    <ID>17</ID>
    <planet>Eeloo</planet>
    <parent>Kerbol</parent>
    <color>#ddd</color>
    <inclination>6.15</inclination>
    <mu>74.4108157</mu>
    <soi>119082.94</soi>
    <radius>210</radius>
    <alt>90118858.179</alt>
  </Planets>
</dataroot>


Which I read and declare using this:
VB
'Loads PlanetData.xml which is the planetary infomation
       Dim planetData = XElement.Load("PlanetData.xml")

       'Declares origin data
       Dim originMu As Decimal = planetData.<Planets>.<mu>(originID)
       Dim originSoi As Decimal = planetData.<Planets>.<soi>(originID)
       Dim originRadius As Decimal = planetData.<Planets>.<radius>(originID)
       Dim originAlt As Decimal = planetData.<Planets>.<alt>(originID)

       'Declares destination data
       Dim destMu As Decimal = planetData.<Planets>.<mu>(destID)
       Dim destSoi As Decimal = planetData.<Planets>.<soi>(destID)
       Dim destRadius As Decimal = planetData.<Planets>.<radius>(destID)
       Dim destAlt As Decimal = planetData.<Planets>.<alt>(destID)

       'Declares Kerbol data
       Dim parentMu As Decimal = planetData.<Planets>.<mu>(0)
       Dim parentRadius As Decimal = planetData.<Planets>.<radius>(0)

Origin is Kerbin
Dest is Duna
Parent is Kerbol
Posted
Updated 6-Nov-14 21:32pm
v3
Comments
OriginalGriff 6-Nov-14 4:21am    
What have you tried?
Where are you stuck?
In what way are you "not seem to get the same result in vb.net"?
Show us the code you tried, and show us what doesn't work.

Use the "Improve question" widget to edit your question and provide better information.
George Jonsson 6-Nov-14 5:37am    
You need to present the vb code and also tell us what the expected result is and the actual outcome you get.
Sinisa Hajnal 6-Nov-14 6:00am    
What is the error? And where?
Thava Rajan 7-Nov-14 2:44am    
please provide the values for the variables in your problem
Member 11211249 7-Nov-14 3:01am    
I don't know what variables you are asking me to provide i'm sure iv'e provided everything. Sorry i'm new to this and confused

1 solution

The C# code below gives the correct answer.
(Not a VB programmer, I'm afraid)
C#
double originMu = 3531.6; //planetData.<planets>.<mu>(originID);
double originSoi = 84159.2865; // planetData.<planets>.<soi>(originID);
double originRadius  = 600; // planetData.<planets>.<radius>(originID);
double originAlt   =  13599840.256; //  planetData.<planets>.<alt>(originID);

// Declares destination data
double destMu = 301.363; // planetData.<planets>.<mu>(destID);
double destSoi = 47921.949; // planetData.<planets>.<soi>(destID);
double destRadius = 320; //planetData.<planets>.<radius>(destID);
double destAlt = 20726155.264; // planetData.<planets>.<alt>(destID);

// Declares Kerbol data
double parentMu  = 1167922000; //planetData.<planets>.<mu>(0);
double parentRadius = 65400; //planetData.<planets>.<radius>(0);

var t_h = Math.PI * Math.Sqrt(Math.Pow(originAlt + destAlt, 3) / (8 * parentMu));
var phase = (180 - Math.Sqrt(parentMu / destAlt) * (t_h / destAlt) * (180 / Math.PI)) % 360;
string text = String.Format("{0} °", Math.Round(phase * 100) / 100);


% 360 means modulo operation of the result.
It means that the result will always be less than 360 degrees.
No reason to have a phase with a bigger value.
Modulo operation[^]

Make sure you get the right data into your variables from the XML file.
(I used double instead of decimal because I was lazy, but maybe you should try it)
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900