Half-circle bumps

Let us know if you have a nice R.U.B.E script to share!
Post Reply
iforce2d
Site Admin
Posts: 860
Joined: Sat Dec 22, 2012 7:20 pm

Half-circle bumps

Post by iforce2d » Sat Feb 18, 2017 2:13 am

Not really sure what to call this one... see this thread for the original topic: viewtopic.php?f=6&t=529

Code: Select all

//Name: Half-circle bumps
//Tags: line, bump
//Add body with a line fixture comprising a series of half-circles, offset from each other in x and y to form a row of bumps

float radius = queryNumericValue('Bump radius', 1);
float separationX = queryNumericValue('Bump separation X', 2);
float separationY = queryNumericValue('Bump separation Y', 2);
uint numSides = queryNumericValue('Number of sides', 14);
uint numBumps = queryNumericValue('Number of bumps', 4);
if ( numSides >= 3 && numBumps > 0 ) {
	body b = addBody(-1, '{"type":"dynamic","awake":true}');
	string xValues;
	string yValues;
	float xOffset = 0;
	float yOffset = 0;
	int rotateDir = -1;
	int yOffsetMod = -1;
	for (uint i = 0; i < numBumps; i++) {
		for (uint n = 0; n < numSides; n++) {
			float angle = n * (dr(360) / numSides);
			if ( i+n > 0 ) {
				xValues += ',';
				yValues += ',';
			}
			vec2 v;
			v.set( radius + xOffset + radius * rotateDir * cos(angle), yOffset + radius * sin(angle) );
			xValues += v.x;
			yValues += v.y;
			bool firstTime = i+n < numSides/2;
			int modulus = n % numSides;
			if ( ! firstTime ) {
				if ( modulus == (numSides/2) ) {
					yOffset += yOffsetMod * separationY;
					xOffset += separationX;
					rotateDir *= -1;
					yOffsetMod *= -1;

					v.set( radius + xOffset + radius * rotateDir * cos(angle), yOffset + radius * sin(angle) );
					xValues += ',' + v.x;
					yValues += ',' + v.y;					
				}
				if ( modulus == numSides-1 ) {

					angle = (n+1) * (dr(360) / numSides);
					v.set( radius + xOffset + radius * rotateDir * cos(angle), yOffset + radius * sin(angle) );
					xValues += ',' + v.x;
					yValues += ',' + v.y;

					yOffset += yOffsetMod * separationY;
					xOffset += separationX;
					rotateDir *= -1;
					yOffsetMod *= -1;
				}
			}
		}
	}
	string fixtureDef = '{"density":1,"shapes":[{"radius":0,"type":"line"}],"friction":0.2,"vertices":{"x":['+xValues+'],"y":['+yValues+']}}';
	b.addFixture(-1, fixtureDef);
	b.setPos( cursor() );
}
else
	print('Number of sides should be >= 3, number of bumps should be > 0');
Attachments
Selection_035.png
Selection_035.png (17.55 KiB) Viewed 8220 times

Post Reply