Search Home Members Contacts
About Us
Products
Downloads
Community
Support
Pages: [1]
  Print  
Author Topic: Circling a set point  (Read 425 times)
xavram
Community Member
*
Posts: 436


« on: September 05, 2008, 03:37:10 PM »

I want a mesh to circle around a set point in space while maintaining a "correct" alignment.  Specifically, the mesh is a spaceship and I want it to orbit a planet, while maintaining the correct angle so the ship looks like its flying around the planet.  Getting the mesh to orbit a point is obviously no big deal but I can't seem to figure out the right way for it to constantly change it's angle (Mesh.RotateY) so that it's aligned properly.

Can anyone spare some quick code for this?
Logged
Fox1980
Customers
Community Member
*****
Posts: 239


« Reply #1 on: September 05, 2008, 06:46:14 PM »

Look into the TVMath library for the MoveAroudPoint function

TVMathLibrary.MoveAroundPoint( Origin As D3DVECTOR, Radius As Single, H_Ang As Single, V_Ang As Single)

use the center of the planet as origin
Logged

"Programmers don't die. They just GO SUB without RETURN."
xavram
Community Member
*
Posts: 436


« Reply #2 on: September 05, 2008, 10:35:01 PM »

Well, yes, that's a given to use that.  But that's not the issue I'm having trouble with, it's keeping the ship properly aligned (pointed in the direction of its rotation around the planet) that I'm having problems with.
Logged
jviper
Community Member
*
Posts: 1366

Discipline in training


« Reply #3 on: September 05, 2008, 10:57:51 PM »

Direction = Normalize(CurrentPosition - LastPosition)
Logged

JAbstract.....Don't just imagine, make it happen!
xavram
Community Member
*
Posts: 436


« Reply #4 on: September 06, 2008, 11:54:46 AM »

JViper, so Direction would be a vector, would i just set the Y rotation of the mesh to equal the Direction.Y value?
Logged
jviper
Community Member
*
Posts: 1366

Discipline in training


« Reply #5 on: September 06, 2008, 12:05:11 PM »

no
you would set the Direction to direction.
Logged

JAbstract.....Don't just imagine, make it happen!
xavram
Community Member
*
Posts: 436


« Reply #6 on: September 06, 2008, 07:21:03 PM »

Wow, guess I'm being dense or something...can you elaborate?

Right now, all I'm using to rotate the mesh around the planet (its a satellite, actually) is...

TestDropShip.Mesh.SetPosition(Satellites(TestDropShip.OnSatellite).Mesh.GetPosition.x + Math.Cos(TV.TickCount * 0.001) * 75, Satellites(TestDropShip.OnSatellite).Mesh.GetPosition.y, Satellites(TestDropShip.OnSatellite).Mesh.GetPosition.z + Math.Sin(TV.TickCount * 0.001) * 75)

This rotate the mesh just fine around the satellite but doesn't do anything with the rotation of the mesh, so of course, the ship flies sideways and backwards, depending on what part of the orbit its on.

How would I use your "Direction" stuff to properly align the ship with the orbit path?
Logged
xavram
Community Member
*
Posts: 436


« Reply #7 on: September 06, 2008, 07:28:28 PM »

Changed the rotation code to...

                        Dim TempPos As TV_3DVECTOR = TVMath.MoveAroundPoint(Satellites(TestDropShip.OnSatellite).Mesh.GetPosition, 110, TV.TickCount * 0.075, 0)
                        TestDropShip.Mesh.SetPosition(TempPos.x, Satellites(TestDropShip.OnSatellite).Mesh.GetPosition.y, TempPos.z)


Seems a bit cleaner than what I had using Math.Cos and Math.Sin but still, the plane isn't facing the right direction obviously.
Logged
jviper
Community Member
*
Posts: 1366

Discipline in training


« Reply #8 on: September 06, 2008, 09:46:39 PM »

ok....here's what I meant:
the meshes have a getdirection/setdirection

so what you do is keep track of ther previous position of the mesh
so the Direction = Normalize(mesh.GetPosition - PreviousPosition)

then mesh.setdirection(Direction).

EDIT:
I've just found out that apperantly SetDirection and GetDIrection have been removed from TVMesh Sad
So.....here's a hack:

Code:
Function GetDirection(Msh as TVMesh) as TV_3DVECTOR
     dim Ret as TV_3DVECTOR
     dim TmpMatx as TV_3DMATRIX

     TmpMatx = Msh.GetRotationMatrix()
     Ret = new TV_3DMatrix(TmpMatx.m31,TmpMatx.m32,TmpMatx.m33)
     Return Ret
End Function

Sub SetDirection(Msh as TVMesh,Dir as TV_3DVECTOR)
     Msh.LookAtPoint(Msh.GetPosition()+Dir)
End Sub
« Last Edit: September 06, 2008, 10:03:33 PM by jviper » Logged

JAbstract.....Don't just imagine, make it happen!
xavram
Community Member
*
Posts: 436


« Reply #9 on: September 06, 2008, 10:12:37 PM »

Hmmm...

dim Ret as TV_3DVECTOR
     dim TmpMatx as TV_3DMATRIX

     TmpMatx = Msh.GetRotationMatrix()
     Ret = new TV_3DMatrix(TmpMatx.m31,TmpMatx.m32,TmpMatx.m33)
     Return Ret

You're "Ret" variable is declared as a 3DVector but it looks like you're trying to set it to a 3DMatrix...and then returning it as a 3DVector?  Confused still...
Logged
jviper
Community Member
*
Posts: 1366

