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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - Guido_Ion

Pages: 1 [2] 3
16
Window / Gamepad Class that reads from SDL controller database
« on: October 02, 2021, 07:50:11 pm »
Hi, I'm trying to do a simple class to read SDL's database of controllers (the txt file). The problem is that SDL uses GUID of devices and I can't get that information with sf::Joystick since SFML works with vendorID and ProductID. I've tried also comparing the names of the joysticks but sometimes they are slightly different, for example SFML writes "Logitech Dual Action" while SDL writes "Logitech Dual Action USB".

Is there a way to read GUID from SFML or maybe somehow converting GUID to VendorID and ProductID? Is that info in there?

I'll share the results with the community once it's done (if it can be done)

17
Thank you very much! I was able to make it work, this is the final code for the formula:

Quote
forceInZAxis = 0.28f * force * std::cos(Game::DegreesToRadians(90 - initialAngle));

I had to add a multiplier (0.28f) because the force in the z-axis was too strong, but that's just for my game. Thanks again!

18
Hi, I'm programming a Football/Soccer game, currently I'm working on the height of the kick. The game is top down so I'm simulating the height of the ball with calculations and visual aids.

The kick consists of a 2d vector with it's magnitude (force) and it's direction. From this information and the angle of the kick in the z-axis (height, angle a in the image) that I assign according to the force, I want to get the z-component of the vector.

From hours of researching I get that I can get the x-component like this: std::abs(magnitude) * cos(angleInDegrees) and the y-component like this: std::abs(magnitude) * sin(angleInDegrees). But of course I have these components in the vector itself.

I have this so far for the z-component:
Quote
initialAngle = getAngleRelativeToForce(force); // this returns an angle from 0 to 60 according to the force of the kick (this works fine)
b2Vec2 resultingVector = force * direction;
float sinAngleY = resultingVector.x / std::abs(force); // angle between y-axis and hypotenus (x-component/hypotenuse)
// Formula for z-component: Force * sinAngleY * sin(initialAngle)
forceInZAxis = std::abs(force) * sinAngleY * std::sin(Game::DegreesToRadians(initialAngle));

This gives me weird behaviors: for example similar kick with huge height when I'm facing up, and no height when facing down. Or a strong kick with no height.

I'm stuck there, I keep reseraching but I feel there is something I'm missing and maybe someone can point it out.

19
But previousMousePosInCoords is static, so this line:

Code: [Select]
static sf::Vector2f previousMousePosInCoords = Game::GetWindow()->mapPixelToCoords(sf::Mouse::getPosition(*Game::GetWindow()));
is only assigned once. All the other times are assigned on this line:

Code: [Select]
previousMousePosInCoords = mousePosInCoords;
While mousePosInCoords is always assigned on this line:
Code: [Select]
sf::Vector2f mousePosInCoords = Game::GetWindow()->mapPixelToCoords(sf::Mouse::getPosition(*Game::GetWindow()));
This way the difference (mouseDifferenceInCoords) is working ok, it returns the difference.

The problem I have is when I move the view. The screen moves and even when there is no mouse movement it register a mouse movement because for SFML when the View is moved the mouse position is moved even if the mouse didn't move. That's the problem.

I guess I should substract the View movement to the mouse position making it messy. I was wondering if there is another way of doing it.
Thanks for answering!

20
Window / Calculating Mouse movement not accurate when View is modified
« on: March 08, 2021, 09:32:00 pm »
Hi! I'm implementing mouse movement that mimmics the movement of a GamePad Stick.

What I need is to calculate the movement each frame, I do this by calculating the difference between the previous position and the new one. The problem occurs when the View is modified by zooming and/or by moving. In this case even if I don't move the mouse, its position is modified.

This is part of the code:
Code: [Select]
static sf::Vector2f previousMousePosInCoords = Game::GetWindow()->mapPixelToCoords(sf::Mouse::getPosition(*Game::GetWindow()));
sf::Vector2f mousePosInCoords = Game::GetWindow()->mapPixelToCoords(sf::Mouse::getPosition(*Game::GetWindow()));
sf::Vector2f mouseDifferenceInCoords = mousePosInCoords - previousMousePosInCoords;
// if mouse moved
if (mouseDifferenceInCoords.x != 0.f && mouseDifferenceInCoords.y != 0.f) {
Input.thereIsMovement = true;
Input.directionNormalized = Game::SfVecToBoxVec(mouseDifferenceInCoords);
Input.directionLenght = Input.directionNormalized.Normalize();
previousMousePosInCoords = mousePosInCoords;
}
else Input.thereIsMovement = false;

