1
Graphics / Re: How to draw object with position relative to another object?
« Last post by eXpl0it3r on Today at 12:22:17 pm »What you can do for relative positioning is set a relative position on the HitPoint, then pass the transform of your player to the HitPoint and a multiply the transform of the HitPoint with the one of the Player, essentially applying all the transformations of the player to the HitPoint.
I also highly recommend to make use of Drawable and Tansformable.
Here's an example, since you seem to use C#, I've also written in in C#:
I also highly recommend to make use of Drawable and Tansformable.
Here's an example, since you seem to use C#, I've also written in in C#:
using SFML.Graphics;
using SFML.System;
using SFML.Window;
var player = new Player();
player.Position = new Vector2f(75, 150);
var hitPoint = new HitPoint();
hitPoint.Position = new Vector2f(2, -10);
var window = new RenderWindow(new VideoMode(200, 200), "SFML.Net Test");
window.SetFramerateLimit(144);
window.Closed += (_, _) => { window.Close(); };
window.KeyPressed += (_, eventArgs) =>
{
const int speed = 2;
var direction = eventArgs.Code switch
{
Keyboard.Key.Left => -1,
Keyboard.Key.Right => 1,
_ => 0
};
player.Position += new Vector2f(direction * speed, 0);
};
while (window.IsOpen)
{
window.DispatchEvents();
window.Clear();
window.Draw(player);
window.Draw(hitPoint, new RenderStates(player.Transform));
window.Display();
}
public class Player : Transformable, Drawable
{
private readonly RectangleShape _shape;
public Player()
{
_shape = new RectangleShape(new Vector2f(20, 20));
_shape.FillColor = Color.Green;
}
public void Draw(RenderTarget target, RenderStates states)
{
states.Transform *= Transform;
target.Draw(_shape, states);
}
}
public class HitPoint : Transformable, Drawable
{
private readonly RectangleShape _shape;
public HitPoint()
{
_shape = new RectangleShape(new Vector2f(16, 4));
_shape.FillColor = Color.Red;
}
public void Draw(RenderTarget target, RenderStates states)
{
states.Transform *= Transform;
target.Draw(_shape, states);
}
}
using SFML.System;
using SFML.Window;
var player = new Player();
player.Position = new Vector2f(75, 150);
var hitPoint = new HitPoint();
hitPoint.Position = new Vector2f(2, -10);
var window = new RenderWindow(new VideoMode(200, 200), "SFML.Net Test");
window.SetFramerateLimit(144);
window.Closed += (_, _) => { window.Close(); };
window.KeyPressed += (_, eventArgs) =>
{
const int speed = 2;
var direction = eventArgs.Code switch
{
Keyboard.Key.Left => -1,
Keyboard.Key.Right => 1,
_ => 0
};
player.Position += new Vector2f(direction * speed, 0);
};
while (window.IsOpen)
{
window.DispatchEvents();
window.Clear();
window.Draw(player);
window.Draw(hitPoint, new RenderStates(player.Transform));
window.Display();
}
public class Player : Transformable, Drawable
{
private readonly RectangleShape _shape;
public Player()
{
_shape = new RectangleShape(new Vector2f(20, 20));
_shape.FillColor = Color.Green;
}
public void Draw(RenderTarget target, RenderStates states)
{
states.Transform *= Transform;
target.Draw(_shape, states);
}
}
public class HitPoint : Transformable, Drawable
{
private readonly RectangleShape _shape;
public HitPoint()
{
_shape = new RectangleShape(new Vector2f(16, 4));
_shape.FillColor = Color.Red;
}
public void Draw(RenderTarget target, RenderStates states)
{
states.Transform *= Transform;
target.Draw(_shape, states);
}
}