Jump to content

Orbital Inclination calculation


Schoppy0384
 Share

Recommended Posts

Hi,

for my Project, I must calculate the Sphere Position with Sin and Cos on the x/y/z axis.

 

There is a Error in the Orbital Inclination calculation. My Math in Sin and Cos are not so good ;-)

 

Ok, i can use a Pivot and the move and turn Commands, but I need the calculation for later work without

the Models.

 

Here are my Code:

 

#include "App.h"
using namespace Leadwerks;
App::App() : window(NULL), context(NULL), world(NULL), camera(NULL) {}
App::~App() { delete world; delete window; }
Vec3 camerarotation;
#if defined (PLATFORM_WINDOWS) || defined (PLATFORM_MACOS)
bool freelookmode=true;
#else
bool freelookmode=false;
#endif

Model* sunSphere = NULL;
Model* planetSphere = NULL;
float planetDistancetoSun = 5.0;
float pitch = -45.0;
float yaw = 0.0;
float soll_pitch = 0.0;
bool Orbital_inclination;

bool App::Start()
{
Orbital_inclination = false;
//Create a window
window = Leadwerks::Window::Create("OrbitInc");

//Create a context
context = Context::Create(window);

//Create a world
world = World::Create();

//Create a camera
camera = Camera::Create();
camera->Move(0,2,-10);

//Hide the mouse cursor
window->HideMouse();

//std::string mapname = System::GetProperty("map","Maps/start.map");
//Map::Load(mapname);
//Create a Light
Light* light = DirectionalLight::Create();
light->SetRotation(35, 35, 0);

//Create Sun Sphere
sunSphere = Model::Sphere();
sunSphere->SetColor(1.0, 1.0, 0.0);
sunSphere->SetScale(2.0,2.0,2.0);
//Create Planet Sphere
planetSphere = Model::Sphere();
planetSphere->SetColor(0.0, 0.0, 1.0);


//Move the mouse to the center of the screen
window->SetMousePosition(context->GetWidth()/2,context->GetHeight()/2);

return true;
}
bool App::Loop()
{
//Close the window to end the program
if (window->Closed()) return false;

//Press escape to end freelook mode
if (window->KeyHit(Key::Escape))
{
if (!freelookmode) return false;
freelookmode=false;
window->ShowMouse();
}

if (freelookmode)
{
//Keyboard movement
float strafe = (window->KeyDown(Key:) - window->KeyDown(Key::A))*Leadwerks::Time::GetSpeed() * 0.05;
float move = (window->KeyDown(Key::W) - window->KeyDown(Key::S))*Leadwerks::Time::GetSpeed() * 0.05;
camera->Move(strafe,0,move);
//Get the mouse movement
float sx = context->GetWidth()/2;
float sy = context->GetHeight()/2;
Vec3 mouseposition = window->GetMousePosition();
float dx = mouseposition.x - sx;
float dy = mouseposition.y - sy;
//Adjust and set the camera rotation
camerarotation.x += dy / 10.0;
camerarotation.y += dx / 10.0;
camera->SetRotation(camerarotation);
//Move the mouse to the center of the screen
window->SetMousePosition(sx,sy);
}
if (window->KeyHit(Key:))
{
if (Orbital_inclination == false)
{
Orbital_inclination = true;
}
else
{
Orbital_inclination = false;
}
}
Leadwerks::Time::Update();

//calculate the planet Position
yaw = yaw + 0.5;
float ny = Math::Sin(soll_pitch) / Math::Cos(soll_pitch);
float nx = Math::Sin(yaw);
float nz = Math::Cos(yaw);

//calculate the Orbital_inclination
if (Orbital_inclination)
{
float ist_yaw = Math::Mod(yaw,360);
float p_weg = (ist_yaw * 100.0) / 360.0;
float winkelWeg = (2 * pitch);
if (winkelWeg < 0) { winkelWeg = -1 * winkelWeg; }
//calculation from the first 180 degrees
soll_pitch = -(winkelWeg / 2) + (p_weg *(winkelWeg * 2)) / 100;
//calculation from the second 180 degrees --- not correct !!!!!!!!!!
if (ist_yaw > 180.0)
{
soll_pitch = -(winkelWeg / 2) - (p_weg *(winkelWeg * 2)) / 100;
}
}

//normalize vector
float factor = sqrt(nx*nx + ny*ny + nz*nz);
nx = nx / Math::Abs(factor);
ny = ny / Math::Abs(factor);
nz = nz / Math::Abs(factor);

planetSphere->SetPosition(nx*planetDistancetoSun, ny*planetDistancetoSun, nz*planetDistancetoSun);


world->Update();
world->Render();

context->SetBlendMode(Blend::Alpha);
context->SetColor(1, 1, 1);
context->DrawText("Press Key O for Oribital Inclinaton", 2, 2);
if (Orbital_inclination) { context->DrawText("Oribital Inclinaton: 1" , 2, 20); }
if (!Orbital_inclination) { context->DrawText("Oribital Inclinaton: 0", 2, 20); }
context->SetBlendMode(Blend::Solid);

context->Sync(false);

return true;
}

