[Suggested addition] ICameraSceneNode::setTargetNode()

You discovered a bug in the engine, and you are sure that it is not a problem of your code? Just post it in here. Please read the bug posting guidelines first.
Post Reply
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

[Suggested addition] ICameraSceneNode::setTargetNode()

Post by rogerborg »

We often suggest that a camera problem can be solved by having a camera look at the position of another scene node every frame. So... why not have the camera do it itself?

I suggest:

ICameraSceneNode::setTargetNode()
ICameraSceneNode::getTargetNode()

I suggest that naming because the get method needs to be disambiguated, and the set should be consistent.

The change to example 04 is just to demonstrate the functionality, not a commit candidate.

Code: Select all

Index: examples/04.Movement/main.cpp
===================================================================
--- examples/04.Movement/main.cpp	(revision 1250)
+++ examples/04.Movement/main.cpp	(working copy)
@@ -183,6 +183,8 @@
 	scene::ICameraSceneNode * cam = smgr->addCameraSceneNodeFPS(0, 100.0f, 100.0f);
 	device->getCursorControl()->setVisible(false);
 
+	cam->setTargetNode(anms);
+
 	/*
 	Add a colorful irrlicht logo
 	*/
Index: include/ICameraSceneNode.h
===================================================================
--- include/ICameraSceneNode.h	(revision 1250)
+++ include/ICameraSceneNode.h	(working copy)
@@ -66,6 +66,15 @@
 		/** \return Returns the current look at target of the camera */
 		virtual core::vector3df getTarget() const = 0;
 
+		//! Sets a scene node as the look at target of the camera.
+		//! The camera will track this node's position.
+		/** \param node: Look at target of the camera. */
+		virtual void setTargetNode(ISceneNode * node) = 0;
+
+		//! Gets the current look at target node of the camera
+		/** \return Returns the current look at target node of the camera, which may be 0 */
+		virtual ISceneNode * getTargetNode() const = 0;
+		
 		//! Sets the up vector of the camera.
 		/** \param pos: New upvector of the camera. */
 		virtual void setUpVector(const core::vector3df& pos) = 0;
Index: source/Irrlicht/CCameraSceneNode.cpp
===================================================================
--- source/Irrlicht/CCameraSceneNode.cpp	(revision 1250)
+++ source/Irrlicht/CCameraSceneNode.cpp	(working copy)
@@ -23,8 +23,10 @@
 	setDebugName("CCameraSceneNode");
 	#endif
 
+	// By default, don't track a target node
+	TargetNode = 0;
+
 	// set default view
-
 	UpVector.set(0.0f, 1.0f, 0.0f);
 	Target.set(lookat);
 
@@ -49,6 +51,8 @@
 //! destructor
 CCameraSceneNode::~CCameraSceneNode()
 {
+	if(TargetNode)
+		TargetNode->drop();
 }
 
 
@@ -125,7 +129,30 @@
 }
 
 
+//! Sets a scene node as the look at target of the camera.
+//! The camera will track this node's position.
+/** \param node: Look at target of the camera. */
+void CCameraSceneNode::setTargetNode(ISceneNode * node)
+{
+	if(TargetNode)
+		TargetNode->drop();
 
+	TargetNode = node;
+
+	if(TargetNode)
+		TargetNode->grab();
+}
+
+//! Gets the current look at target node of the camera
+/** \return Returns the current look at target node of the camera, which may be 0 */
+ISceneNode * CCameraSceneNode::getTargetNode() const
+{
+	return TargetNode;
+}
+
+
+
+
 //! sets the up vector of the camera
 //! \param pos: New upvector of the camera.
 void CCameraSceneNode::setUpVector(const core::vector3df& pos)
