Jump to content

Mouse over block in 3d world ?-> how to check that


Slastraf
 Share

Recommended Posts

For my towerdefense game I want to allow the player to place a turret into the 3d world with the mouse.

I dont understand how to send a raycast from the camera when you click somewhere in the 3d world.

For example if i want to place a block on the terrain by left clicking inside the game.

Also I dont know how raycasting in general works . a link for that should be enough.

 

thanks

Friedrich

Link to comment
Share on other sites

Camera Pick: http://www.leadwerks.com/werkspace/page/api-reference/_/camera/camerapick-r199

 

simple example:

window = Window:Create("example",0,0,800,600)

context = Context:Create(window)

world = World:Create()

camera = Camera:Create()

camera:SetPosition(0,6,0)

light = DirectionalLight:Create()

light:SetRotation(45,45,0)

terrain = Terrain:Create(128,true)

 

box = Model:Box()

box:SetPickMode(0)

box:SetColor(1,0.5,0,1)

 

camrot = camera:GetRotation()

gx=Math:Round(context:GetWidth()/2)

gy=Math:Round(context:GetHeight()/2)

move = 0

strafe = 0

 

while window:KeyDown(Key.Escape)==false do

if window:Closed() then break end

 

mpos = window:GetMousePosition()

dx = mpos.x - gx

dy = mpos.y - gy

camrot.x = camrot.x + dy / 10.0

camrot.y = camrot.y + dx / 10.0

camera:SetRotation(camrot)

window:SetMousePosition(gx,gy)

move = Math:Curve(((window:KeyDown(Key.W)and 1 or 0) - (window:KeyDown(Key.S) and 1 or 0)),move,10)

strafe = Math:Curve(((window:KeyDown(Key.D)and 1 or 0) - (window:KeyDown(Key.A) and 1 or 0)),strafe,10)

camera:Move(strafe,0,move)

 

if window:MouseHit(1) then

pickinfo = PickInfo()

if (camera:Pick(mpos.x,mpos.y,pickinfo,0,true)) then

box:SetPosition(pickinfo.position)

end

end

 

Time:Update()

world:Update()

world:Render()

context:Sync(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

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