0

I have a "small" problem with my code that convert the mouse position to radiance.

I need the mouse position to determine where the head of my robot must point to. The code works and head turns ... but! It reaches max of pitch too quickly.

As I am not exactly super sharp in radiance calculation or C#, I have to resort to expert help. :)

My code:

maxcursory = Screen.PrimaryScreen.Bounds.Height;

cursory = Cursor.Position.Y;

pitch = (float) (((3 / maxcursory * cursory) - 1.5) * 1);

The min and max values I need: -0.6720 to 0.5149

Edit: Fixed code display error. ;)

Edit 2: Added min and max values

bmm6o
  • 6,187
  • 3
  • 28
  • 55
Chris Jensen
  • 89
  • 1
  • 6
  • 1
    that's a not compiling code sir @tnw – No Idea For Name Aug 26 '13 at 13:03
  • Err, I don't know how that happened, but yes. It's fixed in the question now. :) – Chris Jensen Aug 26 '13 at 13:10
  • @NoIdeaForName Yes that's why I pointed it out hahah – tnw Aug 26 '13 at 13:13
  • I think you need to clarify your question. How must the robot's head move when a user clicks somewhere? Is the robot's head displayed on the screen and must it turn towards the click? If so, what is the location of the robot's head? – Roy Dictus Aug 26 '13 at 13:13
  • The head is moved using a function from the framwork, called angleInterpolation. No clicking is involved here(where did you get that idea?) No onscreen robothead, it's a real robot standing on my desk. ;) There really isn't any another way to move the head, and as I said, it works. :) Only the pitch angel is a bit off. – Chris Jensen Aug 26 '13 at 13:18
  • `Screen.PrimaryScreen.Bounds.Height` and `Cursor.Position.Y` are both integers. Unless `maxcursory` is declared as a float or your screen is only a few pixels tall, `(float) (((3 / maxcursory * cursory) - 1.5) * 1)` is going to evaluate to `-1.5` no matter the cursor position. Also, what is the expected units of pitch (degrees, pixels, radians)? – SLuck49 Aug 26 '13 at 17:01
  • Well, as I've said more once now: the code works!! :) So yes maxcursory IS a float, and so is pitch. The expected units are(as the title of the question says) radians. :) – Chris Jensen Aug 26 '13 at 17:40

2 Answers2

1

So you want to pick a value within your min and max based on the ratio of cursor y position to max screen height?

The current calculation for pitch gives the range from -1.5 to 1.5.

For a range between -.6720 and .5149 use:

1.1869 * cursory / maxcursory -.672

Or, generalized:

(rangeEnd-rangeStart)*(cursory / maxcursory) + rangeStart

Edit: I assumed you knew this but just in case, Cursor.Position.Y treats the top of the screen as 0 and the bottom as the same as the height. So if you wanted the top of the screen to have the value of .5149 and the bottom to be -.672 your range start and end would be .5149 and -.672 respectively.

-1.1869 * cursory / maxcursory + .5149
SLuck49
  • 353
  • 1
  • 8
0

It seems you have your * sign in the wrong position

pitch = (float) (((3 / maxcursory cursory *) - 1.5) * 1);

try:

pitch = (float) (((3 / maxcursory * cursory ) - 1.5) * 1);
Paddyd
  • 1,870
  • 16
  • 26