Adam's Lair Forum

game development and casual madness
It is currently 2018/08/16, 07:53

All times are UTC + 1 hour [ DST ]




Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: 2017/07/11, 22:23 
Novice Member
Novice Member
User avatar

Joined: 2017/06/22, 02:43
Posts: 12
Location: Canada
Role: Hobbyist
Hello,

I made this code that rotates a GameObject around a center point based off of the potion of the mouse by getting the angle difference from the mouse and the object and moving the object by that amount, making the angle difference 0. But instead of that the object just rotates around the center point endlessly.

Code:
// Get the player's location on the viewport from the camera
Camera camera = Scene.Current.FindComponent<Camera>();
Vector2 playerScreenPos = (camera != null) ? camera.GetScreenCoord(GameObj.Transform.Pos).Xy : Vector2.Zero;

// Calculate the angle that the player is looking at
// Using the pos of the mouse and the player on the screen
float playerLookAngle = (DualityApp.Mouse.Pos - playerScreenPos).Angle;
           
// Rotate the player's arm based off of the angle of the mouse
weaponObj.Transform.TurnToAbs(playerLookAngle - 1.5f);

Vector2 mouseSpacePos = camera.GetSpaceCoord(DualityApp.Mouse.Pos).Xy;
Vector3 weaponPos = weaponObj.Transform.Pos;
float angleFromMouse = (weaponPos.Xy - mouseSpacePos).Angle;

MathF.TransformCoord(ref weaponPos.X, ref weaponPos.Y, angleFromMouse, 1, GameObj.Transform.Pos.X, GameObj.Transform.Pos.Y);

weaponObj.Transform.MoveToAbs(weaponPos);

_________________
Gregory Karastergios
http://gregnk.com


Top
 Profile  
 
PostPosted: 2017/07/12, 21:43 
Site Admin
Site Admin
User avatar

Joined: 2013/05/11, 22:30
Posts: 2055
Location: Germany
Role: Professional
I don't see an obvious error on what I think are the relevant lines (the first few determine the rotation and apply it, right?), but it's a bit hard to say what's going on without more context. Can you isolate the problem and pin it down to something that is less dependent on project specifics?

Edit: If you're unsure how to further debug this, maybe add a VisualLog text on screen to output realtime info on values? Alternatively, do the same with a regular log and observe how the values change while you're moving the mouse cursor. Live info can help a lot in these cases!

_________________
Blog | GitHub | Twitter (@Adams_Lair)


Top
 Profile  
 
PostPosted: 2017/07/12, 23:12 
Novice Member
Novice Member
User avatar

Joined: 2017/06/22, 02:43
Posts: 12
Location: Canada
Role: Hobbyist
I fixed it. The issue was with how angleFromMouse was calculated. angleFromMouse was calculating what direction the mouse was facing rather than the difference between mouseSpacePos and weaponPos.

So The right code would be:
Code:
float angleFromMouse = (mouseSpacePos - GameObj.Transform.Pos.Xy).Angle - (weaponPos.Xy - GameObj.Transform.Pos.Xy).Angle


Also I was able to check the values in realtime by declaring them in the global scope and enabling debug mode in the object inspector. It was really helpful! :+1:

_________________
Gregory Karastergios
http://gregnk.com


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC + 1 hour [ DST ]


Who is online

Users browsing this forum: No registered users and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group