Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Problem (perhaps bug) with mapPixelToCoords(RenderWindow x) - results in NaN  (Read 16098 times)

0 Members and 1 Guest are viewing this topic.

enigma22134

  • Newbie
  • *
  • Posts: 13
    • View Profile
When I make a call to mapPixelToCoords(), it functions as needed in about 70% of screen.

Here's what I am trying to do:

Use the mouse position to rotate a sprite so that it faces the mouse. I've got this to work with 100% success using the c++ sfml.

Here's my problem:

In certain positions, the function returns a NaN for the y component(float2f). I have narrowed the problem down to this function.

Here's the method's code

   public void rotateToMouse(RenderWindow window){
      mousePosWin = Mouse.getPosition(window);
      mousePos = window.mapPixelToCoords(mousePosWin);
      System.out.println("x: "+ mousePosWin.x + " y: " + mousePosWin.y + " relX: "+ mousePos.x + " relY " + mousePos.y);;
      
      actorPos = actorSprite.getPosition();
      relativeActToMouse = new Vector2f(mousePos.x - actorPos.x, mousePos.y -actorPos.y);
      playerRotation = Math.atan((double)relativeActToMouse.x/relativeActToMouse.y)*(180/3.141);
      actorSprite.setRotation((float)playerRotation);
   }
actorSprite is an instance of jsfml sprite class.

Here's an the console output when the problem is occuring:
x: 363 y: 352 relX: -37.0 relY NaN
x: 363 y: 352 relX: -37.0 relY NaN
x: 359 y: 352 relX: -41.0 relY NaN
x: 356 y: 352 relX: -44.0 relY NaN
x: 356 y: 352 relX: -44.0 relY NaN

x and y are the mouse's pixel position (i.e. where it is on the computer screen)
relX and relY are the x and y (float2f) for the mouse AFTER I have called mapPixelToCoords(RenderWindow window)[/glow]

As you can see, for some reason this results in NaN. I have no idea why. it does not always do this either.

pdinklag

  • Sr. Member
  • ****
  • Posts: 330
  • JSFML Developer
    • View Profile
    • JSFML Website
Hm, if the SFML functions works (couldn't find any matching SFML bugs from the past), it can only be a problem with the Intercom encoding and decoding of vectors between JSFML and SFML, because no calculation is done in the Java code.

However, I fail to spot any mistakes there - and any mistake here would affect several other features. :-\

Could you specify "in certain positions"? Is there any pattern to it (e.g. the right portion of the screen, or stuff like that)?
JSFML - The Java binding to SFML.

enigma22134

  • Newbie
  • *
  • Posts: 13
    • View Profile
There is a pattern I've identified. It returns NaN when the mouse is to the left of the sprite (position 0,0). That is, when the returned x falls below 0 (negative), it returns NaN for the Y value (suprisingly, not the x value). Here's my console output (code is in previous message).

Below results from dragging the mouse from the right to the left.

x: 560 y: 125 relX: 160.0  relY -175.0
x: 504 y: 125 relX: 104.0  relY -175.0
x: 455 y: 122 relX: 55.0    relY -177.99998
x: 413 y: 119 relX: 13.0    relY -181.0
x: 413 y: 119 relX: 13.0    relY -181.0
x: 354 y: 112 relX: -46.0   relY NaN
x: 259 y: 101 relX: -141.0 relY NaN
x: 193 y: 91   relX: -207.0 relY NaN
x: 193 y: 91   relX: -207.0 relY NaN
x: 123 y: 84   relX: -277.0 relY NaN

relX and relY are the values returned by the mapPixelToCoords

The function seems to be limited only to x coordinates above 0; i.e. when the method returns negative x values, it corrupts the returned y value and y become NaN. restated:it cannot return number y values when returned x is 0 or lower.

This happens when the mouse is in the left portion of the screen.However, If I move the sprite(and the view) to the right far enough, I can get the function to work flawlessly; it works because all x values are positive. It as if there is an imaginary boundary for anything less than the x-value of  0.

For a bit, I thought this might be due using the View class and not passing it in as a parameter (for an overloaded version of the mapPixelToCoords that accepts (RenderWindow, const View). But I disabled my view and it did not fix the problem. I also tried passing in my view object using the overloaded function and could not get it to work.

edit:
Here's a video of what is happening (had to use my phone on this computer, sorry).
https://www.youtube.com/watch?v=rhUNHfDgMNI&feature=youtu.be
I have the sprite being rotated using the value returned from mapPixelToCoords. When it is NaN the sprite disappears (I guess because the rotation value is not possible).
Thanks so much for your help!
« Last Edit: December 26, 2015, 02:49:09 am by enigma22134 »

pdinklag

  • Sr. Member
  • ****
  • Posts: 330
  • JSFML Developer
    • View Profile
    • JSFML Website
Your observations speak for a bug in the vector encoding / decoding.

Instead of passing objects between the C++ and Java parts (which is pretty slow), I'm compressing 2D vectors into 64-bit integers - the high 32 bits representing the Y coordinate and the low bits representing X. I call this the "Intercom". I suppose that something goes wrong here for certain value ranges, most likely sign bits being confused or getting lost on either the Java or C++ side.

The big problem is that I have no development environment set up for JSFML or even Java in general at the moment, and I'm rather busy preparing a music live set for a new year's party I'm booked at. So I can't do any testing and fixing right about now. :-\ I hope it's understandable.
JSFML - The Java binding to SFML.

enigma22134

  • Newbie
  • *
  • Posts: 13
    • View Profile
That's definitely understandable. I'm not really familiar with the details with using c++ and java together, so I cannot pretend to know the complexities of what you're describing. Do you think sometime after new years you will be able to investigate the bug? And perhaps create a fix? Hopefully it is isn't too large of an issue?

I know you're probably extremely busy with life. My university uses java as its main teaching language, hence the reason I switched from using c++ to using this library in java. Let me know if you think this is something that might be fixed in the somewhat near future. :)

pdinklag

  • Sr. Member
  • ****
  • Posts: 330
  • JSFML Developer
    • View Profile
    • JSFML Website
Do you think sometime after new years you will be able to investigate the bug? And perhaps create a fix? Hopefully it is isn't too large of an issue?
Yes, I should have. And no, whatever it is, it should be easy to fix once I figured exactly where the problem lies. Sorry I can't help right about now.

Also, are you on Windows or Linux?
I may be able to create a fix for Windows very quickly once I got my dev stuff set up again, but for Linux I'll need an entirely new development system, that could take a little longer.
JSFML - The Java binding to SFML.

enigma22134

  • Newbie
  • *
  • Posts: 13
    • View Profile
I'm currently using windows with eclipse. Though, I am hoping that I can have my project run on all platforms. Setting up a new linux system is on my todo list for this break. :)

Thanks again. :)