Jump to content

Raycasting / Pick help

Recommended Posts

I'm trying to translate my raycasting experience in Unity and Blender GE to Leadwerks, but I'm having a hard time connecting the dots. My test scene includes one cube on the ground, and another cube next to the first (on X axis) and raised above the ground and above the height of the ground cube so that when the game starts, the raised cube falls next to the ground cube. I have a lua script attached to the ground cube and I'm trying to cast a ray from the ground cubes position to a position farther down the x axis so that the raised cube will intersect when it falls.


This is my code in the UpdateWorld function. I get an blank error window. I'm very inexperienced with Leadwerks, so I may be going about it all wrong, but this is what I've gathered from the docs.


Thanks for any help you can provide.


function Script:UpdateWorld()
self.position = self.entity:GetPosition()
self.right = Vec3(self.position.x + self.right.x, self.position.y, self.position.z);

local pickInfo = PickInfo()
local clear = true
local world = World:GetCurrent()
local context = Context:GetCurrent()

local pos = self.camera:Project(Vec3(self.position))
local pRight = self.camera:Project(Vec3(self.right))
context:DrawLine(pos.x, pos.y, pRight.x, pRight.y)

if (world:Pick(self.position, self.right, pickinfo, 0.0, true)) then
clear= false

Share this post

Link to post

To me it looks like you are referencing a variable that doesn't exist yet:

The line

self.right = Vec3(self.position.x + self.right.x, self.position.y, self.position.z);

uses the variable self.right.x, which isn't defined yet, I guess.

Try using a constant there (something like

self.right = Vec3(self.position.x + 100, self.position.y, self.position.z);

) and see, if it solves the issue.

Share this post

Link to post

[self.right] is defined outside of the function as:


Script.right=Vec3(1, 0, 0)--Vec3


I found the problem, I had accidentally used a lowercase in the [pickInfo] variable in the Pick function. It's working now.



Share this post

Link to post

That solved the raycast problem, But I'm still having issues drawing the line for debugging. I'm getting an error with no error message on the following line:

local pos = self.camera:Project(Vec3(self.position)) 


I have the camera defined outside the function as:


And I linked the scene camera to this property in the editor.

Share this post

Link to post

The problem is that self.position is already a Vec3, so you just need to remove "Vec3"

local pos = self.camera:Project(self.position)


Also note, that your value of right.x will increase each frame, as in the first one it is:

right.x = 1 ==> position.x + right.x = position.x + 1

right.x = position.x + 1 ==> position.x + right.x = position.x + position.x + 1

right.x = position.x + position.x + 1 ==> position.x + right.x = position.x + position.x + position.x + 1


Share this post

Link to post

Looks like my duplicated cube had a duplicate of the script and the Camera was not bound to the exposed entity property.


Removing the script on the second cube, and removing the Vec3 cleared the errors.


I moved my self.right setter to the Start function.


Thanks for helping, sorry for the Leadwerks noob mistakes!

Share this post

Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

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.

  • Create New...