Jump to content

Camera with collision. Help please...?


ChrisV
 Share

Recommended Posts

Hi everyone,

 

I'm having some problems getting a camera that collides with the walls/environment to work in the LESoldierEntityAndGame script I downloaded from the Asset store.

The camera in the script works nicely for a third person view, but there's no collision with the environment. :P

My idea is to add a sphere to the scene, and attach the sphere to the camera and check for collision with the sphere and the environment, but how do I do the collision stuff?

I know how to attach the sphere (CreateBodySphere), but I can't get collision working.

Would creating a sphere in a 3D program and then loading it in the editor and attach it to the camera work?

Or, is there another way of doing this?

Is there someone that could help me getting this to work?

Thanks. :(

 

Here's the LESoldierEntityAndGame script:

 

--[[
Leadwerks Soldier Game Script by Paul "Masterxilo" Frischknecht
(c) 2010 Paul Frischknecht/Hurricane-Eye Entertainment
visit http://www.hurricane-eye.webs.com/

This implements an RPG like 3rd person camera (controlled by holding rmb and moving the mouse/zooming with mousewheel)
and a keyboard controlled soldier character.
Clicking on entities not more than 2m away from the character will send them a "use" message
]]

--Required scripts
require("Scripts/constants/keycodes")
require("Scripts/constants/engine_const")
require("Scripts/constants/collision_const")
require("Scripts/LinkedList")
require("Scripts/filesystem")
require("Scripts/math/math")
require("Scripts/math/vector")
require("Scripts/class")

-- Constants
ADJUST_POSITION_STEPS = 50.0
ZOOM_SHIFT_MULTIPLIER = 5.0
HEAD_YOFFSET	      = 1.75
COLLISION_HELPER      = 10

--Setup
ShowMouse()
FlushKeys()
FlushMouse()

--Camera control
--Mouse movement
gx=Round(GraphicsWidth()/2)
gy=Round(GraphicsHeight()/2)
dx = 0.0 dy = 0.0 dz = 0.0 lastMz = MouseZ()

camera=fw.main.camera
camerapitch=EntityRotation(camera).x
camerayaw=EntityRotation(camera).y
campiv=CreatePivot()
camdistance=4.0
initialSoldierPos = TFormPoint(Vec3(0,0,3),camera) initialSoldierPos.y = initialSoldierPos.y-HEAD_YOFFSET

--Camera control function
function _3rdPersonCameraControl(followPos)
       -- Adjust rotation and distance
       if MouseDown(2) ~= 0 then
	--Camera look
	HideMouse()
	if MouseHit(2) ~= 0 then MoveMouse(gx,gy) end
	dx=MouseX()-gx
	dy=MouseY()-gy
	MoveMouse(gx,gy)

	camerayaw = camerayaw-dx*0.1
	camerapitch = camerapitch+dy*0.1
       else
	--ShowMouse()
end

dz = Curve(MouseZ()-lastMz,dz ,1.0/AppSpeed())
lastMz = MouseZ()

mul = 1
if KeyDown(KEY_LSHIFT) ~= 0 then mul = ZOOM_SHIFT_MULTIPLIER end
       camdistance = camdistance- dz*0.5*mul 
camdistance = math.max(camdistance, 1.0);

       -- Figure out new position
       lastPosition = EntityPosition(camera)

--Find wished position
       PositionEntity(camera, followPos)
RotateEntity(camera, Vec3(camerapitch,camerayaw,0))
       MoveEntity(camera, Vec3(0,0,-camdistance))
targetPosition = EntityPosition(camera)

--Correct with collision position
pick=LinePick(followPos, targetPosition, 0, COLLOSION_PROP) 
if pick~=nil then
           targetPosition = Vec3(pick.position.x, pick.position.y, pick.position.z)
end

-- Apply position
lastPosition.x = Curve(targetPosition.x, lastPosition.x, ADJUST_POSITION_STEPS*(1.0/AppSpeed()))
lastPosition.y = Curve(targetPosition.y, lastPosition.y, ADJUST_POSITION_STEPS*(1.0/AppSpeed()))
lastPosition.z = Curve(targetPosition.z, lastPosition.z, ADJUST_POSITION_STEPS*(1.0/AppSpeed()))

PositionEntity(camera, lastPosition)
PositionEntity(campiv, followPos)
PointEntity(camera, campiv, 3, 1.0, 0.0)

end

--Create the player
soldier=LoadModel("abstract::soldier.gmf")
PositionEntity(soldier, initialSoldierPos)
RotateEntity(soldier, Vec3(0,camerayaw+180,0))

--Create the physics drag helpers 
--[[ WIP
phydragPlane = CreatePlane()
--HideEntity(phydragPlane)
ScaleEntity(phydragPlane, Vec3(1000,1000,1000))
PositionEntity(phydragPlane, Vec3(0,0,0))
EntityType(phydragPlane,COLLISION_HELPER)
Collisions(COLLISION_HELPER,COLLISION_HELPER,1)

phydragPivot = CreateBodySphere(0.025)--CreateBodyPivot()
phydragJoint = nil
phydragDragged = nil]]

-- Helper functions
function getMeshModel(mesh)
curEntity = mesh
repeat
	if GetEntityClass(curEntity)==ENTITY_MODEL then
		return curEntity
	end
	curEntity=curEntity.parent

until 	curEntity==nil or curEntity==0 or EntityExists(curEntity)==0
return nil
end

--Main "function"/loop
while KeyHit(KEY_ESCAPE)==0 do

--Player control
SendEntityMessage(soldier, "SetHeading="..tostring(camerayaw))

if KeyHit(KEY_SPACE) ~= 0 then SendEntityMessage(soldier, "Jump") end

if KeyDown(KEY_W) ~= 0 then
	if KeyDown(KEY_A) ~= 0 then
		SendEntityMessage(soldier, "SetMovement=".."MOVE_FORWARD_LEFT")
	elseif KeyDown(KEY_D) ~= 0 then
		SendEntityMessage(soldier, "SetMovement=".."MOVE_FORWARD_RIGHT")
	else
		SendEntityMessage(soldier, "SetMovement=".."MOVE_FORWARD")
	end
elseif KeyDown(KEY_S) ~= 0 then
	if KeyDown(KEY_A) ~= 0 then
		SendEntityMessage(soldier, "SetMovement=".."MOVE_BACK_LEFT")
	elseif KeyDown(KEY_D) ~= 0 then
		SendEntityMessage(soldier, "SetMovement=".."MOVE_BACK_RIGHT")
	else
		SendEntityMessage(soldier, "SetMovement=".."MOVE_BACK")
	end
elseif KeyDown(KEY_A) ~= 0 then
	SendEntityMessage(soldier, "SetMovement=".."MOVE_LEFT")
elseif KeyDown(KEY_D) ~= 0 then
	SendEntityMessage(soldier, "SetMovement=".."MOVE_RIGHT")
else
	SendEntityMessage(soldier, "SetMovement=".."")
end

-- Use entities
if MouseHit(1) ~= 0 then -- Lag at first click???	
	pick = CameraPick(camera,Vec3(MouseX(), MouseY(), 1000), 0, 0)--COLLISION_PROP)
	if pick~=nil then pickedModel = getMeshModel(pick.entity) end

	if pickedModel~=nil then 
		if KeyDown(KEY_LSHIFT) ~= 0 then
			--[[-- WIP
			PositionEntity(phydragPlane, pick.position)
			PositionEntity(phydragPivot, pick.position)
			phydragJoint=CreateJointFixed(pickedModel,phydragPivot,pick.position)
			phydragJoint:SetStiffness(-10)
			phydragDragged = pickedModel
			RotateEntity(phydragPlane, Vec3(90,camerayaw+180,0))]]
		else
			if EntityDistance(campiv, pickedModel) < 2.0 then
				SendEntityMessage(pickedModel,"use",soldier)
			end		
		end
	end
