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 - OualidH38

Pages: [1] 2
1
I made it, it is working perfectly!

To resolve the collision in my case, I just had to move the entity back to the last correct position, doesn't matter if it is a top/bottom or left/right collision. I used the same getManifold function, this is the resolve function:
void Character::resolve(const sf::Vector3f & manifold) {

        move(sf::Vector2f(manifold.x, manifold.y) * -manifold.z);
}
 

Thank you MORTAL for your help  ;D

2
Ok I think I've understand the logic, moving the entity to the last correct position. I will write some code tonight.

For an RPG like mine, I just need to check if manifold.x != 0 (left/right collision) and manifold.y - manifold.z value (top/bottom collision) inside the resolve function right?

3
Thanks for your help, I will look at it tomorrow  ;)

4
Thank you for the reply!  :D

So you have managed to parsed tiled objects into entities, then you handle the collision between the player and those entities.

But I'm not sure to understand how you react to a collision. How do you resolve the collision?


5
Hello everyone !  ;D

I'm back to collision issues since I started to use Tiled Editor and the sfml-tmxloader. Now I'm wondering how could I handle efficiently a collision between an entity and a tilemap created with Tiled?

I use a TileMap class (that inherit from SceneNode class), which allows me to load a tilemap from TiledEditor using sfml-tmxloader.

So far I was able to detect a collision between the player entity and objects from the tilemap, here is the code:
std::vector<sf::Vector2f> points;
    points.push_back(sf::Vector2f(0.f, 8.f));
    points.push_back(sf::Vector2f(-8.f, 0.f));
    points.push_back(sf::Vector2f(8.f, 0.f));
    points.push_back(sf::Vector2f(0.f, 16.f));
 
    sf::Vector2f position = mPlayerCharacter->getWorldPosition();
 
    std::vector<tmx::MapLayer>& layers = mTileMap->getLayers();
 
    for (auto& layer : layers)
    {
        if (layer.type == tmx::ObjectGroup)
        {
            for (auto& obj : layer.objects)
            {
                if (layer.name == "Objects")
                {
                    for (auto& point : points) {
 
                        if (obj.Contains(position + point))
                        {
                            std::cout << "Collision";
                            break;
                        }
                       
                       
                    }
 
                }
            }
           
        }
           
    }
 

But I was wondering how to react to that collision according to the book way-of-doing-things.

Usually I just take the current position and calculate the future position, then I test the future position, if there's no collision I just set the future position to the sprite, like this :
std::vector<sf::Vector2f> points;
    points.push_back(sf::Vector2f(0.f, 8.f));
    points.push_back(sf::Vector2f(-8.f, 0.f));
    points.push_back(sf::Vector2f(8.f, 0.f));
    points.push_back(sf::Vector2f(0.f, 16.f));
 
 
sf::Vector2f position = mSprite.getPosition();
sf::Vector2f velocity = getVelocity() * dt.asSeconds();
position += velocity;
 
//Testing collision in the future position
for(auto& point : points) {
 
if(obj.Contains(position + point)) {
    //Collision!
}
else
    mSprite.setPosition(position);
}
 

But I don't know how to implement this logic according to the entity system. I tried to handle this directly in my Character class (which looks like the Aircraft class) but I wasn't able to get a correct collision response.
I don't see how I could get the future position of an entity in a generalize approach.

I was thinking of using the SceneNode::checkNodeCollision to handle the collision, which will give me something like this:

//Inside the handleCollision function


