• entries
743
1924
• views
581047

# Blob Grappling Hooks

901 views

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 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::iterator begin(){ return v.begin(); } typename std::vector::iterator end(){ return v.end(); }private: void erase(std::vector &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 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 Event;template class BaseEvent{public: BaseEvent() : d(false) { } ~BaseEvent(){ for(auto i: v) i->remove(this); } void connect(AbstractDelegate &s){ v.push_back(&s); s.add(this); } void disconnect(AbstractDelegate &s){ v.remove_all(&s); }private: friend class Event; 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*> v;};template class Event{public: Event(){ e = new BaseEvent(); } ~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 *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.

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

## Create an account

Register a new account