end

--[[-- WIP
if MouseDown(1) ~= 0 and KeyDown(KEY_LSHIFT) ~= 0 and phydragDragged ~= nil then
	pick=CameraPick(camera,Vec3(MouseX(), MouseY(), 1000), 0, COLLISION_HELPER)
	if pick~=nil then 
		PositionEntity(phydragPivot, pick.position)
		phydragDragged:AddForce(Vec3(0.001, 0,0))
	end
elseif phydragJoint ~= nil then
	FreeJoint(phydragJoint)
	phydragJoint = nil
	phydragDragged = nil

end]]

--Update
fw:Update()

-- Camera control
followPos = EntityPosition(soldier) followPos.y = followPos.y+HEAD_YOFFSET
_3rdPersonCameraControl(followPos)


-- Render
fw:Render()

-- Debug info
--SetBlend(1)
--DrawText("In Game-Mode, running:",0,15*6)
--DrawText("Leadwerks Soldier Game Script by Paul Frischknecht",0,15*7)
--SetBlend(0)

-- Update screen

Flip(0)

end

ShowMouse()

 

Cheers

 

ZBrush 4R7 64-bit - 3DCoat 4.5 BETA 12 - Fl Studio 12 64Bit - LE 3.2 Indie version - Truespace 7 - Blender 2.71 - iClone 5.51 Pro - iClone 3DXChange 5.51 pipeline - Kontakt 5 - Bryce 7 - UU3D Pro - Substance Designer/Painter - Shadermap 3 - PaintShop Photo Pro X7 - Hexagon - Audacity - Gimp 2.8 - Vue 2015 - Reaktor 5 - Guitar Rig 5 - Bitmap2Material 3

