Improve company productivity with a Business Account.Sign Up

x
?
Solved

Moving on a sphere facing center

Posted on 2001-08-05
3
Medium Priority
?
368 Views
Last Modified: 2013-12-26
Hi,

Is there an easy way (a supplied function or so) in DX8 to set the view matrix so that the viewer will be able to move around (as on a sphere) an object in all directions always facing the centre of the sphere?

What I do now is this (this allows me to move horizontally around the object in a circle and straight up and down always facing the object):

long lElapsedTime = timeGetTime() - m_dwTime;
m_dwTime = timeGetTime();

fHorzPos += fHorzSpeed * lElapsedTime;
vFromPt.x = sinf(fHorzPos) * 60.0f;
vFromPt.z = cosf(fHorzPos) * 60.0f;

fVertPos += fVertSpeed * lElapsedTime;
vFromPt.y = fVertPos * 60.0f;

D3DXMATRIX matView;
D3DXMatrixLookAtLH(&matView, &vFromPt, &vLookatPt, &vUpVec);
m_pd3dDevice->SetTransform(D3DTS_VIEW, &matView);

What I want is that when I move up or down, I always have the same distance to the object.
Do I have to do the calculations myself or is there already a function for this in DX8?

Jacob
0
Comment
Question by:jacobkristensen
  • 2
3 Comments
 
LVL 2

Expert Comment

by:cybermike3d
ID: 6358167
There are two methods of specifying your position in space relative to another. The one is to use an x,y,z offset to a reference point, the other is to use the x angular, y angular and range offset. I use the following math to determine a camear position when panning and tilting around an object.

    eye.X = posx(sel) + Sin(((hang) / 57.2958 - heading(sel) / 57.29)) * zoom * Cos(vang / 57.2958)
    eye.Y = posy(sel) + Sin(vang / 57.2958 - roly(sel) / rad) * zoom
    eye.z = posz(sel) + Cos(((hang) / 57.2958 - heading(sel) / 57.29)) * zoom * -1 * Cos(vang / 57.2958)

In the above code snippet. POSX, POSY, POSZ are then x,y,z coordinates of the object in space. ZOOM is the distance to the object. VANG = vertical angular offset (the tilt) and HANG = horizontal angular offset. When I want to pan and tilt the camera around an object, I merely assign the mouse x and y co ordinates to HANG and VANG. ROLY is the pitch of the object being viewed. In other words, if in this case, the aircraft goes nose down, the camera angle in increased so as to keep a constant vertical viewing angle to the craft. Likewise, heading is the direction that the target craft is facing. If the target turns right or left, so the camera turns, maintaining a constant orientation to the target.

In order to translate an X,Y,Z coordinate into a XANG, YANG, RANGE co ordinate, use the following routine.

ixdif = eyeposx - targetposx
iydif = eyeposz - targetposz
getcord
xang = itnang
ixdif = itargetrange
iydif = eyeposy - targetposy
getcord
yang = itnang
range = itargetrange

Sub getcord()
        itargetrange = Sqr(ixdif ^ 2 + iydif ^ 2)
        If iydif <> 0 Then itgan = Atn(ixdif / iydif) * 57.2958
        If iydif = 0 And ixdif = 0 Then itgan = 0: itgang = 0
        If iydif = 0 And ixdif < 0 Then itgan = 270
        If iydif = 0 And ixdif > 0 Then itgan = 90
        itgang = itgan
        If iydif < 0 Then itgang = 180 + itgan
        If ixdif < 0 And iydif > 0 Then itgang = 360 + itgan
        itnang = ina + itgang
        If itnang > 360 Then itnang = itnang - 360
        If itnang < 0 Then itnang = itnang + 360
End Sub


Unfortunately the code is in visual basic ... but the math is correct. Let me know if u need any further assistance
Regards
Cybermike.
0
 
LVL 2

Accepted Solution

by:
cybermike3d earned 300 total points
ID: 6358201
Just an afterthought ... is your aplication for a camera or for an object moving around an object that must allways point at the targets centre like a compass. If your app. is for a camera ... take a look at the LOOKAT function.

    eye.X = posx(sel) + Sin(((hang) / 57.2958 - heading(sel) / 57.29)) * zoom * Cos(vang / 57.2958)
    eye.Y = posy(sel) + Sin(vang / 57.2958 - roly(sel) / rad) * zoom
    eye.z = posz(sel) + Cos(((hang) / 57.2958 - heading(sel) / 57.29)) * zoom * -1 * Cos(vang / 57.2958)
    tar.X = posx(sel)
    tar.Y = posy(sel)
    tar.z = posz(sel)
    rollz = roll(sel) * 0.2 * rollmag
'roll the camera as the rarget banks
    orr.X = (Sin((heading(sel) - 90) / 57.29)) * rollz / 30
    orr.Y = Cos(rollz / 57.29)
    orr.z = (Cos((heading(sel) - 90) / 57.29)) * rollz / 30
    D3DXMatrixLookAtLH matView, eye, tar, orr
    g_D3DDevice.SetTransform D3DTS_VIEW, matView
    D3DXMatrixPerspectiveFovLH matProj, fov, 1, 0.001, 100000000
    g_D3DDevice.SetTransform D3DTS_PROJECTION, matProj

If it is an object ... then use the top algorithm.  then apply the HANG, VANG variables to the object that is moving on the YAW and PITCH of the SETYAWPITCHROLL angular translation of that object.
0
 
LVL 1

Author Comment

by:jacobkristensen
ID: 6371360
Hi CyberMike3D

Thanks for your answers.

What I was doing, was moving the camera around the object and as you can see I was also using the D3DXMatrixLookAtLH function.

I ended up making a lot of changes to my game, so now I'm actually rotating the objects instead of moving the camera.

I'm happy to give you the points though, as you (of cource) gave me the answer for what I asked in the first place...
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Artificial Intelligence comes in many forms, and for game developers, Path-Finding is an important ability for making an NPC (Non-Playable Character) maneuver through terrain.  A* is a particularly easy way to approach it.  I’ll start with the algor…
Recently, in one of the tech-blogs I usually read, I saw a post about the best-selling video games through history. The first place in the list is for the classic, extremely addictive Tetris. Well, a long time ago, in a galaxy far far away, I was…
If you are looking for an automated solution for backup single or multiple Office 365 user mailboxes to Outlook data file, then you can use Kernel Office 365 Backup & Restore tool. Go through the video to check out the steps to backup single or mult…
Watch the video to know the simple way to remove or recover or reset lost or forgotten passwords of Outlook PST file. With Kernel Outlook Password Recovery tool such operation is very easy to perform. It is a freeware with limitation to use with 500…

606 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question