playerNode getPosition

Get answers to all your basic programming questions. No Ogre questions, please!
marcalaque
Kobold
Posts: 29
Joined: Sun Jul 21, 2013 9:51 am

playerNode getPosition

Post by marcalaque »

I have a problem with my first person camera wherein I wanted to put a mouse event on left click if the playernode went to the position on the area of x, y and z
I am quite confuse because the position I am putting in and using is the Vector3
Here are my codes

Code: Select all

if(id == OIS::MB_Left)
	{
		
		if(m_pCamera->getPosition() == Ogre::Vector3(1970 < 1985, 4 < 7, 100 < 120))
		{
		onLeftPressed(evt);
		playerNode->setPosition(1974.8, 4.6, 98);
		//playerNode->lookAt(mNode->getPosition(), Node::TS_PARENT, Vector3::UNIT_X);
		//mNode->lookAt(playerNode->_getDerivedOrientation(), Node::TS_PARENT, Vector3::UNIT_Z);
		//mNode->_getDerivedOrientation();
		m_bLMouseDown = true;
		}
		
	}
Any help please? :D
You do not have the required permissions to view the files attached to this post.
User avatar
Kojack
OGRE Moderator
OGRE Moderator
Posts: 7157
Joined: Sun Jan 25, 2004 7:35 am
Location: Brisbane, Australia
x 535

Re: playerNode getPosition

Post by Kojack »

I'm not completely sure what it is you are trying to do, but this is not doing what you think it should:

Code: Select all

if(m_pCamera->getPosition() == Ogre::Vector3(1970 < 1985, 4 < 7, 100 < 120))
It looks like you want to check if the camera is within a rectangular region. But 1970 < 1985 isn't a range, it's a direct comparison that gives a true or false result. 1970 is always less than 1985, so the result is always true. The same with the other comparisons.
It's also generally a bad idea to do == with floats (or classes made from them like Vector3). Due to floating point error, values can be a fraction off, which makes the comparison fail.

You need to do something like this:

Code: Select all

Ogre::Vector3 pos = m_pCamera->getPosition();
if(pos.x >= 1970 && pos.x <= 1985 && pos.y >= 4 && pos.y <= 7 && pos.z >= 100 && pos.z <= 120)
User avatar
areay
Bugbear
Posts: 819
Joined: Wed May 05, 2010 4:59 am
Location: Auckland, NZ
x 69

Re: playerNode getPosition

Post by areay »

This bit is wrong

Code: Select all

 if(m_pCamera->getPosition() == Ogre::Vector3(1970 < 1985, 4 < 7, 100 < 120))
Is your intention to see if the player's position falls within the range (1970 < x < 1985, 4 < y < 7, 100 < z < 120) ??

Because what's actually happening here is that your compiler is looking at those statements and saying;
Is 1970 < 1985? Yes! Then substitute in the value for true (1) etc, etc, which means that your statement compiles down to something like this

Code: Select all

 if(m_pCamera->getPosition() == Ogre::Vector3(1, 1, 1))

Which is why it's not working.

What I'd do is create an axis aligned box http://www.ogre3d.org/docs/api/html/cla ... edBox.html and then test if the player's position is within that

Code: Select all

Ogre::AxisAlignedBox *myAAB =  new Ogre::AxisAlignedBox(Ogre::Vector3(1970,4,100), Ogre::Vector3(1985,7,120));
if (myAAB.intersects(m_pCamera->getPosition())
{
 blah
}
marcalaque
Kobold
Posts: 29
Joined: Sun Jul 21, 2013 9:51 am

Re: playerNode getPosition

Post by marcalaque »

Thanks a lot to the both of you It is working now I will try to finish my game for our thesis defense :D