Esempio n. 1
0
File: a.c Progetto: zeotrope/j7-src
static DF1(breduce){A z;B b,*u,*v,*x,*xx;I c,cv,d,m;SF f2;VA*p;
 RZ(w); /* AN(w)&&1<IC(w) */
 m=IC(w); RZ(z=tail(w)); c=AN(z); x=BAV(z); v=BAV(w);
 p=vap(self);
 switch(1<c?0:p->bf){
  case V0001: *x=memchr(v,C0,m)?0:1; R z;
  case V0111: *x=memchr(v,C1,m)?1:0; R z;
  case V1110: u=memchr(v,C0,m); d=u?u-v:m; *x=d%2!=d<m-1; R z;
  case V1000: u=memchr(v,C1,m); d=u?u-v:m; *x=d%2==d<m-1; R z;
  case V0010: u=memchr(v,C0,m); *x=(u?u-v:m)%2?1:0; R z;
  case V1011: u=memchr(v,C1,m); *x=(u?u-v:m)%2?0:1; R z;
  case V0100: *x= *(v+m-1)&&!memchr(v,C1,m-1)?1:0; R z;
  case V1101: *x=!*(v+m-1)&&!memchr(v,C0,m-1)?0:1; R z;
  case V0110: b=0; DO(m, b=b!=*v++); *x=b; R z;
  case V1001: b=1; DO(m, b=b==*v++); *x=b; R z;
 }
 switch(p->id){I*x,*xx;
  case CPLUS:
   RZ(z=cvt(INT,z)); x=AV(z);
   if(1==c){d=0; DO(m, if(*v++)d++); *x=d;}
   else{xx=x+=c; v+=c*(m-1); DO(m-1, DO(c, --x; *x=*--v+*x); x=xx);}
Esempio n. 2
0
Collision::Collision(PObject* obj1, PObject* obj2)
{
	trueCollision = false;
	
	objects = new PObject*[2];
	objects[0] = obj1;
	objects[1] = obj2;
	
	/*
	 * the following was written before the Vect2D class was written
	 * (and that's why it's so messy)
	 */
	Vect2D norm;
	Point p1,p2;
	
	const Point* obj1vertices = objects[0]->getVertices();
	const Point* obj2vertices = objects[1]->getVertices();
	int numPoints=0;
	float sumx=0;
	float sumy=0;
	float parametric;
	float parametric2;
	float denominator;
	
	float vec1x;
	float vec1y;
	float vec1deltax;//between this point and the next one in the shape (going counterclockwise)
	float vec1deltay;
	
	float vec2x;
	float vec2y;
	float vec2deltax;
	float vec2deltay;
	
	float deltax, deltay; //between the two points on different shapes
	
	/*
	 * x1+k*t = x2+c*k
	 * y1+b*t = y2+d*k
	 */
	
	for (int i = 0; i < objects[0]->getNumVertices(); i++)
	{
		vec1x = obj1vertices[i].x;//x1
		vec1y = obj1vertices[i].y;//y1
		
		vec1deltax = obj1vertices[(i + 1) % objects[0]->getNumVertices()].x - vec1x;//a
		vec1deltay = obj1vertices[(i + 1) % objects[0]->getNumVertices()].y - vec1y;//b
		
		for (int j = 0; j < objects[1]->getNumVertices(); j++)
		{
			vec2x = obj2vertices[j].x;//x2
			vec2y = obj2vertices[j].y;//y2
			
			vec2deltax = obj2vertices[(j + 1) % objects[1]->getNumVertices()].x - vec2x;//c
			vec2deltay = obj2vertices[(j + 1) % objects[1]->getNumVertices()].y - vec2y;//d
			
			denominator = (vec1deltax * vec2deltay) - (vec2deltax * vec1deltay);//ad-bc
			if (denominator != 0)
			{
				deltax = vec1x-vec2x;//x1-x2
				deltay = vec1y-vec2y;//y1-y2
				
				parametric = ((vec2deltax * (deltay)) - vec2deltay * (deltax)) / (denominator);//t
				parametric2 = ((vec1deltax * (deltay)) - vec1deltay * (deltax)) / (denominator);//k

				if (parametric <= 1 && parametric >= 0 && parametric2 <= 1 && parametric2 >= 0)
				{
					numPoints++;
					sumx += vec1x + (parametric * vec1deltax);//x = x1+a*t
					sumy += vec1y + (parametric * vec1deltay);//y = y1+b*t
					//uses the normal vector of the first collision
					if (!trueCollision){
						/* old way of finding
						//figure out which one collided on the corner
						if (abs(parametric2-.5) < abs(parametric-.5)){
							//vector1's point of collision was closer to the corner
							norm.set(vec2deltay,-vec2deltax);
						}
						else
						{
							norm.set(vec2deltax,-vec2deltay);
						}*/
						//VERY crude approximation for normal vector at collision point
						norm.set(objects[1]->get_centerx()-objects[0]->get_centerx(), objects[1]->get_centery()-objects[0]->get_centery());
						norm.normalize();
					}
					trueCollision = true;
				}
			}
		}
	}
	if (trueCollision)
	{
		intersection.x = sumx/numPoints;
		intersection.y = sumy/numPoints;
		//calculate impulse here
		float e = objects[0]->get_elasticity()*objects[1]->get_elasticity();
		Vect2D r1(intersection.x-objects[0]->get_centerx(), intersection.y-objects[0]->get_centery());
		Vect2D r2(intersection.x-objects[1]->get_centerx(), intersection.y-objects[1]->get_centery());
		//vap = vcenter + w x r1 where w is angular velocity vector that points into screen
		//vap = velocity of point of collision on each object
		Vect2D vap(objects[0]->get_vx()-objects[0]->get_dtheta()*r1.y, objects[0]->get_vy()+objects[0]->get_dtheta()*r1.x);
		Vect2D vbp(objects[1]->get_vx()-objects[1]->get_dtheta()*r2.y, objects[1]->get_vy()+objects[1]->get_dtheta()*r2.x);
		Vect2D& vabp = vap-vbp;
		float ran = r1.cross(norm);
		float rbn = r2.cross(norm);
		//source: http://www.myphysicslab.com/collision.html
		float j= (-(1+e)*vabp.dot(norm))/(1/objects[0]->get_mass() + 1/objects[1]->get_mass() + ran*ran/objects[0]->get_momentInertia() + rbn*rbn/objects[1]->get_momentInertia());
		
		impulse.set(norm);
		impulse.scale(j);
	}
}