Jump to content

RTS Camera Script Problem


Thirsty Panther
 Share

Recommended Posts

I'm continuing development of my RTS camera script. Which is a modification of the default spectator script.

After fixing "middle mouse button for rotating the camera" problem. Many thanks to Macklebee.

I have run into another problem. 

If you attach the following script to a camera in your scene. Moving the mouse to the top/bottom of the scene will move you forwards/backwards. Move the mouse left/right will move you left/right. Scrolling the mouse wheel will raise/lower the camera height. Holding the middle button and moving the mouse left/right will rotate the camera in that direction.

The problem I have is that after you rotate the camera. Moving the mouse to the top of the scene results in the camera moving in the north direction of the map and not the forward facing direction of the camera. I think my problem is to do with the "move" variable in the Update Physics function.

I've tried changing line 88 to

 local desiredvelocity = Vec3(strafe,0,move) + Transform:Vector(0,ascension,o,nil,self.entity)

ie I've moved the "move" variable from the Transform to the Vec3 factor. This fixes the rotation problem but now when you move the mouse to the top of the screen the camera moves forward but also loses height.

Script.movespeed=5.0--float "Move speed"
Script.movementsmoothing = 0.3--float "Move smoothness"
Script.radius = 0.5--float "Radius"
Script.lookspeed = 0.1--float "Look speed"
Script.looksmoothing = 0.5--float "Look smoothness"
Script.MousBoarder = 50 --float "Mouse Boarder"
Script.camerarotation = 0,0,45 --Vec3 "Camera Rotation"

function Script:Start()

	local window = Window:GetCurrent()
	self.mousepos = window:GetMousePosition()
	window:ShowMouse()
	self.camerarotation = self.entity:GetRotation()
	if (self.entity:GetMass()==0) then
		self.entity:SetMass(10)
	end
	self.entity:SetGravityMode(false)
	if type(self.entity.SetElasticity)=="SetBuoyancyMode" then
		self.entity:SetBuoyancyMode(false)
	end
	self.entity:SetCollisionType(Collision.Projectile)
	if self.entity:GetShape()==nil then
		local shape = Shape:Sphere(0,0,0, 0,0,0, self.radius*2,self.radius*2,self.radius*2)
		self.entity:SetShape(shape)
		shape:Release()
	end
	self.entity:SetFriction(0,0)
	if type(self.entity.SetElasticity)=="function" then
		self.entity:SetElasticity(0)
	end
	self.entity:SetSweptCollisionMode(true)
	self.listener = Listener:Create(self.entity)
	self.entity:SetBuoyancyMode(false)
end

--Collision filter so the spectator doesn't knock things over
function Script:Overlap(entity)
	if entity:GetMass()==0 then
		return Collision.Collide
	else
		return Collision.None
	end
end

function Script:UpdateWorld()
	local window = Window:GetCurrent()
	local cx = Math:Round(context:GetWidth()/2)
	local cy = Math:Round(context:GetHeight()/2)
	local mpos = window:GetMousePosition()
	local mhit = window:MouseDown(Key.MButton)
	window:ShowMouse()
	--window:SetMousePosition(cx,cy)
	local centerpos = window:GetMousePosition()
	if self.started then
		mpos = mpos * self.looksmoothing + self.mousepos * (1-self.looksmoothing)
		local dx = (mpos.x - centerpos.x) * self.lookspeed
		local dy = (mpos.y - centerpos.y) * self.lookspeed		
		--self.camerarotation.x = self.camerarotation.x + dy
		if mhit then
			self.camerarotation.y = self.camerarotation.y + dx
		end
		self.mousepos = mpos
	else
		--self.mousepos = Vec3(centerpos.x,centerpos.y,0)
		self.started=true
	end
end

function Script:UpdatePhysics()
	local move=0
	local strafe=0
	local ascension=0
	local window = Window:GetCurrent()
	local mpos = window:GetMousePosition()
	local sx = context:GetWidth()
	local sy = context:GetHeight()
	

	if mpos.y < self.MousBoarder then move = move + self.movespeed end
	if mpos.y > sy - self.MousBoarder then move = move - self.movespeed end
	if mpos.x < self.MousBoarder then strafe = strafe - self.movespeed end
	if mpos.x > sx - self.MousBoarder then strafe = strafe + self.movespeed end
	if mpos.z < 0 then ascension = ascension - self.movespeed/2 end
	if mpos.z > 0 then ascension = ascension + self.movespeed/2 end

	local currentvelocity = self.entity:GetVelocity(false)
	local desiredvelocity = Vec3(strafe,0,0) + Transform:Vector(0,ascension,move,nil,self.entity)
	local velocity = currentvelocity * self.movementsmoothing + desiredvelocity * (1.0-self.movementsmoothing)
	self.entity:AddForce((desiredvelocity - currentvelocity) * self.entity:GetMass() / self.movementsmoothing,false)
	self.entity:PhysicsSetRotation(self.camerarotation.x,self.camerarotation.y,self.camerarotation.z)

	
end

 

  • Like 1
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...