Discipline in training


« Reply #10 on: September 06, 2008, 10:15:49 PM »

that's a typo

should be Ret = new TV_3DVECTOR(TmpMatx.m31,TmpMatx.m32,TmpMatx.m33)
Logged

JAbstract.....Don't just imagine, make it happen!
xavram
Community Member
*
Posts: 436


« Reply #11 on: September 06, 2008, 10:39:12 PM »

Nope, didn't change the angle of the ship at all.  Here's how I put your code in, see anything wrong?

  Dim PreviousPos As TV_3DVECTOR = TestDropShip.Mesh.GetPosition
  Dim TempPos As TV_3DVECTOR = TVMath.MoveAroundPoint(Satellites   
      (TestDropShip.OnSatellite).Mesh.GetPosition, 110, TV.TickCount * 0.075, 0)
       TestDropShip.Mesh.SetPosition(TempPos.x, Satellites
      (TestDropShip.OnSatellite).Mesh.GetPosition.y, TempPos.z)
  'JViper's Get Direction Code
  Dim Ret As TV_3DVECTOR
  Dim TmpMatx As TV_3DMATRIX
  TmpMatx = TestDropShip.Mesh.GetRotationMatrix()
  Ret = New TV_3DVECTOR(TmpMatx.m31, TmpMatx.m32, TmpMatx.m33)
  'JViper's Set Direciton Code
  TestDropShip.Mesh.LookAtPoint(TestDropShip.Mesh.GetPosition + Ret)
Logged
jviper
Community Member
*
Posts: 1366

Discipline in training


« Reply #12 on: September 06, 2008, 10:54:32 PM »

yep. Here's the problem:
Essentually what you just did is set the mesh's direction to it's direction (so it did not change).

You want to change the direction. So the only code needed to change the direction of the mesh is as follows:

TestDropShip.Mesh.LookAtPoint(TestDropShip.Mesh.GetPosition + NewDirection).

Now, what you need to do is, in your loop keep track of the PreviousPosition of the Mesh.
NewDirection will then become TestDropShip.Mesh.GetPosition-PreviousPosition.
Logged

JAbstract.....Don't just imagine, make it happen!
xavram
Community Member
*
Posts: 436


« Reply #13 on: September 06, 2008, 11:33:32 PM »

Gah, so close!  The plane is orbiting the satellite but with a weird flickering effect, as if it's moving back and forth in the orbit.  I've looked for any other place in the code where I might have been setting the rotation or position of the plane but I can't find anything.  Here's what the code looks like now.

                        Dim PreviousPos As TV_3DVECTOR = TestDropShip.Mesh.GetPosition
                        Dim TempPos As TV_3DVECTOR = TVMath.MoveAroundPoint(Satellites(TestDropShip.OnSatellite).Mesh.GetPosition, 110, TV.TickCount * 0.075, 0)
                        TestDropShip.Mesh.SetPosition(TempPos.x, Satellites(TestDropShip.OnSatellite).Mesh.GetPosition.y, TempPos.z)

                        Dim NewDirection As TV_3DVECTOR = TestDropShip.Mesh.GetPosition - PreviousPos
                        TestDropShip.Mesh.LookAtPoint(TestDropShip.Mesh.GetPosition + NewDirection)
Logged
jviper
Community Member
*
Posts: 1366

Discipline in training


« Reply #14 on: September 07, 2008, 12:00:34 AM »

hmm, perhaps NEwDirection needs to be normalized:

Dim NewDirection As TV_3DVECTOR = Normalize(TestDropShip.Mesh.GetPosition - PreviousPos)
Logged

JAbstract.....Don't just imagine, make it happen!
xavram
Community Member
*
Posts: 436


« Reply #15 on: September 07, 2008, 09:30:52 PM »

Nope...it's very strange, it's like the ship disappears and reappears as it orbits the space station....I will try to do some logging of its position, so that I can see if it actually moving "somewhere else" like every other frame or something...odd
Logged
jviper
Community Member
*
Posts: 1366

Discipline in training


« Reply #16 on: September 07, 2008, 10:13:26 PM »

hmmmm
well I was thinking the problem is Direction somtimes becomes (0,0,0), which would make  your ship disappear. That's why I suggested normalizing NewDirection so that the lenght of it would always be 1. But if your frame-rate is ver high, the changes between frames would be small, normalizing will not do anything if it turns out the PReviousPosition is the same as CurrentPosition. Perhaps what you do is so a check on the length of NewDirection after normalizing. If the normalized vector is 0 that means there was no position change during that frame (which can happen when you have a high frame-rate). So if this is the case simply don't set the direction to the NewDirection is NewDirection is 0.
Logged

JAbstract.....Don't just imagine, make it happen!
xavram
Community Member
*
Posts: 436


« Reply #17 on: September 08, 2008, 11:16:54 AM »

How do I check the length of NewDirection?  I didn't know that a vector had a length.
Logged
xavram
Community Member
*
Posts: 436


« Reply #18 on: September 08, 2008, 11:38:29 AM »

Never mind, found it!!!  And it worked!

Dim NewDirection As TV_3DVECTOR = TVMath.VNormalize(TestDropShip.Mesh.GetPosition - PreviousPos)
                        If TVMath.VLength(NewDirection) > 0 Then
                            TestDropShip.Mesh.LookAtPoint(TestDropShip.Mesh.GetPosition + NewDirection)
                        End If

And suddenly, the ship rotates around the satellite just perfectly!!!  Thanks a bunch for your help (and patience) on this JViper!
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by SMF 1.1.3 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks