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.


Topics - warbaque

Pages: [1]
1
Just needed to add following to Entity constructor. (why does it work on Windows even without?)
    def __init__(self):
        sf.Drawable.__init__(self)
 

The following code works fine on windows machine, but I get segfault when trying to get it run on Arch Linux.
Am I doing something wrong or why does this happen?

import random
import sfml as sf


width = 1280
height = 720


class Entity(sf.Drawable):


        def __init__(self, position):

                self.shape = sf.CircleShape()
                self.shape.radius = 20

                r = 55 + random.randrange(200)
                g = 55 + random.randrange(200)
                b = 55 + random.randrange(200)
                self.shape.fill_color = sf.Color(r, g, b, 50)
                self.shape.outline_color = sf.Color(r, g, b, 200)
                self.shape.outline_thickness = 1

                self.position = position


        # segmentation fault (core dumped)
        def draw(self, target, states):

                self.shape.position = self.position - self.shape.radius
                target.draw(self.shape, states)


        # works fine
        def draw_this(self, target):

                self.shape.position = self.position - self.shape.radius
                target.draw(self.shape)



class Test(object):


        def __init__(self):

                self.window = sf.RenderWindow(sf.VideoMode(width, height), "Render Test")
                self.window.vertical_synchronization = False
                self.entities = []


        def run(self):

                for i in range(20):
                        self.entities.append(Entity(sf.Vector2(
                                random.randrange(width),
                                random.randrange(height))))

                while self.window.is_open:

                        for event in self.window.events:
                                if (type(event) is sf.CloseEvent):
                                        self.window.close()

                        self.render()


        def render(self):

                self.window.clear()

                for e in self.entities:
                        self.window.draw(e)
                        # e.draw_this(self.window)

                self.window.display()



if __name__ == "__main__":

        test = Test()
        test.run()
 

2
General / Integrating pathfinding to the world and scenegraph
« on: December 19, 2013, 05:21:04 am »
Now that I've got my collision handling working (and components dependant on that) quite well, it's time to move to next problem.

How should I integrate pathfinding in to the book structure?

When and how should I update pathfinding?
How often should I update pathfinding for moving entities?

How does my pathfinding know what entities are placed on the map?

One idea I had was to set pathfinding nodes to my collisiongrid and check nodes to see if they collide with anything and change their characteristics based on that.

Example map:
Start and goal location for 2 entities
###################################
# start   ooo                 end #
#  x      ooo                  x  #
#         ooo                     #
#         ooo                     #
#         ooo                     #
#         ooo                     #
#         ooo                     #
#         ooo                     #
#         ooo                     #
#         ooo                     #
#  oooooooooo                     #
#  oooooooooo                     #
#  oooooooooo                     #
#                                 #
###################################

Entity 1 can dig slowly through (o) wall while Entity 2 cannot
###################################
# start   ooo                 end #
#  x 1________________________ x  #
# 2       ooo           /         #
# |       ooo          /          #
# |       ooo         /           #
# |       ooo        /            #
# |       ooo       /             #
# |       ooo      /              #
# |       ooo     /               #
# |       ooo    /                #
# |oooooooooo   /                 #
# |oooooooooo  /                  #
# |oooooooooo /                   #
# \ _________/                    #
###################################

Pathfinding nodes and entity pathfinding should be updated so Entity 2 can see that shorter path has appeared
###################################
# start   ooo                 end #
#  x...../__1_________________ x  #
# .      |ooo                     #
# .     / ooo                     #
# .    /  ooo                     #
# .   /   ooo                     #
# .  /    ooo                     #
# . /     ooo                     #
# ./      ooo                     #
# /       ooo                     #
# |oooooooooo                     #
# 2oooooooooo                     #
#  oooooooooo                     #
#                                 #
###################################
 

And my current collision detection
#include <CollisionGrid.hpp>

#include <SFML/System/Vector2.hpp>
#include <SFML/System/Time.hpp>

#include <vector>

class Entity;
class CommandQueue;

class Collision
{
        public:
                typedef std::pair<Entity*, Entity*> Pair;

        public:
                                                                                        Collision();
                void                                                            update(sf::Time dt);
                void                                                            updateCollisionGrid(CommandQueue& commands);

        private:
                void                                                            handleCollision(Pair colliders, sf::Vector2f offset, sf::Time dt);

        private:
                CollisionGrid                                           mCollisionGrid;
                std::vector<Entity*>                            mMovingEntities;
};

#include <SFML/System/Vector2.hpp>

#include <map>
#include <set>
#include <vector>


class Entity;

class Cell
{
        public:
                typedef std::set<Entity*> Container;

        public:
                void                            setNeighbours(Cell* left, Cell* right, Cell* up, Cell* down);
                void                            addEntity(Entity& entity);
                void                            removeEntity(Entity& entity);
                Container                       getNearbyEntities();
                void                            clear();

        private:
                void                            appendEntitiesTo(Container& entities) const;

        private:
                Container                       mEntities;

        protected:
                Cell*                           mLeft;
                Cell*                           mRight;
                Cell*                           mUp;
                Cell*                           mDown;
};

class CollisionGrid
{
        public:
                                                        CollisionGrid(float sceneWidth, float sceneHeight, float cellSize);

                void                            addEntity(Entity& entity);
                void                            removeEntity(Entity& entity);
                Cell::Container         getNearbyEntities(Entity& entity);
                void                            clear();

        private:
                Cell&                           getCell(const sf::Vector2f position);

        private:
                std::vector<Cell>       mCells;
                int                                     mCols;
                int                                     mRows;

                float                           mSceneWidth;
                float                           mSceneHeight;
                float                           mCellSize;
};

3
General / Attack implementation from the book: 4 projectiles instead of 1
« on: December 17, 2013, 05:44:44 pm »
Fixed now. I had a typo in my scenenode categories.

Everytime any character pushes attackCommand to the commandqueue, that command gets executed 4 times.
3 projectiles are added where character is currently and 4. as an invisible child to the character. And if I enable players text node, 5th one is added relative to that.  What could cause this?

Category changes behaviour, but I still haven't completely understood the command structure.


Attack command
mAttackCommand.category = Category::Scene;
mAttackCommand.action = [this, &textures] (SceneNode& node, sf::Time)
{
        std::cout << "  -> create projectile" << std::endl;
        createProjectile(node, textures);
};

void Character::createProjectile(SceneNode& node, const TextureHolder& textures) const
{
        std::unique_ptr<Projectile> projectile(new Projectile(Projectile::Placeholder, textures));

        projectile->setPosition(getPosition());
        node.attachChild(std::move(projectile));
}
 

Character update
void Character::updateCurrent(sf::Time dt, CommandQueue& commands)
{
        if (getCategory() == Category::Player)
                updateTexts();

        checkAttackCooldown(dt, commands);
        Entity::updateCurrent(dt, commands);
}

void Character::checkAttackCooldown(sf::Time dt, CommandQueue& commands)
{
        if (getCategory() & Category::Enemy)
                attack();

        // Check for automatic attacking, allow only in intervals
        if (mIsAttacking && mAttackCooldown <= sf::Time::Zero)
        {
                std::cout << "push attack command" << std::endl;
                // Interval expired: Attack
                commands.push(mAttackCommand);
                mAttackCooldown += Table[mType].attackInterval;
                mIsAttacking = false;
        }
        else if (mAttackCooldown > sf::Time::Zero)
        {
                // Interval not expired: Decrease it further
                mAttackCooldown -= dt;
                mIsAttacking = false;
        }
}
 

stdout
push attack command
        -> create projectile
        -> create projectile
        -> create projectile
        -> create projectile
        -> create projectile

4
My game structure follows SFML book quiet closely (scenegraph, commands, etc) and I'm currently trying to implement better collision detection.

Where should I store my grid / quadtree and how my entities should access and use it?


My gameworld (everything is visible) consists currently of 3600 tile entities (stationary, might have collision box) and 1-400 character entities (roam around map, colliding into things)


**Current entity update step.**
I do X/Y-axis movement separately. Because I want to slide against entities when colliding diagonally.

1. Move entity horizontally
2. Check and handle collisions
3. Move entity vertically
4. Check and handle collisions
5. Repeat 1-4 for all entities

void Entity::updateCurrent(sf::Time dt, CommandQueue& commands)
{
        setPreviousPosition(getPosition());
        move(sf::Vector2f(mVelocity.x, 0) * dt.asSeconds());
        handleCollision();
        setPreviousPosition(getPosition());
        move(sf::Vector2f(0, mVelocity.y) * dt.asSeconds());
        handleCollision();
}

void Entity::handleCollision()
{
        if (getCategory() & Category::Character)
        {
                std::set<SceneNode::Pair> collisionPairs;
                checkCollision(*mSceneGraph, collisionPairs);

                for (SceneNode::Pair pair : collisionPairs)
                {
                        if (matchesCategories(pair, Category::Enemy, Category::Player))
                        {
                                auto& player = static_cast<Entity&>(*pair.second);
                                resetPreviousPosition();
                                player.damage(1);
                                break;
                        }
                        else if (matchesCategories(pair, Category::Character, Category::NonPassable))
                        {
                                resetPreviousPosition();
                                break;
                        }
                }
        }
}


**How I plan to do entity update step.**

1. Remove entity from grid (old location)
2. Move entity horizontally
3. Add entity to grid (new location)
4. Check and handle collisions
5. Remove entity from grid (old location)
6. Move entity vertically
7. Add entity to grid (new location)
8. Check and handle collisions
9. Repeat 1-8 for all entities


So my questions are as follow:
(1) Is the above implementation reasonable or smart way to do this?
(2) Where should I store and how to access my grid implemtation?

Pages: [1]