@@ -197,6 +224,12 @@
 //! prerender
 void CCameraSceneNode::OnRegisterSceneNode()
 {
+	if(TargetNode)
+	{
+		TargetNode->updateAbsolutePosition();
+		setTarget(TargetNode->getAbsolutePosition());
+	}
+
 	// if upvector and vector to the target are the same, we have a
 	// problem. so solve this problem:
 
Index: source/Irrlicht/CCameraSceneNode.h
===================================================================
--- source/Irrlicht/CCameraSceneNode.h	(revision 1250)
+++ source/Irrlicht/CCameraSceneNode.h	(working copy)
@@ -53,6 +53,15 @@
 		//! \return Returns the current look at target of the camera
 		virtual core::vector3df getTarget() const;
 
+		//! Sets a scene node as the look at target of the camera.
+		//! The camera will track this node's position.
+		/** \param node: Look at target of the camera. */
+		virtual void setTargetNode(ISceneNode * node);
+
+		//! Gets the current look at target node of the camera
+		/** \return Returns the current look at target node of the camera, which may be 0 */
+		virtual ISceneNode * getTargetNode() const;
+
 		//! Sets the up vector of the camera.
 		//! \param pos: New upvector of the camera.
 		virtual void setUpVector(const core::vector3df& pos);
@@ -128,6 +137,8 @@
 		core::vector3df Target;
 		core::vector3df UpVector;
 
+		ISceneNode * TargetNode; // May be 0
+
 		f32 Fovy;	// Field of view, in radians. 
 		f32 Aspect;	// Aspect ratio. 
 		f32 ZNear;	// value of the near view-plane. 
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

Why not using a scene node animator instead? Continuous transformations are often done using them.
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

:? I'm not aware of any animator that can directly affect a camera's look-at target. Example 04 does use a scene node animator, but on the node that I've told the camera to look at.

This patch is really just a convenient equivalent for calling (e.g. in example 04) cam->setTarget(anms->getAbsolutePosition()) in the app every frame. It's not a big issue, it just seems like having a camera look at another scene node is a natural thing to do.
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

No, there's none yet, but maybe it's better to put this functionality into an animator instead of putting more and more into the camera interface.
BlindSide
Admin
Posts: 2821
Joined: Thu Dec 08, 2005 9:09 am
Location: NZ!

Post by BlindSide »

How about we start by overriding the setRotation() method of the camera so that it actually rotates the cameras view using a normalised position and setTarget (And possibly modifying the upVector).

After this we can have a smgr->createLookAtAnimator(ISceneNode* target).

This animator can then be used on any scene node, including a camera.
ShadowMapping for Irrlicht!: Get it here
Need help? Come on the IRC!: #irrlicht on irc://irc.freenode.net
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

That sounds fair enough. Are you interested in implementing it?
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

Why not some camera specific animator? Since we want to put the camera controls into specialized animators anyway, it's just as good (and does not need to change the setRotation/setTarget semantics).
Dorth
Posts: 931
Joined: Sat May 26, 2007 11:03 pm

Post by Dorth »

Well, SetRotation doesn't act like it promises right now. So I agree that should be modified. And animators are nice, but they shouldn't be there at the cost of easilly doing it by the direct access functions.
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

hybrid wrote:Since we want to put the camera controls into specialized animators anyway, [...]
To be honest, I've never been that keen on using animators for anything that will typically have some degree of application control, like cameras or projectiles. Using an animator to control the projectile in the Demo app is just... urgh. It very cleverly demonstrates animators, but it's a poor solution for projectiles.

Likewise, having an animator drive the camera's look-at position, well, great, let's have that, but what this patch provides is a simple way to have a camera track another scene node, which is generally useful irrespective of whether you're using animators or not. They're not exclusive.
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
randomMesh
Posts: 1186
Joined: Fri Dec 29, 2006 12:04 am

Post by randomMesh »

Just my two cents: I prefer the solution of rogerborg.
bitplane
Admin
Posts: 3204
Joined: Mon Mar 28, 2005 3:45 am
Location: England
Contact:

Post by bitplane »

I'm personally not in favour of this idea as it increases complexity of all cameras. Also the position of the node is not always the centre, getCenter of the transformed bounding box would work better, but the centre of the node is not always the point of interest either.

Also, on the subject of controlling cameras more easily, I just added this thread:
http://irrlicht.sourceforge.net/phpBB2/ ... p?p=147619
Submit bugs/patches to the tracker!
Need help right now? Visit the chat room
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

Fair enough.

Me == Image

:P
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
MasterGod
Posts: 2061
Joined: Fri May 25, 2007 8:06 pm
Location: Israel
Contact:

Post by MasterGod »

Coming to get you rogerborg :!:
Image

:lol:
Image
Dev State: Abandoned (For now..)
Requirements Analysis Doc: ~87%
UML: ~0.5%
Frosty Topaz
Posts: 107
Joined: Sat Nov 04, 2006 9:42 pm

Post by Frosty Topaz »

bitplane wrote:Also the position of the node is not always the centre, getCenter of the transformed bounding box would work better, but the centre of the node is not always the point of interest either.
I would assume that the origin of the node is always the point of interest... if not you could always attach an empty scene node with some offset, and then target that.
Frosty Topaz
=========
It isn't easy being ice-encrusted aluminum silicate fluoride hydroxide...
Post Reply