Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

dense

odd pointer question...

This topic is 6134 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

So far in my game, I''ve got a base object class that most everything is derived from. This has orientation/position/etc information in it. Now, I have my camera. I want the camera''s information to be based on whatever object in the game I choose to have it based on. Now here is where I have the problem. Lets say my setup is like this: camera *maincamera; player me; Both are derived from class object. Is it possible to point the camera''s object information to the players object information without having to point the camera at the player (which wouldn''t be possible). Or, could I have a pointer to an object in my camera class, and point that at the player''s object? I can''t see either of those options being possible. I know of one way that would work, and that would be to have a camera in every object and just point the main camera at whatever object''s camera I wanted, but that would require calculating every objects camera position every frame... Or I could possible have a pointer to an object in my camera class, but I still can''t figure out how I would point it at something that doesn''t really exist.. If anyone has any information on how this could be possible, please share. I''m using C++ and OpenGL if it makes a difference. I hope this makes sense.

Share this post


Link to post
Share on other sites
Advertisement
I''m not sure if I understood you well, but how about adding a Target to your camera? Which would force the camera to "follow" it... I don''t know if you''re trying to have a 1st person view or third person view... but both would be quite easy using a target and a position/orientation relative to the target?

Hope it helps

"Do it with style or don''t bother doing it."

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I tend to use Structs for my objects simply because making pointers is simple that way.

struct PlayerS
{
float x,y,z;
//Aditional info here.
};

struct MonsterS
{
float x,y,z;
//Additional info here
};

struct VertexS
{
float x,y,z;
};

struct CameraS
{
VertexS *ptr;
};

CameraS Camera;
PlayerS me;

Camera.ptr = (VertexS*)&me;

now you can use..
Camera.ptr->x,y,z no matter what else you put in the other structs.

OR:

VertexS *Camera;
PlayerS me;

Camera = (VertexS)&me;
Camera->x
Camera->y
Camera->z

now you can just send camera, which is just a pointer to an object, to a function. Either way would work fine.

Share this post


Link to post
Share on other sites
it works! even with derived classes and all that other stuff I was talking about. Thanks a lot.

Share this post


Link to post
Share on other sites
I wouldn''t use cast such way though. This looks quite error prone.

I think the least you should do is declare player as:
struct PlayerS
{
VertexS position;
};

And then assign:
Camera.ptr = &me.position;

This way you can get rid of that dangerous cast.

Also, if you would want to do things in your original way, where you derived each class from base object class, you could write:

class Object
{
public:
VertexS position;
};

class Player : public Object
{ /* blah blah */ };

class Monster : public Object
{ /* blah blah */ };

class Camera
{
public:
Camera() {objptr = 0;}
~Camera() {objptr = 0;}

Attach(Object * obj) {objptr = obj;}
Detach() {objptr = 0;}

private:
Object * objptr;
};

Player player;
Monster monster;
Camera cam ;

cam.Attach(&player);
or
cam.Attach(&monster);

Then in camera you can get coordinates by writing:

if(objptr)
{
int x = objptr->position.x;
int y = objptr->position.y;
int z = objptr->position.z;
}

There''s one danger though, if player or monster object gets destroyed and camera stays attached to this object. Then objptr would point to deallocated memory, and could cause crashes. This could be handled with some kind of double reference system, where list of attached cameras is also kept in the attached object, and when object is destroyed, all cameras are detached before memory gets deallocated. This way your code could be more bulletproof and a bit more extendable.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!