Jump to content

3rd person camera :rotate an model part to mousepos in Lua


holschuh
 Share

Recommended Posts

Hi,

 

is there an easy way to get the rotation from an Entity to the mousecursor?

(like an Tank cannon)

 

setting the rotation is with getchild + setrotation easy but how do i get an value for the rotation to the mouse with an 3rd person camera?

 

the yellow sphere is the mousecursor in picture...

 

post-13671-0-00585100-1429686544.jpg

 

cu

Volker

Link to comment
Share on other sites

hi beo6,

 

i think Window::GetMousePosition dont give an vec3

wich is usable in Entity::AlignToVector

z is the mouse wheel and x y are the 3rd person camera view 2d coordinates - these must translated to the map-ground 3d coordinates i think but i dont know how...

 

the other functions need an entity as target - i have only the mousecursor as target of the rotation.

 

 

cu

Volker

Link to comment
Share on other sites

What is your goal here? What is the final result you are after? In 3rd person generally you would each frame move the camera to the location of the thing you are rotating around. Then rotate the camera here (if you allow free rotation use the mouse x/y or if you want it to match the thing you are rotating just get the y rotation of the thing you are following). Then use Move(0, 0, -dist) to move the camera backward by whatever distance you want.

 

If doing this in Lua via entity scripts then make a script variable for the thing you are following to accept the camera entity and make the camera entity take a script variable to the entity it should be following. Now you can get anything between the 2 entities you want.

Link to comment
Share on other sites

hi rick,

 

the goal is:

the camera moves over the ground (with an angle) having the player as parent and following him ...

 

the player is an tank moving around with "wasd" or something.

the tank has an gun wich faces always the mousecursor position, left click firing and s.o...

 

cu

Volker

Link to comment
Share on other sites

the camera moves over the ground (with an angle) having the player as parent and following him ...

 

So the camera follows the tank and the camera follows it. Can you rotate this camera with the mouse like more 3rd person games or is the camera always at the tanks back and rotates when the tank rotates OR rotates with the gun on top of the tank?

 

the tank has an gun wich faces always the mousecursor position, left click firing and s.o...

 

So with this are you saying you want the 3D gun on top of the tank to rotate around the Y axis based on where the 2D mouse is around the gun?

Link to comment
Share on other sites

So the camera follows the tank and the camera follows it. Can you rotate this camera with the mouse like more 3rd person games or is the camera always at the tanks back and rotates when the tank rotates OR rotates with the gun on top of the tank?

 

 

 

So with this are you saying you want the 3D gun on top of the tank to rotate around the Y axis based on where the 2D mouse is around the gun?

 

yes

camera rotation always same direction - not rotating to be behind the tank if the tank rotate.

and yes if i move the mouse in the upper right corner of themonitor the gun should target this position..

 

 

at the moment i modified some lines from the fps character controller but so the turret rotates not precisely and only moving mouse left ... right of the screen ignorring top--down

 

maybe setrotation is the false decision for this problem

 

post-13671-0-92065400-1429735990_thumb.png

 

function Script:Start()
self.currentyrotation = self.entity:GetRotation().y
end



function Script:UpdateWorld()

local rot = self.entity:GetRotation(true)
local window = Window:GetCurrent()
local gy = window:GetWidth()/2
local mouseposition = window:GetMousePosition()
local child=self.entity:FindChild("Head")

local dx = mouseposition.x-gy
rot.y = rot.y + dx / 5 --look left and right
child:SetRotation(0,rot.y,0,true)
end



function Script:UpdatePhysics()

local window = Window:GetCurrent()
--Detect Key hits
local move = ((window:KeyDown(Key.S) and 1 or 0) - (window:KeyDown(Key.W) and 1 or 0))
local yrotation = ((window:KeyDown(Key.D)and 1 or 0) - (window:KeyDown(Key.A) and 1 or 0))

--Increment y rotation
self.currentyrotation = self.currentyrotation + yrotation

self.entity:SetInput(self.currentyrotation,move,0)

end

 

cu

Volker

