Jump to content

get the Yaw of Pick result direction ?


YouGroove
 Share

Recommended Posts

Hi,

 

I use Pick result and it's normal to rotate some object to that normal vector , but something is wrong.

Is normal of Pick normal to the surface that was hit ?

 

This code always returns the same Yaw instead of a Yaw based on the surface hit by the racast ?

 

local invNormal =  pickInfo.normal:Inverse() 
local angle = Math:ATan2(  pickInfo.normal .y,   pickInfo.normal .x)
 self.yawPlayer =  angle + 180
end

 

Any clues ? The idea is just the character and camera rotated to face the wall or object hit by the raycast.

Stop toying and make games

Link to comment
Share on other sites

A normal is between -1 and 1 and is used to show direction/orientation. If the picked object is a vertical face (like a wall), then 'normal.y' will always be 0. If the picked face is parallel to the x-axis, then it will also return 0. So performing a Math:Atan2 on (0,0) will always return 0. So the return here is just the orientation without regards to the player's current position. So the best it can do is orient the player but doesn't mean the player will be facing the object picked. The code listed here (similar to the same post that you have already posted here) that uses the position of the pick will return what you need.

Win7 64bit / Intel i7-2600 CPU @ 3.9 GHz / 16 GB DDR3 / NVIDIA GeForce GTX 590

LE / 3DWS / BMX / Hexagon

macklebee's channel

Link to comment
Share on other sites

I see, but if i use to calculate the vector :

- Player position

- Pick raycast hit position

This will represent the orientation between the player and the wall not the normal vector of the face of the wall hit by the raycast.

The goal was to make the character face the wall, whatever is the Yaw angle of the face of the wall.

 

Perhaps i just don't understood the function so i'll give a try at your solution using the pick position.

Stop toying and make games

Link to comment
Share on other sites

Yes, it is supposed the determine the angle between the picked position and the player. You need a reference to the player position to determine how much the player needs to turn to face the picked position. You could use wall normals to determine how to orient the player but it will not necessarily end up with the player facing the wall itself.

 

Here is an example of how to use the camera and picked positions to determine the angle to rotate the camera to face the picked position:

function App:Start()
  self.window=Window:Create("Test",0,0,800,600,window.Titlebar+window.Center)
  self.context=Context:Create(self.window,0)
  self.world=World:Create()
  self.light = DirectionalLight:Create()
  self.light:SetRotation(35,35,0)
  self.camera = Camera:Create()

  self.box = Model:Box()
  self.box:SetColor(1,.5,0,1)
  self.box:SetPosition(1,1,3)

  self.box1 = Model:Sphere()
  self.box1:SetColor(1,0,0,1)
  self.box1:SetPosition(-1,0,3)

  function PerfectAngle(pos1,pos2)
     local dx, dz
     dx = pos1.x - pos2.x
     dz = pos1.z - pos2.z
     return Math:ATan2(dx,dz)
  end

  pickinfo = PickInfo()
  pick = nil
  angle = self.camera:GetRotation().y

  return true
end

function App:Loop()
  if self.window:Closed() or self.window:KeyDown(Key.Escape) then return false end

  if (self.window:MouseHit(1)) then
     local mousePos = self.window:GetMousePosition()
     pick = self.camera:Pick(mousePos.x, mousePos.y, pickinfo, 0, true)
  end
  if pick then
     perfectangle = PerfectAngle(pickinfo.position,self.camera.position)
     angle=Math:IncAngle(perfectangle,angle,5/Time:GetSpeed())
     self.camera:SetRotation(0,angle,0)
  end

  Time:Update()
  self.world:Update()
  self.world:Render()
  self.context:Sync(true)

  return true
end

  • Upvote 1

Win7 64bit / Intel i7-2600 CPU @ 3.9 GHz / 16 GB DDR3 / NVIDIA GeForce GTX 590

LE / 3DWS / BMX / Hexagon

macklebee's channel

Link to comment
Share on other sites

Well the perfect angle is used to rotate some entity towards some other entity.

But that can't work for a cover system, if i use it the player rotates but based on it's position and pick position.

And for a cover system you need the player to face the wall surface normal vector, whatever is the player direction.

cover.jpg

 

Can't we use Pick.normal ? It is not the normal vector of the wall surface whatever is the surface angle ?

I think we need the normal of the surface (triangle) where the Pick hits something.

cover3.jpg

Stop toying and make games

Link to comment
Share on other sites

Well thats something completely different. You are asking to rotate the player to not face the picked position but to also move the player. You can use the picked position and the normal to determine location.

newPos = pickinfo.position + Vec3(1,1,1) * pickinfo.normal --where vec3(1,1,1) is the max distance from the picked pos

 

and you should be able to perform the '+ 180' to the player rotation after the player is positioned while rotating to face the point.

 

Keep in mind you will probably have to perform some checks to make sure that 'normal.y' doesn't put the player in a weird location in case the pick can ever be ontop or below a "box".

  • Upvote 1

Win7 64bit / Intel i7-2600 CPU @ 3.9 GHz / 16 GB DDR3 / NVIDIA GeForce GTX 590

LE / 3DWS / BMX / Hexagon

macklebee's channel

Link to comment
Share on other sites

Well thats something completely different. You are asking to rotate the player to not face the picked position but to also move the player. You can use the picked position and the normal to determine location.

No i don't want the player to move. You gave me the good solution smile.png

 

We can calculate a virtual position then calculate the Yaw to apply to the player without having to move the player

 

 	 local virtualPos = piInfo.position + ( Vec3(1,1,1) * piInfo.normal )
rotY = PerfectAngle( piInfo.position , virtualPos)

 

Applying that rotation to the player and it always faces the normal of the surfaces whenever it is it's position.

 

 

Keep in mind you will probably have to perform some checks to make sure that 'normal.y' doesn't put the player in a weird location in case the pick can ever be ontop or below a "box".

Perhaps giving a bigger value to the pick cylinder argument ?

Is pick performing on visual mesh only ?

Stop toying and make games

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...