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

Author Topic: SFML C# Views and Sprites  (Read 5381 times)

0 Members and 1 Guest are viewing this topic.

Xivister

  • Newbie
  • *
  • Posts: 2
    • View Profile
SFML C# Views and Sprites
« on: February 16, 2014, 07:21:25 am »
I am newer to using C# with SFML. I am having a problem with sprite transformations with views. In the game, when a mouse pressed event is triggered, the sprite is supposed to move to the position of the mouse click at a constant velocity over time. However, it does not always move to the correct position, which I believe is due to local versus global positions. Any idea on how to fix this issue?

My creation of the RenderWindow:
myProgram.app = new RenderWindow(new VideoMode(5000, 5000), "Game", Styles.Fullscreen);

My creation of the View:
VideoMode mode = VideoMode.DesktopMode;
View view = new View(new Vector2f(mode.Width/2, mode.Height/2), new Vector2f(mode.Width, mode.Height));

Event method:
static void MouseButtonPressed(object sender, MouseButtonEventArgs e)
{
        if (e.Button == Mouse.Button.Right)
            fighter.MoveToPoint(new Vector2f(Mouse.GetPosition().X, Mouse.GetPosition().Y));
}

Transformation Methods:
 
 public void MovetoPoint(Vector2f inputCoord)
  {
        aTimer.Enabled = true;
        inputPosition = inputCoord;
  }
  private void OnTimedEvent(object source, ElapsedEventArgs e)
  {
    if (this.Length(inputPosition - this.Position) > 0)
        this.Position = this.Position + (this.Normalize(inputPosition - this.Position) * (Math.Min(v, this.Length(inputPosition - this.Position))));
    else
            aTimer.Enabled = false;
  }



Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: SFML C# Views and Sprites
« Reply #1 on: February 16, 2014, 08:57:38 am »
Call window.MapPixelToCoords.
Laurent Gomila - SFML developer

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: SFML C# Views and Sprites
« Reply #2 on: February 16, 2014, 01:27:43 pm »
What Laurent said is correct of course, but three other things.

Mouse.GetPosition().Y

If you are using events then just use the position provided by the event args, no need to query real time information when it is already provided. Not to mention if your code is running slowly, by the time you process the event the mouse may have moved. Then stuff will happen like your sprite moving to the wrong location because you are basing your code on realtime positioning, not the position where the event happened.

private void OnTimedEvent(object source, ElapsedEventArgs e)

Don't use these timers and expect to get proper movement, they have a very low resolution and may not even execute the callback when you expect.

Instead measure the delta time that elapses between frames and use that. If you don't want to use System.Diagnostics.Stopwatch directly then look at NetEXT (see my signature). I have provided nice wrappers for easy handling and measuring of high resolution time with an API similar to sf::Clock and sf::Time.

myProgram.app = new RenderWindow(new VideoMode(5000, 5000), "Game", Styles.Fullscreen);
VideoMode mode = VideoMode.DesktopMode;
View view = new View(new Vector2f(mode.Width/2, mode.Height/2), new Vector2f(mode.Width, mode.Height));

Not sure how that is supposed to work, considering you should probably be using the desktop mode for window creation and the absolute size for the view creation  ;)
« Last Edit: February 16, 2014, 01:35:42 pm by zsbzsb »
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

Xivister

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: SFML C# Views and Sprites
« Reply #3 on: February 16, 2014, 07:23:18 pm »
Thank you - I will definitely check out NetEXT.