I understand that if I work on pixels instead of coordinates it should work but the movement will depend on the resolution the game is played.
Other thing I could do is substract the amount of movement and zoom of the view but I think they might be a simpler better solution.
How could I do this?

21
Thanks fallahn! This is the final code, tested, working great:

Code: [Select]
void MapLoader::SetDrawingBounds(const sf::View& view) // MODIFIED - SEE https://en.sfml-dev.org/forums/index.php?topic=27182.0
{
// place this here so I don't modify more code outside
static float lastAngle = 0.f;

// only if center and/or rotation changed
if (view.getCenter() != m_lastViewPos || lastAngle != view.getRotation())
{
sf::FloatRect bounds;
bounds.left = view.getCenter().x - (view.getSize().x / 2.f);
bounds.top = view.getCenter().y - (view.getSize().y / 2.f);
bounds.width = view.getSize().x;
bounds.height = view.getSize().y;

///add a tile border to prevent gaps appearing
// additional space to add (half of x axis lenght)
const float borderSizeToAdd = view.getSize().x / 2.f;

bounds.left -= borderSizeToAdd;
bounds.top -= borderSizeToAdd;
bounds.width += 2 * borderSizeToAdd;
bounds.height +=2 * borderSizeToAdd;
m_bounds = bounds;

for(auto& layer : m_layers)
layer.Cull(m_bounds);
}
m_lastViewPos = view.getCenter();
lastAngle = view.getRotation();
}

IMPORTANT: the method "void MapLoader::draw(sf::RenderTarget& rt, sf::RenderStates states) const" has to be modified the same way so you can call renderWindow.draw (drawing the entire map) or MapLoader.Draw (drawing each layer separately)

So far tmxLoader is working great but I'll check tmxlite just in case.

EDIT: borderSizeToAdd was unnecesary large and some calculations weren't necessary

22
I'm using tmxLoader by fallahn and my view rotates with the movement of the player. The issue is that some parts of the map are not drawn when rotating. It seems that the drawing only takes the size of the view without the rotation.

As you can see in attachment 1, when the player and the view are not rotated (or slightly) the maps draws correctly, and in attachment 2 you can see that when they are rotated part of the map is not drawn.

Before drawing this is being called (private method):
Code: [Select]
void MapLoader::SetDrawingBounds(const sf::View& view)
{
if(view.getCenter() != m_lastViewPos)
{
sf::FloatRect bounds;
bounds.left = view.getCenter().x - (view.getSize().x / 2.f);
bounds.top = view.getCenter().y - (view.getSize().y / 2.f);
bounds.width = view.getSize().x;
bounds.height = view.getSize().y;

//add a tile border to prevent gaps appearing
bounds.left -= static_cast<float>(m_tileWidth);
bounds.top -= static_cast<float>(m_tileHeight);
bounds.width += static_cast<float>(m_tileWidth * 2);
bounds.height += static_cast<float>(m_tileHeight * 2);
m_bounds = bounds;

for(auto& layer : m_layers)
layer.Cull(m_bounds);
}
m_lastViewPos = view.getCenter();
}

Do I need to change tmxLoader myself or is there a way to set it up manually outside MapLoader?
And how do I need to change it?   ???

Note: I didn't want to post in the tmxLoader specific Topic since is been inactive for 4 years now.

23
Graphics / Re: Making the view follow player's position and rotation
« on: April 21, 2020, 06:15:02 pm »
I've solved it with a lot of trial and error, with absolute rotation and position. Thanks for the help. Here it is the final code:

First I calculate the offset in coordinates once (in Match constructor):
Code: [Select]
sf::Vector2i segment(0, (int)Game::WindowHeight / 4); // amount of pixels to shift (only in Y axis)
sf::Vector2f segmentCoords = Game::GetWindow()->mapPixelToCoords(segment); // translate to Coordinates
segmentCoord = segmentCoords.y; // store in a float

Then every frame I calculate the view like this:
Code: [Select]
m_view.setRotation(HumanFieldPlayer::Instance().getSFMLAngleInDegrees()) // rotation, straight forward

b2Vec2 direction = -HumanFieldPlayer::Instance().getHeading(); // get inverted direction
sf::Vector2f offset = sf::Vector2f(direction.x, direction.y) * segmentCoord; // add direction (opposite to player) to the segment (offset)

