Jump to content

Pick distance


MexSource
 Share

Recommended Posts

Hey,

I think something ins't functioning properly here.

I have this:

 

(In my player class)

 

if (wd->KeyHit(Key::E)){
 Vec2 mousepos = wd->GetMousePosition();
 if (cam->Pick(mousepos.x, mousepos.y, pickinfo, 0.1)){
  if (pickinfo.entity->GetKeyValue("tag") == "dbutton"){
   //pickinfo.entity->SetKeyValue("tag", "");
   System::Print("Entity picked, distance: " + String(pickinfo.distance));
   pickinfo.entity->SetPosition(pickinfo.entity->GetPosition().x, pickinfo.entity->GetPosition().y - 2, pickinfo.entity->GetPosition().z);
  }
 }
}

 

But it only get triggered if i'm nearly directly in contact with that object.

 

The line

System::Print("Entity picked, distance: " + String(pickinfo.distance));

only says:

Entity picked, distance -1

 

Is that -1 a convert problem or because I'm so close to the object that I'm touching it?

C++ :3

Link to comment
Share on other sites

Try with this Lua code example

 

if self.entity.world:Pick(p0,p1, pickInfo, 0, true) then

 

 

From Lua example :

 


pickradius = 0.5

....
function App:Loop()
...

if (self.camera:Pick(p.x,p.y,pickinfo,pickradius,true)) then

self.picksphere:Show()
self.picksphere:SetPosition(pickinfo.position)
end

Perhaps change your pickradius, and add "true" as last parameter.

Stop toying and make games

Link to comment
Share on other sites

It seems your problem is that your two positions are your mousepos.x and mousepos.y.

The two positions you need are 1 the area the pick starts from and 2 the area the pick will end.

example positions that may work--

1-self.entity:GetPosition()

2-target.entity:GetPosition()

 

or

1-self.entity:GetPosition()

2-self.entity:GetPosition()+Vec3(0,0,5)

Check out my game Rogue Snowboarding- 

https://haydenmango.itch.io/roguesnowboarding

Link to comment
Share on other sites

Did you even try it? and btw they are in Vec3.

<p>

"Syntax

  • bool Pick(const Vec3& p0, const Vec3& p1, Pick& pick, float radius = 0.0, bool closest=false, bool recursive=false, int collisiontype=0)

Parameters

  • p0: the origin of the ray in global coordinates.
  • p1: the terminal end of the ray in global coordinates."

 

"

PickInfo

 

The pick class is a container for storing information about a pick operation.

 

Members

  • Vec3 position
  • Vec3 normal
  • Entity* entity
  • Surface* surface
  • Face* face
  • int triangle"

 

Using Vec3 positions for the start and end of the pick works for me.

Check out my game Rogue Snowboarding- 

https://haydenmango.itch.io/roguesnowboarding

Link to comment
Share on other sites

Mexsource is using a camera pick and not a global pick. He doesn't have to specify a starting 3d point as the campick takes the screencoordinates in to account as a startposition.

http://www.leadwerks.com/werkspace/page/documentation/_/command-reference/camera/camerapick-r199

 

@Mexsource, can you post a small demo a map and your code? you can also send a pm and I will have a look.

Link to comment
Share on other sites

Ok here some more information:

 

I try to create a game where you need to press a button, i wanted to do this with a raycast

 

This is the pick code, it's from my MainPlayer.cpp "update" function that set up in my App::Loop function:

if (wd->KeyHit(Key::E)){
 Vec2 mousepos = wd->GetMousePosition();
 if (cam->Pick(mousepos.x, mousepos.y, pickinfo, 0)){
  if (pickinfo.entity->GetKeyValue("tag") == "dbutton"){
   //pickinfo.entity->SetKeyValue("tag", "");
   System::Print("Entity picked, distance: " + String(pickinfo.distance));
   pickinfo.entity->Hide();
  }
 }
}

 

on the map load i set the tag "dbutton" for every object that has "dbutton" in it's name. Later when I did the raycast i would remove the tag so the button is "pressed" (outcommented for testing). It should then print the distance from camera to object. And then the button should be set invisible, so you can see it's pressed.

 

And here are my problems: The line that should output the distance only says -1 everytime and it only works if you are so close to the object, that you are nearly touching it.

 

I will attach my current map. (Maybe it could help)

Btw: maybe it's not working because my objects are csg objects?

 

Is this enough explanation or do you need full code?

 

 

Btw: why can't I upload .map files?

map.zip

C++ :3

Link to comment
Share on other sites

Yep, will do

 

Here it is.

 

 

Rick what do you mean, how could I do it?

I tried:

if (wd->KeyHit(Key::E)){
Vec3 pickPos2 = wd->GetMousePosition();
pickPos2.z = 10;
if (wrd->Pick(cam->GetPosition(), cam->UnProject(pickPos2), pickinfo, 0.0F, true)){
if (pickinfo.entity->GetKeyValue("tag") == "dbutton"){
//pickinfo.entity->SetKeyValue("tag", "");
System::Print("Entity picked, distance: " + String(pickinfo.distance));
//pickinfo.entity->SetPosition(pickinfo.entity->GetPosition().x, pickinfo.entity->GetPosition().y - 2, pickinfo.entity->GetPosition().z);
}
}
}

 

wouldn't this do a raycast of length 10, in the looking direction of the camera? Because it only works when i look at a object in a certain angle that.

 

Fixed it, you need to use:

 

Vec3 pickPos2 = Vec3(contxt->GetWidth() / 2, contxt->GetHeight() / 2, 5);

 

instead of

 

Vec3 pickPos2 = wd->GetMousePosition();
pickPos2.z = 10;

 

seems like mouseposition really returns the mouse position on whole screen not only for the window, how i thought

C++ :3

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...