04 Dec

WCK. Conveyor belt problem.

For Dolly The Sheep game I’m using WCK + Box2D Alchemy Port.
It’s a very handy framework, but unfortunately, it lacks documentation and has some bugs.
One of the bugs, I have recently discovered was with the build-in Conveyor belt.
Here is an example.
There are 3 boxes, they should move to the same direction, while they are lying on the conveyor, but… their behavior is unpredictable.

I’ve decided to make my own simple Conveyor Belt, by listening Contact Events.
Here is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
...
public override function create():void
{
	friction = 0; //this is very important, otherwise objects on conveyor will be moving with different speed
	reportBeginContact = true;
	reportEndContact = true;
 
	super.create();
 
	listenWhileVisible(this, ContactEvent.BEGIN_CONTACT, onBeginContact);
	listenWhileVisible(this, ContactEvent.END_CONTACT, onEndContact);
}
 
public override function update(param:Param):void //your own update function, calls every frame
{
	//set velocity to objects lying on the conveyor belt, I'm only using horizontal velocity here
	//_dir - direction, where object on conveyor should move to
	//_shape._normal - only the "y" of the normal, when object has contacted conveyor, in my case it can be only 1(from above) or -1(from below)
	for each(_shape in _shapes) //go through all the objects, contacting conveyor
	{
		_shape2 = _shape._shape;
		_shape2.b2body.SetLinearVelocity(new V2(_dir*_shape._normal*_speed, 0));
	}
}
 
protected function onBeginContact(event:ContactEvent):void
{
	//add object and its contact normal to the list
	//event.other.m_userData - our object
	//event.normal - our normal of the contact, that points from conveyor belt to object
	//if event.normal.y > 0, object has contacted conveyor from below
	//if event.normal.y  0 ? -1 : 1)));
}
 
protected function onEndContact(event:ContactEvent):void
{
	//remove object, that is not contacting with conveyor from the list
	...
}

Of course, my version has some problems too, mainly because of the zero friction.
Hope, new version of Box2D Alchemy Port with fixed bugs will be released soon.

Leave a Reply

Your email address will not be published. Required fields are marked *