void World::handleCollision(sf::Time dt) {

        std::set<SceneNode::Pair> collisionPairs;
        mSceneGraph.checkSceneCollision(mSceneGraph, collisionPairs);

        for (SceneElement::Pair pair : collisionPairs) {

                if (matchesCategories(pair, Category::PlayerCharacter, Category::TileMap)) {

                        auto& player = static_cast<Character&>(*pair.first);
                        auto& tileMap = static_cast<TileMap&>(*pair.second);

                        player.checkTileMapCollision(tileMap, dt);
                }
....


// Inside the Character class

void Character::checkTileMapCollision(TileMap& tileMap, sf::Time dt){

std::vector<sf::Vector2f> points;
    points.push_back(sf::Vector2f(0.f, 8.f));
    points.push_back(sf::Vector2f(-8.f, 0.f));
    points.push_back(sf::Vector2f(8.f, 0.f));
    points.push_back(sf::Vector2f(0.f, 16.f));
 
 
sf::Vector2f position = getWorldPosition();
sf::Vector2f velocity = getVelocity() * dt.asSeconds();
position += velocity;
 
//Testing collision in the future position
for(auto& point : points) {
 
if(obj.Contains(position + point)) {
    //Collision!
}
else
    setPosition(position);
}
 


I don't know if my logic is correct to be honest. Looking for some help guys, any idea is welcomed  ;D
 

6
Graphics / Re: Animated Tile with Tiled Editor and SFML
« on: March 23, 2016, 09:05:28 am »
Hi,

I've found this link talking about my issue: https://github.com/fallahn/sfml-tmxloader/issues/6

Unfortunately I've not found in which way Tiled add properties to an animated tile and how to get that property using the parser.

Do you have any idea?

7
Graphics / Animated Tile with Tiled Editor and SFML
« on: March 17, 2016, 07:44:47 pm »
Hi everyone,

I have a little question about animated tiles in Tiled Editor and SFML. I'm using this parser : https://github.com/fallahn/sfml-tmxloader

I was wondering if there is a way to handle animated tiles as follow:
https://youtu.be/ZwaomOYGuYo?t=15m19s

Because when I add an animation, it is not shown as animated, so I guess it is not handle by the parser.

Any idea?

Thank you in advance  ;D

8
General / Re: I want to make sprites appear with a random time delay
« on: March 14, 2016, 01:51:31 pm »
Can you please past the solution here and put your problem as "solved" if you found a solution by your own?

I was not trying to be mean when I said that you should learn a bit more. The if statement was a basic condition, and you did not understand it. No offense.

I admire the fact that you are motivated and looking for how things work, but start with the begining (c++ basis, logic, etc) before.

It's fine when you ask questions of course, but the logic that I've put before is easy to understand.

Sorry if I offended you, that was not my purpose.
Good luck

9
General / Re: I want to make sprites appear with a random time delay
« on: March 14, 2016, 12:13:42 am »
I don't really understand the code you sent me ?
Why is mvalue which is a bool less than time zero? What are you achieving with the += and the -= Please elaborate ..

I suggest you to read carefully what I wrote, I'm not trying to compare a bool with sf::Time::Zero.
The if statement tests the boolean and the mCountDown value.

eXpl0it3r is right, you should learn a bit more before asking for a pre-made solution.

10
General / Re: I want to make sprites appear with a random time delay
« on: March 13, 2016, 08:15:01 pm »
/*Use a boolean for your condition: mValue
a float that will holds the time: mCountDown
another float with a const value sf::seconds(5.f): TimeInterval

Use this inside an update function with an sf::Time dt that holds elapsedTime */


if (mValue && mCountDown <= sf::Time::Zero) {

                mValue= false;

                //Do your stuff here

                mCountDown += TimeInterval;
               
               
        }
        else if (mCountDown > sf::Time::Zero) {

                mCountDown -= dt;

}

 

11
You're trying to pass a const type to a lambda that takes a non-const argument. If it's the find_if lambda, it can (and must) take a const Ptr&.

It works, you were right!  :D

Actually this is what I have :

//The two draw functions
void drawBackChildren(sf::RenderTarget& target, sf::RenderStates states, const Ptr& child) const;
void drawFrontChildren(sf::RenderTarget& target, sf::RenderStates states, const Ptr& child) const;

//Inside the draw function

states.transform *= getTransform();

        auto split = std::find_if(mChildren.begin(), mChildren.end(), [this](const Ptr& child) {return child->getWorldPosition().y >= this->getWorldPosition().y; });

        // draw children that are behind parent
        for (auto it = mChildren.begin(); it != split; ++it)
                drawBackChildren(target, states, *it);// It needs only one dereferencing

        drawCurrent(target, states);

                // draw children that are in front of parent
                for (auto it = split; it != mChildren.end(); ++it)
                        drawFrontChildren(target, states, *it);

 

Thank you all for your help  ;D

Fixed!


12
Yes, don't use Ptr as a parameter type. It's a typedef to std::unique_ptr<SceneNode> and thus owns the scene node. Passing unique pointers by value means transferring ownership to the function.

Instead, simply use a reference, possibly to a const object.

Why are drawBackChildren() and drawFrontChildren() virtual?

Oh I didn't notice that, it was an error  ;D

Ok so I changed the argument from Ptr to const SceneNode&, dereferenced twice the iterator, and I have a weird error that says:

Error    C2664  'bool SceneElement::draw::<lambda_cc8223163d3d6fd2ab5ca905407b6710>::operator ()(SceneNode::Ptr &) const' : impossible to convert argument 1 from 'const std::unique_ptr<SceneNode,std::default_delete<_Ty>>' to 'SceneNode::Ptr &'

2DGameSFML      g:\programmes\visual studio\vc\include\algorithm        43     

 


There's still a conversion issue  :-\

13
You must dereference twice, once for the iterator and once for the unique_ptr.
**it

I tried but it says that there's no conversion between SceneNode and SceneNode::Ptr defined.
The problem is perphaps my child argument in the functions drawBack and Front.

I'm not used to multiple dereferencing  :-[

14
That is exactly what I was looking for thank you!  ;)

This is what I've done :
//two function for drawing the children

virtual void drawBackChildren(sf::RenderTarget& target, sf::RenderStates states, Ptr child) const;
virtual void drawFrontChildren(sf::RenderTarget& target, sf::RenderStates states, Ptr child) const;

//Inside the draw function

states.transform *= getTransform();

auto split = std::find_if(mChildren.begin(), mChildren.end(), [this](Ptr& child) {return child->getWorldPosition().y >= this->getWorldPosition().y; });

        // draw children that are behind parent
        for (auto it = mChildren.begin(); it != split; ++it)
                drawBackChildren(target, states, *it);

        drawCurrent(target, states);

                // draw children that are in front of parent
                for (auto it = split; it != mChildren.end(); ++it)
                        drawFrontChildren(target, states, *it);

//the drawFrontChildren and drawBackChildren look as follow

child->draw(target, states);
 

But I have an issue with the child param, how could I get the child from the iterator "it" and pass it as an argument?
"it" is a unique_ptr iterator pointing to a child how I am suppose to access to the child?

With this code an error says:
"Impossible to do a reference to a function "std::unqiue_ptr<_Ty, _Dx>::unqiue_ptr....."

15
And why do you think it's not a good solution?

To make it a bit nicer you could add a property to the entity and check that.

I think it's not a good solution because I would have to change the drawChildren function, and add a parameter like a child, and then draw only one child by one.
The drawChildren will become a drawChild.
Inside the draw function I compare the position in the y axis between children and the parent, then I draw the parent and the current child like this:


Inside the draw() function
Compare the the position between the parent and all his children individually

for(auto child : mChildren){

if(parent.position().y < child.position().y)
{
   drawChild(child)
   drawCurrent()
}
else
{
   drawCurrent()
   drawChild(child)
}
}
 

However, with this solution I draw the parent x times depending on how many children it has.
Let's say that we have a parent, with 5 children.
The parent will be drawn 5 times, because for each child we draw.
There's a way to do, but I only have words to explain it, don't know how to implement it.


The solution looks like this for me :


two child containers :
std::vector<std::unique_ptr<SceneNode>> mFrontChildren
std::vector<std::unique_ptr<SceneNode>> mBackChildren

two drawChildren function:
drawFrontChildren(...)
drawBackChildren(...)

Inside the update function:

for(auto child : mChildren){

if(parent.position().y < child.position().y)
{
   mBackChildren.push_back(std::move(child));
}
else
{
   mFrontChildren.push_back(std::move(child));
}
}


Inside the draw function

drawBackChildren(...)
drawCurrent(...)
drawFrontChildren(...)

 

I need two separate functions for front and back children, the problem is, how to dynamically switch children from front and behind?
With the example of the weapon, sometimes it's in front of the player, sometimes it's behind him.

I don't really know how to implement this kind of logique.

Pages: [1] 2
anything