Jump to content

[SOLVED]Rotate the camera around the object


swipis
 Share

Recommended Posts

heya!

I trying to make a camera rotating around the player.

if window:MouseDown(Key.RButton) then
window:HideMouse()

self.currentMousePos = window:GetMousePosition()
self.currentMousePos.x = Math:Round(self.currentMousePos.x)
self.currentMousePos.y = Math:Round(self.currentMousePos.y)

self.mouseDifference.x = Math:Curve(self.currentMousePos.x - Math:Round(context:GetWidth()/2),self.mouseDifference.x,3)
self.mouseDifference.y = Math:Curve(self.currentMousePos.y - Math:Round(context:GetHeight()/2),self.mouseDifference.y,3)
self.camRotation.x = Math:Clamp(self.camRotation.x + self.mouseDifference.y / self.mouseSensitivity,-90,90)
self.camRotation.y = self.camRotation.y + (self.mouseDifference.x / self.mouseSensitivity)

--Set the camera angle
self.entity:SetRotation(self.camRotation + self.pivot:GetPosition())

else
window:ShowMouse()
end

this code taken from FPS script. what I should do to rotate around player?

thanks

Link to comment
Share on other sites

My Leadwerks isn't working at the moment, so I can't test any code. I can only give you pseudo code.

 

You basically want to make the camera work like in the FPS example, but then move its position backwards in local space.

Basically, what you already have there, but add something like:

 

self.entity:SetPosition(self.pivot:GetPosition() + Vec3(0, 0, -10))

 

I'm assuming pivot is where the player is?

Link to comment
Share on other sites

Here is a small example of an orbital camera around an object like in the video you posted:

window = Window:Create("Orbital Camera",0,0,800,600,Window.Titlebar+Window.Center)
context = Context:Create(window)
world = World:Create()
camera = Camera:Create()
camera:SetSkybox("Materials/Sky/skybox_texture.tex")
light = DirectionalLight:Create()
light:SetRotation(35,35,0)

player = Model:Load("Models/Characters/generic/generic.mdl")
player:SetColor(0,1,0,1)
pivot = Pivot:Create()
pivot:SetPosition(0,0.8,0)
camera:SetParent(pivot)
camera:Move(0,1,-3)

mx = 0
my = 0
prevmx = 0
prevmy = 0
toggle = 0

while window:KeyDown(Key.Escape)==false do
	if window:Closed() then break end
	if window:MouseHit(Key.RButton) then toggle = 1 end
	if window:MouseDown(Key.RButton) then
		mousepos = window:GetMousePosition()
		mx = pivot:GetRotation().x + (mousepos.y-prevmy)
		my = pivot:GetRotation().y + (mousepos.x-prevmx)
		if toggle==1 then
			toggle = 0
			mx = pivot:GetRotation().x
			my = pivot:GetRotation().y
		end
		pivot:SetRotation(mx,my,pivot:GetRotation().z)
		prevmx = mousepos.x
		prevmy = mousepos.y
	end

	Time:Update()
	world:Update()
	world:Render()
	context:SetBlendMode(Blend.Alpha)
	context:DrawText(string.format("FPS: %.1f",Time:UPS()), 2, 2)
	context:DrawText("Hold Right Mouse Button Down to Rotate", 2, 22)
	context:SetBlendMode(Blend.Solid)
	context:Sync(true)
end
  • Like 1
  • 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...