3 Replies - 459 Views - Last Post: 16 August 2013 - 01:14 PM Rate Topic: -----

#1 frostraver  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 93
  • Joined: 11-September 12

Picking isn't working in 3D environment

Posted 15 August 2013 - 06:49 AM

Hi guys, yes here I am again with yet another problem.

I'm trying to to pick an object that is in my scene and then do stuff with it.

I created an object that I can see, so I guess that works, that has a Rigid Body and a MeshCollider. (It's component based if that's important)

After I create the object I try to pick it. My picking function is the following:

GameObject* PickComponent::Pick(POINT mousePosition, D3DXVECTOR2 screenSize)
{
	//Transform to NDC
	float ndc_X = (mousePosition.x - (screenSize.x/2)) / (screenSize.x/2);
	float ndc_Y = ((screenSize.y/2)- mousePosition.y) / (screenSize.y/2);

	//Transform the near and far point with viewprojection inverse
	auto viewProjection = GetGameScene()->GetActiveCamera()->GetView() * GetGameScene()->GetActiveCamera()->GetProjection();
	D3DXMatrixInverse(&viewProjection, NULL, &viewProjection);

	D3DXVECTOR3 nearPoint(0,0,0);
	D3DXVec3TransformCoord(&nearPoint, &D3DXVECTOR3(ndc_X, ndc_Y, 0), &viewProjection);
	D3DXVECTOR3 farPoint(0,0,0);
	D3DXVec3TransformCoord(&farPoint, &D3DXVECTOR3(ndc_X, ndc_Y, 1), &viewProjection);

	NxVec3 direction = NxVec3(farPoint - nearPoint);
	//float distance = direction.magnitude();
	direction.normalize();

	NxVec3 startPos = NxVec3(nearPoint);
	NxRay ray = NxRay(startPos, direction);
	
	//GetClosestShape
	auto shape = PhysicsManager::GetInstance()->GetClosestShape(GetGameScene()->GetPhysicsScene(), ray, NX_ALL_SHAPES, 1000000.0f);

	if( shape != nullptr)
	{
		auto temp = (RigidBodyComponent*)shape->userData;
		return temp->GetParentGameObject();
	}
	
	return nullptr;
}


It always returns nullptr and I really can't figure out why.

My mousePosition I give to this method is right and I think the calculations above are right as well.
Is there anyone who has some experience with this and knows what could be wrong so I can check that out?

Thanks in advance!

Is This A Good Question/Topic? 0
  • +

Replies To: Picking isn't working in 3D environment

#2 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 1
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Picking isn't working in 3D environment

Posted 15 August 2013 - 11:24 AM

What is the actual value of shape? Once determining that you can see if it's an error returned by PhysicsManager::GetInstance()->GetClosestShape() singleton or not. The condition shape != nullptr is a bit too general an indicator of what is going on, in that it only tells you that 'something' went wrong rather than what 'is' wrong.
Was This Post Helpful? 0
  • +
  • -

#3 frostraver  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 93
  • Joined: 11-September 12

Re: Picking isn't working in 3D environment

Posted 16 August 2013 - 10:02 AM

the value of shape stays null (so completely empty) when GetClosestShape() returns. I checked at the "if" line what the value of shape was so I'm sure it was assigned something. I'll check out the GetClosestShape() for now.
Was This Post Helpful? 0
  • +
  • -

#4 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 1
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Picking isn't working in 3D environment

Posted 16 August 2013 - 01:14 PM

As you've guessed, when null is returned something is very wrong. Check the docs for what you're using.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1