Jump to content
  • Advertisement
Sign in to follow this  
howie_007

Getting position in world view from first person camera

This topic is 2657 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

As the player moves around the map (or should I say, as the map moves around the player), I want to get the point in world space of this position.

Since the movement is stored in my matrix class, this is easy to retrieve if I invert the values. The problem is if I move and rotate at the same time.

If I move around with only w,s,a,d, then it's not a problem. If I just move forward and steer with the mouse, then my position and collision no longer match.

Normally I translate my collision mesh with the same matrix. This obviously works just fine but if I can figure out how to get this point, then I don't need to translate the collision mesh.

Any help would be greatly appreciated.

Share this post


Link to post
Share on other sites
Advertisement
Post some code.

It sounds to me like you're doing a single transform instead of an integration of the transform curve between the origin and the current offset/rotation; but without seeing what your code is actually doing, that's just a shot in the dark.

Share this post


Link to post
Share on other sites
Unfortunately posting code will only confuse the issue because my engine is a collection of many objects that work together. It would be simpler to communicate the problem with some pseudo code.

Say you are using a matrix class to store the incremental movement of the player.

matrix.Translate( playerTrans.x, playerTrans.y, playerTrans.z );
matrix.Rotate( playerRot.x, playerRot.y, playerRot.z );

This matrix is given to directX to translate the map to the players position of 0, 0, 0. The player is always at 0, 0, 0. The same thing is done to the collision mesh. The collision mesh is translated so that collision can be tested for the player position of 0, 0, 0. All is good.

What I'm trying to do is an optimization. There's no need to translate the collision mesh if I can somehow get the point the player would be at if they were moving and not the map around them.

It turns out, this is easy, just translate a point of 0, 0, 0 from the matrix and invert it.

CPoint playerPos; // initialized to 0, 0, 0
CPoint playerOffset;

matrix.Transform( playerOffset, playerPos );
playerOffset.Invert();

playerOffset can now be used as a point to collide with the collision matrix. Translating the collision mesh is no longer needed.

As it stands, this works... as long as I don't use any rotations. So if I move around with translation only, it all works.

What's happening is, once rotation is added, the map is rotated at these different points along the way. Inverting the point from the matrix no longer works.

I'm not sure if what I'm trying to do can be done or if there is a completely different way this needs to be handled.

Share this post


Link to post
Share on other sites

Just curious - why move the map around the player...and not move the player position around the map ?


Agree. I think that moving the player and camera is much more optimized. Having to loop through a big list of objects every time the player move feels unnecessary.

Share this post


Link to post
Share on other sites

[quote name='GregMichael' timestamp='1300038421' post='4785267']
Just curious - why move the map around the player...and not move the player position around the map ?

Agree. I think that moving the player and camera is much more optimized. Having to loop through a big list of objects every time the player move feels unnecessary.
[/quote]

It's all the same thing. You guys might be using some high level functions and are not fully aware of what's really happening under the hood. In a fps game, no matter where you go, you are standing at position 0, 0, 0. This is what the map has been translated to.

Share this post


Link to post
Share on other sites
Believe me...after 25+ years of working in the Games Industry I know what is and isn't working under the hood.

You are completely wrong on this one.

Move the player / camera and NOT the map....

Share this post


Link to post
Share on other sites

It's all the same thing. You guys might be using some high level functions and are not fully aware of what's really happening under the hood. In a fps game, no matter where you go, you are standing at position 0, 0, 0. This is what the map has been translated to.

Typically a game or similar simulation will work in a few different spaces over the course of a single frame. For example, the simulation will likely do work in model, world, and maybe view space, and the graphics API or hardware will do work in view space, clip space, screen space, and so on.

It's true that at some point in the pipeline, the viewer will end up at the origin and looking down the + or - z axis (typically), but don't be misled into thinking that you need to think of the rest of the simulation this way. For most games, from a higher-level view, the kind of reasoning you're doing here will take place in world space (and sometimes model space, depending). In any case, rather than thinking that you have to express the problem with the player at the origin and with identity rotation, just think in terms of what space will make things easy to code and reasonably efficient (and that may very well be world space rather than model/view space).

Generally speaking, you shouldn't have to jump through any hoops to 'get' the position of an object in your simulation. The analogy I think of when this question comes up is: "Ok, I just put the book on the table. Now all I have to do is figure out where I put it." In other words, there's no need to figure out where the player (or any other object) is, because you're the one who put it there in the first place.

Typically, the transform for a game object will be represented using a position vector and an orientation in one form or another (Euler angles, quaternion, etc.). To move the object around, you just modify the position vector; furthermore, at any given time, the position vector tells you where the object is. If at any time you need the transform in matrix form (e.g. for submission to an API), you can easily build a matrix from the position and orientation. And if you need an inverted matrix (e.g. to create a view transform), you can simply invert that matrix, either using a general matrix inversion function, or a special-case function for greater efficiency (although any difference in performance will be unlikely to matter if you're only performing a few inversions per frame).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!