Link to comment
Share on other sites

you can use the same idea that is applied to Making a Spectator

Thanks for the response, macklebee.

Actually, that's what I'm trying to do, but when I add the first line 'TBody spectator=CreateBodySphere()' to the Lua script, I'm getting the error '=' expected near 'spectator', and the command 'TBody' doesn't highlight in blue either. Is 'TBody' actually a command? I've searched for it in the Command Reference under 'Bodies', and didn't find it. :(

The spectator example is in C++, so that might explain why 'TBody' doesn't highlight in the Lua script editor.

I'll experiment a bit more, but if anyone could give a small example of how to do this in Lua for the LESoldierEntityandGame script, that would be great. Thanks. :P

 

Cheers

 

ZBrush 4R7 64-bit - 3DCoat 4.5 BETA 12 - Fl Studio 12 64Bit - LE 3.2 Indie version - Truespace 7 - Blender 2.71 - iClone 5.51 Pro - iClone 3DXChange 5.51 pipeline - Kontakt 5 - Bryce 7 - UU3D Pro - Substance Designer/Painter - Shadermap 3 - PaintShop Photo Pro X7 - Hexagon - Audacity - Gimp 2.8 - Vue 2015 - Reaktor 5 - Guitar Rig 5 - Bitmap2Material 3

Link to comment
Share on other sites

Thanks for the answers, guys. :P

 

TBody is a type. In Lua you don't define the type like you do in other languages so just remove TBody and say:

 

spectator = CreateBodySphere()

Yeah, I know. That's what I did after I got the error. But, I still can't find a way to make the collision on the camera (attached sphere) work. :rolleyes:

I'll experiment a bit more. ;)

 

ZBrush 4R7 64-bit - 3DCoat 4.5 BETA 12 - Fl Studio 12 64Bit - LE 3.2 Indie version - Truespace 7 - Blender 2.71 - iClone 5.51 Pro - iClone 3DXChange 5.51 pipeline - Kontakt 5 - Bryce 7 - UU3D Pro - Substance Designer/Painter - Shadermap 3 - PaintShop Photo Pro X7 - Hexagon - Audacity - Gimp 2.8 - Vue 2015 - Reaktor 5 - Guitar Rig 5 - Bitmap2Material 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...