Link to comment
Share on other sites

I think what beo6 might have been suggestion is that you do a mouse pick. That'll return a point on the 3D floor where your mouse is over. Now you have a Vec3 location. Now you can use some function like Point() to point your turret to that around the Y axis. Now this won't work if you can see empty space like in your screenshot because picking won't happen there but it will on the floor. So if your camera is angled in such a way (more top/down) where you never see blank space this method will work.

Link to comment
Share on other sites

  • 2 weeks later...

HI,

 

Is there a Trick to get Point and aligntovector to work with an "child" Object of an model?

point seems to invert the rotation and alighnto seems inverse but not totally the pickpos

With the whole model all works fine but not with an child...

 

I think about to make an hidden dummy entity to steal rotationfor the child of the Main Model but i hope there is an other way feeling more right?

 

self.entity:Point(self.Psphere,2,Time:GetSpeed()*0.1)

--self.entity:AlignToVector(self.Psphere.position,2)

--child:Point(self.Psphere,2,Time:GetSpeed()*0.1)

--child:AlignToVector(self.Psphere.position,2)

 

post-13671-0-73204400-1430666556_thumb.pngpost-13671-0-34385700-1430666847_thumb.png

 

Script.cam = nil--Entity "Target"
Script.Speed=1.0--float
Script.Sequence=0--int
function Script:Draw()
--local t = Time:GetCurrent()
--self.entity:SetAnimationFrame(t/100.0*self.Speed,1,self.Sequence)
end
function Script:Start()
self.currentyrotation = self.entity:GetRotation().y
self.Psphere = Model:Sphere()
self.Psphere:SetScale(1.04, 1.04, 1.04);
self.Psphere:SetCollisionType(0);
self.Psphere:SetColor(1.0,0.0,0.0)
self.Psphere:SetPickMode(0)
end

function Script:UpdateWorld()
local window = Window:GetCurrent()
local mouseposition = window:GetMousePosition()
local child=self.entity:FindChild("Head")
local child2=self.entity:FindChild("Gun")
local pickinfo = PickInfo()

--local t = Time:GetCurrent()
--self.entity:SetAnimationFrame(t/100.0*self.Speed,1,self.Sequence)
if (self.cam:Pick(mouseposition.x,mouseposition.y,pickinfo,0.1,true)) then
if window:MouseHit(1)==true then
 self.Psphere:SetPosition(pickinfo.position)
 end
end

self.entity:Point(self.Psphere,2,Time:GetSpeed()*0.1)
--self.entity:AlignToVector(self.Psphere.position,2)
--child:Point(self.Psphere,2,Time:GetSpeed()*0.1)
--child:AlignToVector(self.Psphere.position,2)

end
function Script:UpdatePhysics()

local window = Window:GetCurrent()
--Detect Key hits
local move = ((window:KeyDown(Key.W) and 1 or 0) - (window:KeyDown(Key.S) and 1 or 0))
 local yrotation = ((window:KeyDown(Key.D)and 1 or 0) - (window:KeyDown(Key.A) and 1 or 0))
--Increment y rotation
 self.currentyrotation = self.currentyrotation + yrotation
self.entity:SetInput(self.currentyrotation,move*8,0)
end

 

cu

Volker

Link to comment
Share on other sites

Playing around with this, there were a couple of modifications I made to the model itself. I scaled the model in a modeling app to 1.5 meters tall so it wasn't so large and I rotated the model so it's initial rotation is facing the +Z direction. Then inside the LE model editor, I extracted an 'idle' and 'walk' animations from the existing animation.

 

Here is the modified model:

testturret.zip

 

If I understood correctly what you were trying to accomplish, I made the turret rotate to face a picked point while the actual character movement and rotation of the "legs" were controlled and animated by the 'WASD' keys.

 

Here is the example script to attach to the above model:

function Script:Start()

local window = Window:GetCurrent()

window:ShowMouse()

self.legs = self.entity:FindChild("Armature")

self.turret = self.entity:FindChild("Hip")

self.entitypos = self.entity:GetPosition(true)

