YouGroove Posted February 5, 2014 Share Posted February 5, 2014 If i move mouse direction, the character rotates to that direction, and move to that direction if i have the "run" key pressed. The mouse direction is taken on Script:UpdateWorld to rotate character to the direction. How to get the angle of the mouse cursor to pass it to SetInput ? (This is not my code , but from codeApe old physic problem ) function Script:UpdateWorld() local window = Window:GetCurrent() -- Mouse pointer handling self.screenCord = window:GetMousePosition() -- Set height over floor to aim self.screenCord.z = self.cam:GetPosition(true).y - self.entity:GetPosition(true).y -- Convert mouse screen space coord to 3d space coord local Vec3 worldCord = self.cam:UnProject(self.screenCord) self.hairPiv:SetPosition(worldCord, false) -- Make the player look at the mouse pointer self.entity:Point(self.hairPiv, 2) end And update pysics where i can't figure out , how to calculate cursor angle for SetInput call ? function Script:UpdatePhysics() local window = Window:GetCurrent() local zMod = 0 if window:KeyDown(Key.Z) then zMod = 1 * Time:GetSpeed() * self.moveSpeed end local pos = self.entity:GetPosition() local dir = Vec2(pos.z,pos.x):Normalize() local angle = -Math:ATan2(dir.y,-dir.x) self.screenCord.x = xMod self.entity:SetInput(angle,zMod) If anyone oculd help me on that ? 1 Quote Stop toying and make games Link to comment Share on other sites More sharing options...
Guppy Posted February 5, 2014 Share Posted February 5, 2014 arccos(a dot b / |a||b| ) Quote System: Linux Mint 17 ( = Ubuntu 14.04 with cinnamon desktop ) Ubuntu 14.04, AMD HD 6850, i5 2500k Link to comment Share on other sites More sharing options...
DudeAwesome Posted February 5, 2014 Share Posted February 5, 2014 I did something similar for a 3rd person game. dont have the code here now but it works fine: 1.get 3D point from player (Point A) 2. get window 2D position from mouse 3. Pick with 2D position from mouse 4. pickinfo.position (Point B ) 5. now you can easily calculate the angle between A and B with dont sure but it was arctan((y2-y1)/(x2-x1)) (there is also a cosinus way but i dont know them right now) i guess to get the angle you also can do it with arctan2 but I believe atan2 gives you radians not degrees 6. I used http://www.leadwerks.com/werkspace/page/documentation/_/command-reference/math/mathcurveangle-r603 to get a smooth moving between different angles (turning 180 degrees would be superfast and I want a small "lag" to turn) Quote It doesn´t work... why? mhmmm It works... why? Link to comment Share on other sites More sharing options...
YouGroove Posted February 5, 2014 Author Share Posted February 5, 2014 5. now you can easily calculate the angle between A and B with dont sure but it was arctan((y2-y1)/(x2-x1)) (there is also a cosinus way but i dont know them right now) i guess to get the angle you also can do it with arctan2 but I believe atan2 gives you radians not degrees Thanks, but don't understand that. Yes i have mouse Coords and Player 3D position : the angle player have in X,Z to mouse Coords , how to get that ? How to do with that objects below ? Enity.Player:GetPosition() MouseX MouseY I just ask the lua code and math retrieving the angle if someone know how to. Quote Stop toying and make games Link to comment Share on other sites More sharing options...
DudeAwesome Posted February 5, 2014 Share Posted February 5, 2014 I will post my solution later when I´m home to show you how I did it (I also had some trouble with the angle function but it works for me now) Quote It doesn´t work... why? mhmmm It works... why? Link to comment Share on other sites More sharing options...
YouGroove Posted February 5, 2014 Author Share Posted February 5, 2014 Ok working now. The working example (for WIKI or tutorials section) function Script:Start() --Player movement speed self.moveSpeed = 0.3 -- Camera distance from player self.camDist = 5 -- Create camera and position it local camPos = self.entity:GetPosition(true) self.cam = Camera:Create() camPos.y = self.camDist self.cam:Move(camPos) self.cam:SetRotation(90,0,0) -- Crosshair resources self.cross = Texture:Load("Materials/HUD/crosshair.tex") self.hairPiv = Pivot:Create(); self.screenCord = Vec3(0, 0, 0) self.entity:SetFriction(0,0) end function Script:UpdateWorld() local window = Window:GetCurrent() -- Mouse pointer handling self.screenCord = window:GetMousePosition() -- Set height over floor to aim self.screenCord.z = self.cam:GetPosition(true).y - self.entity:GetPosition(true).y -- Convert mouse screen space coord to 3d space coord local Vec3 worldCord = self.cam:UnProject(self.screenCord) self.hairPiv:SetPosition(worldCord, false) -- Make the player look at the mouse pointer self.entity:Point(self.hairPiv, 2) end function Script:UpdatePhysics() local window = Window:GetCurrent() -- Player keyboard input local xMod = 0; if window:KeyDown(Key.D) then xMod = 1 * Time:GetSpeed() * self.moveSpeed elseif window:KeyDown(Key.Q) then xMod = -1 * Time:GetSpeed() * self.moveSpeed end local zMod = 0 if window:KeyDown(Key.Z) then zMod = 1 * Time:GetSpeed() * self.moveSpeed elseif window:KeyDown(Key.S) then zMod = -1 * Time:GetSpeed() * self.moveSpeed end local targetpos = worldCord local posPlayer = self.entity:GetPosition() local dir = Vec2(targetpos.z-posPlayer.z,targetpos.x-posPlayer.x):Normalize() local angle = -Math:ATan2(dir.y,-dir.x) self.entity:SetInput(angle,zMod) end Quote Stop toying and make games Link to comment Share on other sites More sharing options...
DudeAwesome Posted February 5, 2014 Share Posted February 5, 2014 nice example. I implemented this way for prototyping. you can also use IncAngle() if you dont like that the player follows the mouse that fast. so it take some msec to turn 180° self.camera:Pick(App.window:GetMousePosition().x, App.window:GetMousePosition().y, self.mouseInfo, 0.0, true) --angle to mouse/joystick if self.mouseInfo.position.z - self.playerPosition.z >= 0 then self.mouseAngle = Math:ATan((self.mouseInfo.position.x - self.playerPosition.x)/(self.mouseInfo.position.z - self.playerPosition.z)) end if self.mouseInfo.position.z - self.playerPosition.z < 0 then self.mouseAngle = Math:ATan((self.mouseInfo.position.x - self.playerPosition.x)/(self.mouseInfo.position.z - self.playerPosition.z))+180 end self.playerAngle = Math:IncAngle(self.mouseAngle, self.playerAngle, 20.0) self.entity:SetRotation(0,self.playerAngle,0) --later use SetInput Quote It doesn´t work... why? mhmmm It works... why? Link to comment Share on other sites More sharing options...
YouGroove Posted February 5, 2014 Author Share Posted February 5, 2014 I don't understand a lot your code. Well in my example you have only one Athan call, i think your and mine code do same thing. Quote Stop toying and make games Link to comment Share on other sites More sharing options...
Recommended Posts
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.