sf::Vector2f posCoords = HumanFieldPlayer::Instance().getSFMLPosition(); // get Player's Position in SFML Coordinates
sf::Vector2f finalVec(posCoords + offset); // add offset to players position
m_view.setCenter(finalVec); // set new center for the view

24
Graphics / Re: Making the view follow player's position and rotation
« on: April 18, 2020, 11:30:25 pm »
Thanks Hapax, that's close but not exactly what I need and I'm trying to change it without good results.

That function works on the added rotation, I need it to work with the full rotation. Namely, instead of using rotate on the view I need to use setRotation, or figure out how to get the rotation to add instead of the full rotation.

So far I have this:
Code: [Select]
float rotationToAdd = m_view.getRotation() - HumanFieldPlayer::Instance().getSFMLAngleInDegrees();
float rotationInRadians = rotationToAdd * b2_pi / 180.f;
sf::Vector2f posCoords = HumanFieldPlayer::Instance().getSFMLPosition(); // in SFML Coordinates
posCoords.y -= Game::WindowHeight / 4.f;

const sf::Vector2f offset{ posCoords - m_view.getCenter() };
const float sine{ std::sin(rotationInRadians) };
const float cosine{ std::cos(rotationInRadians) };
const sf::Vector2f rotatedOffset{ cosine * offset.x - sine * offset.y, sine * offset.x + cosine * offset.y };
m_view.rotate(rotationToAdd);
m_view.move(offset - rotatedOffset);

Game::GetWindow()->setView(m_view);

I've tried several things, in this case I'm trying to get the rotation to add instead of the full rotation but when I rotate the player, the view goes crazy.

25
I'm trying to get the view to follow the player's position and rotation as it moves. I want to place the view slightly upwards in the direction of the player (variable segment) so the player is placed almost at the bottom of the screen.

Here is the code of the placement of the view:

Code: [Select]
float angle = HumanFieldPlayer::Instance().getSFMLAngle(); // in Degrees
float segment = Game::WindowHeight / 4.f;

m_view.setRotation(angle);

sf::Vector2f posCoords = HumanFieldPlayer::Instance().getSFMLPosition(); // in SFML Coordinates
m_view.setCenter(posCoords.x, posCoords.y - segment);

Game::GetWindow()->setView(m_view);

As you can see in the GIF (see attachment) it's not the behavior I want, when I rotate the player ir should be always in the same position, at the bottom in the middle like in the beginning of the GIF.

I tried a lot of things but I'm pretty lost, what am I missing?


26
General / Re: I need an "empty" GUI. Just the behavior, not the graphics.
« on: February 07, 2020, 04:23:53 pm »
I've been looking TGUI and I think that it's what I need. I'm going to try it, Thanks

27
I've been searching different GUIs but they all come with buttons or fonts to draw. My artist will make all the buttons, etc. as sprites so I just need the functionality of the GUI and I will slap the sprite on top of it.

Is there any simple GUI out there for this? Should I make my own?

28
SFML projects / Re: Let There Be Light 2
« on: July 29, 2019, 05:32:40 pm »
Thanks for the answer, but that didn't work, it still draws the dark parts twice and the lights very strong, that's not the result I want.
Maybe I need to create other sprites (penumbraTexture.png, etc.) with the dark parts as transparent for the second lightsystem. I don't know how to do that but I'll mess around with GIMP and see what's the result.

If lolz or someone has another easy answer for using 2 light systems it would be great.

29
SFML projects / Re: Let There Be Light 2
« on: July 25, 2019, 09:29:25 pm »
Hi, I hope this is not dead  ??? I'm still using LTBL2 , it's great!

For performance issues I wanted to use 2 diferent ltbl::LightSystem(), one for static lights (render once, draw every frame) and another one for dynamic lights (render and draw every frame).

The issue I'm having is that when drawing both lightSystems everything is really dark, I guess the not lighted parts are being drawn twice, thus turning everything dark.

Is there a way to only draw the lights for each lightSystem and not the dark parts?  :o

Or maybe there is another way instead of using 2 light systems, I need to render only the lights that change and not the static ones, BUT I need to draw every light, every frame.

30
SFML projects / Re: Let There Be Light 2
« on: January 21, 2017, 08:50:14 pm »
Hey, I'm having trouble getting this effect: I need occluders to cast shadows over other occluders like this:



I need the box on the bottom to be affected by the box on the top (meaning: be in the dark), like the player is.
In (1) player and grey box are visible and on (2) only the player is in the dark while the grey box isn't affected by the shadow of the big box.
Any idea on how to achieve this?

Pages: 1 [2] 3