self.cam = Camera:Create(self.entity)

self.cam:SetPosition(self.entitypos.x, self.entitypos.y +3, self.entitypos.z-4, true)

self.cam:SetRotation(5,0,0)

self.move = 0

self.rotate = 0

self.sphere = Model:Sphere(16)

self.sphere:SetColor(1,0,0,1)

self.sphere:SetScale(.1,.1,.1)

self.sphere:SetPickMode(0)

self.sphere:Hide()

self.pick = nil

self.pickinfo = PickInfo()

end

 

function PerfectAngle(pos1,pos2)

local dx, dz

dx = pos1.x - pos2.x

dz = pos1.z - pos2.z

return Math:ATan2(dx,dz)

end

 

function Script:UpdateWorld()

local window = Window:GetCurrent()

if window:MouseHit(1)==true then

local mousepos = window:GetMousePosition()

self.pick = self.cam:Pick(mousepos.x,mousepos.y,self.pickinfo,0,true,0)

end

if self.pick then

self.sphere:SetPosition(self.pickinfo.position,true)

self.sphere:Show()

local perfectangle = PerfectAngle(self.entity:GetPosition(true),self.pickinfo.position)

local currentangle = self.turret:GetRotation(true).y

self.angle=Math:IncAngle(perfectangle,currentangle,5/Time:GetSpeed())

self.turret:SetRotation(0,self.angle,0,true)

end

end

 

function Script:Draw()

if math.abs(self.move)>0.1 then

local t = Time:GetCurrent()

self.legs:SetAnimationFrame(t/10,1.0,"walk",true)

else

self.legs:SetAnimationFrame(0,1.0,"idle",true)

end

end

 

function Script:UpdatePhysics()

local window = Window:GetCurrent()

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

self.rotate = self.rotate + ((window:KeyDown(Key.D) and 1 or 0) - (window:KeyDown(Key.A) and 1 or 0))

self.entity:SetInput(self.rotate,self.move*3,0,0,false,10)

end

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

Rereading the posts above, I may have originally misunderstood the turret/mouse interaction. If you want the turret to rotate from a constant camera pick based on mouse pointer position and then just place the sphere when a successful pick is performed when left clicking the mouse, then replace the above 'Script:UpdateWorld()' function with the following code:

 

function Script:UpdateWorld()

local window = Window:GetCurrent()

local mousepos = window:GetMousePosition()

self.pick = self.cam:Pick(mousepos.x,mousepos.y,self.pickinfo,0,true,0)

if self.pick then

if window:MouseHit(1)==true then

self.sphere:SetPosition(self.pickinfo.position,true)

self.sphere:Show()

end

local perfectangle = PerfectAngle(self.entity:GetPosition(true),self.pickinfo.position)

local currentangle = self.turret:GetRotation(true).y

self.angle=Math:IncAngle(perfectangle,currentangle,5/Time:GetSpeed())

self.turret:SetRotation(0,self.angle,0,true)

end

end

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

Hi macklebee,

 

ohah you use "math" i can copy paste this but not understand in detail now ohmy.png

 

the sphere was for debugging and because "point" with no target-entitiy dont work -i think.

But you are right there are 2 sub objects from "hip" - "head" and sub of the head "Gun". Head should rotate to the mousepos but dont roll or kipple and the gun schould rotate up-down to exact heigt of mousepos. head rotate around y and gun top down to exact mouse positon in 3d space. - like an tank in reallife ;)

 

overall i think i can also try making 3 models and do parenting in Leadwerks not in modeller..maybe that works for me.

I saw an unity vid doing an tank and working with the submodel origins pos for rotation this is my try to make same in LE...

 

thanks now i have some new ideas

 

cu

volker

Link to comment
Share on other sites

Hi,

 

today i had some time to look again over this..found the problem in the new beta you can go down into model...it is new or i havent seen bevore.

the orientation of the head and gun flipped against the whole model.

 

post-13671-0-18306700-1430941097_thumb.png

 

maybe helps someone also wink.png

cu

volker

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