Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / Efficient camera vector method?

Author
Message
Broken_Code
15
Years of Service
User Offline
Joined: 20th Aug 2010
Location: Bremen, Germany
Posted: 14th Nov 2010 05:41
Hello!

I've started work on a new FPS project using Dark Physics and Dark AI and will be using Dark Physics' ray cast feature for handling the bullets (amongst other things).

My first question is: what is the most efficient way to get the vector of the direction of the camera (let's assume that I'm looking for a unit vector for the moment)?

The method I have used before is: save the camera's position, move the camera forward 1 unit then save the new position then move the camera back again, then use the two positions to make the unit vector ( (P2-P1)/Vector_Length ).
However, I have recently stumbled upon using a hidden limb attached to the player object to give the 2nd position ( (P_Limb - P_Cam)/Vector_Length ), which looks more efficient to me, but I'd like a second opinion on that!

My second question is: If I attach a limb to the player's object, will the limb have collision when I use Dark Physics to wrap the object?

Thanks,
Broken_Code
Sven B
21
Years of Service
User Offline
Joined: 5th Jan 2005
Location: Belgium
Posted: 14th Nov 2010 12:01 Edited at: 14th Nov 2010 12:02
Alternative methods would be:


You can also get the internal data using a dll call.


Sven B

Broken_Code
15
Years of Service
User Offline
Joined: 20th Aug 2010
Location: Bremen, Germany
Posted: 14th Nov 2010 15:34
Thanks for the quick reply.
I hadn't thought of using pick screen for that! Great idea, but is it quicker than using a get limb position call?

I also didn't know about the dll, as that method appears to be used internally by DBP (am I right?) I suspect that to be the quickest method, if someone could confirm it for me?

Are there any more useful dlls that can be used like this, and is there a list of the dlls commands anywhere?

Great example code by the way, nice and clear.
Thanks,
Broken_Code
Benjamin
23
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 14th Nov 2010 15:44 Edited at: 14th Nov 2010 15:59
If you want a direction vector you can obtain it like this, although I have no idea if the equations are right since I just ripped them from a code snippet somewhere else:



No need to use a dummy object or a slow command like pick object.

[edit] See Sven B's second example.
Sven B
21
Years of Service
User Offline
Joined: 5th Jan 2005
Location: Belgium
Posted: 14th Nov 2010 15:56 Edited at: 14th Nov 2010 16:00
Hi Broken_Code,

I'm pretty sure the DLL call is the fastest one. The internal camera data is always up to date, whether you use the dll call or not. So you don't lose any speed there.

You could slightly optimize the code by downloading IanM's MatrixUtils, and using

x# = peek float(ptr + 64 + 8)
y# = peek float(ptr + 64 + 24)
z# = peek float(ptr + 64 + 40)

instead of copying everything to a memblock.



This is one of those moments when I usually say: "Thanks, IanM!"

[edit]
You can optimize this even further, by only calling the dll once:



Probably the fastest one I can think of right now.

The second version might give some problems when the pointer to the internal camera data changes (which might be the case when the Direct3D device is lost, I don't know how DBP handles this).
Also, you might want to make sure ptr <> 0, in case the camera doesn't exist.

Cheers!
Sven B

Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 14th Nov 2010 16:02
Quote: "although I have no idea if the equations are right since I just ripped them from a code snippet somewhere else"


Works perfectly. Are the trig functions the fastest way to do it? How do they compare with the "move camera" method?
Benjamin
23
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 14th Nov 2010 16:21
Quote: "Works perfectly. Are the trig functions the fastest way to do it? How do they compare with the "move camera" method?"


I don't know, I'd just rather do it the proper way rather than moving the camera about.
Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 14th Nov 2010 16:41
Quote: "I'd just rather do it the proper way rather than moving the camera about."


I agree - but is all that trig math the "proper way"? I feel sure the necessary vector ought to be available somewhere in the system already and doesn't need to be re-calculated.
Sven B
21
Years of Service
User Offline
Joined: 5th Jan 2005
Location: Belgium
Posted: 14th Nov 2010 16:51
Quote: "I agree - but is all that trig math the "proper way"? I feel sure the necessary vector ought to be available somewhere in the system already and doesn't need to be re-calculated."


Isn't that what I demonstrated using the DLL call?

There is one more way, that doesn't involve the use of pointers, or a DLL call for that matter, and that is using the command
view matrix mat
By getting elements (1,3), (2,3) and (3,3), you get the pointing vector for the camera.
Well, I guess this could be seen as the correct way...

The trigonometric functions are correct as long as you don't zrotate the camera, and if the rotation order is XYZ.

Cheers!
Sven B

Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 14th Nov 2010 17:31
Quote: "There is one more way, that doesn't involve the use of pointers, or a DLL call for that matter, and that is using the command
view matrix mat
By getting elements (1,3), (2,3) and (3,3), you get the pointing vector for the camera.
Well, I guess this could be seen as the correct way..."


That sounds more like it.

Quote: "The trigonometric functions are correct as long as you don't zrotate the camera"


I see what you mean.
baxslash
Valued Member
Bronze Codemaster
19
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 15th Nov 2010 15:50
Honestly I would not use DP for raycasting it's wrong quite often. Use sparky's, it works well alongside DP.

Broken_Code
15
Years of Service
User Offline
Joined: 20th Aug 2010
Location: Bremen, Germany
Posted: 15th Nov 2010 15:51
Can anyone give any example code on the view matrix method?

I have played around with it for a while and can't get it working properly, some of the axes are inverted some of the time, any help would be appreciated.

Thanks,
Broken_Code
baxslash
Valued Member
Bronze Codemaster
19
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 15th Nov 2010 16:37
Quote: "Are the trig functions the fastest way to do it? How do they compare with the "move camera" method?"

I was wondering this myself so here's a test:


Pick Vector wins here but there's not much in it!

Sven B
21
Years of Service
User Offline
Joined: 5th Jan 2005
Location: Belgium
Posted: 15th Nov 2010 18:40 Edited at: 15th Nov 2010 18:41
Quote: "Pick Vector wins here but there's not much in it!"


FPS 3 won on my machine.
But they were all 3700 cycles/s on average +- 10 cycles/s.
You can't really call any one of them a 'win'...

Quote: "Can anyone give any example code on the view matrix method?"




(The matrix4 element id starts from 0)

Cheers!
Sven B

Login to post a reply

Server time is: 2026-07-21 21:13:05
Your offset time is: 2026-07-21 21:13:05