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

Author Topic: New to SFML, need help with World to Screen  (Read 3703 times)

0 Members and 1 Guest are viewing this topic.

chugginwindex

  • Newbie
  • *
  • Posts: 4
    • View Profile
New to SFML, need help with World to Screen
« on: November 23, 2010, 07:01:51 pm »
Hey all,

I'm working on a orbital sim that lets you place stellar bodies with starting trajectories and have them be influenced realistically by gravity. I've got the basics done, but I'd like to have special overlays in the simulator for different effects such as tracing the orbits of the stars and showing the influence of gravity on the "fabric" of space. Anyways for these effects I really need to be able to convert my mouse / screen position(s) to real world coordinates so that I can do certain math with them such as seeing how strong the influence of gravity on a specific point is. the RenderTarget's ConvertCoords() function has already been a life saver, but now I'm in need of the opposite and haven't been able to find one. I saw a thread here a while back where someone said that the general consensus was that such a function was not necessary and therefore had very low priority for implementation...well I think I need it, and I'm wondering if anyone here may be able to help me figure one out. I tried taking a peek at the underlying code for ConvertCoords and just reversing the process (I remembered to multiple by the matrix of the view instead of the inverse matrix), but my values still got all out of wack when I tried to zoom in and out.

Has anyone else done something similar to this? I can't seem to figure it out for the life of me.

Oh and if it's relevant I'm building with SFML 2.0. Thanks in advance!

-Mike

OniLinkPlus

  • Hero Member
  • *****
  • Posts: 500
    • View Profile
New to SFML, need help with World to Screen
« Reply #1 on: November 24, 2010, 01:01:19 am »
ConvertCoords actually takes the coordinates of the mouse relative to the window and changes them into world coordinates, so you don't have to reverse it.
I use the latest build of SFML2

chugginwindex

  • Newbie
  • *
  • Posts: 4
    • View Profile
New to SFML, need help with World to Screen
« Reply #2 on: November 24, 2010, 01:07:51 am »
No I know that. What I'm looking for is a way that I can take the world coordinates of a star in my orbital simulation, and convert that to the screen coordinates given the current view transformations so that I can work with it to draw extra information such as "trails" behind the orbiting bodies.

Sorry if it didn't make sense the first time, hope this clears it up.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
New to SFML, need help with World to Screen
« Reply #3 on: November 24, 2010, 08:26:37 am »
Why do you want to draw in window coordinates? Can't you draw directly in your view coordinates, if you want your text to be aligned on world objects?
Laurent Gomila - SFML developer

chugginwindex

  • Newbie
  • *
  • Posts: 4
    • View Profile
New to SFML, need help with World to Screen
« Reply #4 on: November 24, 2010, 04:08:21 pm »
Let me give you an example: When I click the mouse in my app I spawn a star, but if you click and hold, the star isn't actually spawned yet and you can use the mouse to set up a line that specifies the star's initial velocity/trajectory. Once you let go the star is spawned. Right now I'm doing what you suggest, I'm drawing the trajectory line from the real world coordinates of the star's spawn location to the real world coordinates of the end of the line (how far the star will travel in one second). When you zoom in/out, this line either becomes much thicker than it was before, or very jagged and sometimes missing in parts as the distance increases between the viewer and the line. What I'd like to do is have the line only be drawn "screen-deep" and therefore always have the same thickness and clarity, but for that I need to be able to take the real world start and end coordinates of the star's trajectory and convert them into screen coordinates in order to draw the line outside of the influence of my current view.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
New to SFML, need help with World to Screen
« Reply #5 on: November 24, 2010, 04:35:49 pm »
I see. What about adjusting the line thickness to the inverse of the current zoom?
Laurent Gomila - SFML developer

chugginwindex

  • Newbie
  • *
  • Posts: 4
    • View Profile
New to SFML, need help with World to Screen
« Reply #6 on: November 24, 2010, 05:23:37 pm »
That would work for this example I suppose. I'm tracing the orbits of stellar bodies as well however, and for that I need to be able to draw the lines exactly where they were. Drawing small circles or lines between their positions every frame with SFML is incredibly inefficient so I'd like to go the native route and write directly to a RenderImage with OpenGL, but for that I need to have the perspective just right so that the image lines up with what the user is actually observing so that the paths of their orbits are in sync with the stars themselves. Which is why I was looking for World to Screen function so that I could lessen the math a little bit...

I'll figure something out either way I'm sure  :wink: