Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.






Blob Grappling Hooks

Posted by Aardvajk, 04 September 2014 · 360 views

Attached Image

Just a quick update on the soft body game. Now have the basic prototypes for the left and right mouse button grappling hooks working in terms of the physics. No idea yet how I'm going to render these arms in a decent way but it is showing some promise.



Thanks for all the positive feedback on my recent article.

A few posters commented on the issues I'd missed to do with deleting events or delegates during the execution of an event call and, funnily enough, shortly after this I discovered I needed to do exactly this in the game.

The solution I have is two-fold. Firstly I created a lockable vector to represent the storage of the Event class. This basically works as a normal vector unless locked. When locked, instead of removing elements, it just swaps them out with nullptrs and any additions to the vector during a lock are added to a second vector. When unlock() is called, it cleans out the nullptrs and adds any of the added items to the main vector. This means that while the vector is locked, the iterators to the main list can't be invalidated:

template<class T> class delegate_vector
{
public:
    delegate_vector() : x(0), n(false) { }

    void lock(){ ++x; }
    void unlock(){ if(!--x) clean_up(); }

    bool locked() const { return x; }

    void push_back(const T &t){ x ? a.push_back(t) : v.push_back(t); }
    void remove_all(const T &t){ x ? remove_all_lock(t) : remove_all_unlock(t); }

    typename std::vector<T>::iterator begin(){ return v.begin(); }
    typename std::vector<T>::iterator end(){ return v.end(); }

private:
    void erase(std::vector<T> &v, const T &t){ v.erase(std::remove(v.begin(), v.end(), t), v.end()); }
    void clean_up(){ if(n) erase(v, nullptr); for(auto i: a) v.push_back(i); n = false; }

    void remove_all_lock(const T &t){ for(auto &i: v) if(i == t){ i = nullptr; n = true; } erase(a, t); }
    void remove_all_unlock(const T &t){ erase(v, t); }

    size_t x;
    bool n;
    std::vector<T> v, a;
};
The next step was to change the Event class to be called BaseEvent, then wrap it inside another Event class. This way, when the Event is deleted, I can postpone deleting the BaseEvent until it has finished its current processing. This involves the dreaded delete this but I believe it to be a good use here. Looks like this now:

template<class... Args> class Event;

template<class... Args> class BaseEvent
{
public:
    BaseEvent() : d(false) { }
    ~BaseEvent(){ for(auto i: v) i->remove(this); }

    void connect(AbstractDelegate<Args...> &s){ v.push_back(&s); s.add(this); }
    void disconnect(AbstractDelegate<Args...> &s){ v.remove_all(&s); }

private:
    friend class Event<Args...>;

    BaseEvent(const BaseEvent&);
    void operator=(const BaseEvent&);

    void call(Args... args){ v.lock(); for(auto i: v) if(i){ i->call(args...); if(d) break; } v.unlock(); if(d) delete this; }

    bool d;
    delegate_vector<AbstractDelegate<Args...>*> v;
};

template<class... Args> class Event
{
public:
    Event(){ e = new BaseEvent<Args...>(); }
    ~Event(){ if(e->v.locked()) e->d = true; else delete e; }

    void operator()(Args... args){ e->call(args...); }

private:
    friend class Delegate;

    Event(const Event&);
    void operator=(const Event&);

    BaseEvent<Args...> *e;
};
So if the Event gets deleted during its own processing, the deletion of the BaseEvent which contains the actual state data is postponed until after the execution has a chance to break out of the loop. delete this is to be used with extreme caution. I accept no responsibility for the many bad things that can happen if you stuff this up. Posted Image

The reason I find delete this acceptable here is this is a non-copyable class that is only ever created as a member of an Event. So at the point d is true, we know the only class that could be referencing this one is already dead and gone. Thus we avoid the many potential problems normally associated with delete this that are generally related to having multiple references to an instance.

The posters in my article were discussing an approach to this that involved maintaining some static state, but I prefer to avoid that wherever possible. My approach avoids the need for any static state but adds a minor overhead that all Events now have an additional indirection when invoked.

In terms of the game, the way I have Box2D wrapped is that my Body class emits a collided<Body*, Vec2> event. This is all handled inside the Physics class, so when you create a Body you can just connect to its collided event via the delegate system.

In the case of the grapple, when it collides with the scenery, I need to delete its body and form a distance joint between the previous link in the grapple rope and the scene object. Rather than hack and postpone removing the body until the next frame, I wanted to be able to delete the Body inside its own collided() event hence the need to work out the system above.

As far as I can grok, the above approach protects against pretty much any combination of connects, disconnects etc that you throw at it. I've also tested making a new connection to an Event inside its own event invocation and that also seems to work okay, but I wouldn't bet much on there being no nasties still lurking somewhere, even in the single-threaded context it is designed for.

I may create a new article on this at some point if anyone is interested.




I really liked the video. Nice job.

I really liked the video. Nice job.


Thanks smile.png

November 2014 »

S M T W T F S
      1
2345678
9101112131415
161718192021 22
23242526272829
30      

Recent Entries

Recent Comments

Recent Entries

Recent Comments

PARTNERS