Link to comment
Share on other sites

I see more than just a couple of things that strike me as odd about your math before we even get to the orbital inclination.

 

Please forget about the code for a moment, explain in words what you are trying to do and why you are doing it like this and lay down your math, step by step.

 

And what exactly is the "error". (Expected result vs actual result.)

Link to comment
Share on other sites

Ok, first i calculate the new Planet Positions with:

 

//calculate the planet Position

yaw = yaw + 0.5;

float ny = Math::Sin(soll_pitch) / Math::Cos(soll_pitch);

float nx = Math::Sin(yaw);

float nz = Math::Cos(yaw);

 

then normalize

//normalize vector

float factor = sqrt(nx*nx + ny*ny + nz*nz);

nx = nx / Math::Abs(factor);

ny = ny / Math::Abs(factor);

nz = nz / Math::Abs(factor);

 

and last position

 

planetSphere->SetPosition(nx*planetDistancetoSun, 0, nz*planetDistancetoSun);

 

this works for the X and Z Axis.

 

But now I would like calculate the Y Axis like 45° or another °.

Link to comment
Share on other sites

It seems to me like you are using the wrong coordinate transformation to begin with.

 

What you are trying to do is place your planet around a sun that's sitting at (0,0,0) while the data you have are the two angles and the distance, right?

 

What you have (yaw, soll_pitch, olanetDistancetoSun) are all the components you need in spherical coordinates, the correct transformation from spherical to Cartesian (x,y,z) coordinates can be found here: https://en.wikipedia.org/wiki/Spherical_coordinates#Cartesian_coordinates

 

r is your distance (so just omit that for your normalized vector, though you can actually skip that and go straight for the full vector) and theta and phi are yaw and pitch respectively, depending on which convention you use ( I recommend the physics convention)

 

What was killing your y (besides being wrong) is the tangent you used which diverges at ±90°, which would place your planet somewhere in infinity, which obviously doesn't work.

Link to comment
Share on other sites

 

What you are trying to do is place your planet around a sun that's sitting at (0,0,0) while the data you have are the two angles and the distance, right?

If working with physics any planet should be at the 0,0,0 position and sun should be moving around that while the planet is rotating.. if adding a planet ...if thats what he is planning to do.. since I assume Leadwerks is like other engines and has issues with distances and physics(the farther out in the scene..the more glitchy physics will get)..

 

Probably trivial though at the moment..

Threadripper 2920X Gen2 CPU(AMD 12-core 24 thread) | 32Gigs DDR4 RAM | MSI Nvidia GeForce RTX 2070 Stock OCed | ASRock X399 Professional Gaming Motherboard | Triple M.2 500Gig SSD's in Raid0

Windows 10 Pro | Blender | Paint.NetWorld Machine | Shader Map 4 | Substance Designer | Substance Painter | Inkscape | Universal Sound FX | ProBuilder | 3D World Studio | Spacescape | OpenSky | CubeMapGen | Ecrett Music | Godot Engine | Krita | Kumoworks | GDScript | Lua | Python | C# | Leadworks Engine | Unity Engine

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...