void Phy::resolveCollisionBoundary(ContactBoundary* c) {
	Circle* circle = c->circle;
	Box* box = c->box;

	Vec2 direction;


	if (circle->velocity.equal(0, 0)) {
		direction = c->normal.normalise().scale(-1);
	}
	else {
		direction = circle->velocity.normalise();
	}

	Vec2 temp;

	while (absf(c->penetration) > 1) {
		if (!direction.equal(0, 0)) {
			Vec2 error = direction.scale(c->penetration * -1);
			circle->position = vecSum(circle->position, error);
		}
		c->normal = calculateNormal(circle, box, &temp);
		c->penetration = circle->radius - sqrt(c->normal.lengthSquared());
	}


	c->normal = c->normal.normalise();

	circle->velocity = vecSum(c->normal.scale(vecDot(c->normal, circle->velocity) * -2), circle->velocity);
}
Ejemplo n.º 2
0
Archivo: widget.cpp Proyecto: 4b4b/CG
void Widget::paintEvent(QPaintEvent *)
{
    int i, nSteps, count;
    double t, tt, scale = ( width()/4 + height()/4 )/6, sum, k;
    double x, y, z, delta = 0.005, dxdt = 1, dydt = 1, tempDelta;

    my_vector4 xyz(0.0, 0.0, 0.0), xyz0(width()/2, height()/2, scale*5.0), xxyyzz(0.0, 0.0, 0.0),
            light_point(0.0, 0.0, scale*13.0), xyz_s(0.0,0.0,0.0), xxyyzz_s(0.0, 0.0, 0.0);

    QPainter p(this);

    if (!dF){
        delta = inpDelta;
        nSteps =  (2*pi) / delta  + 1;
    }
    else{
        delta = 0.05;
        count = 0;
        sum = 0;
        t = 0;
        tt = 0;
        k = 1;
        while (sum < 2*pi){
            dxdt = -inpA * sin( t ) + inpB * inpC * sin ( inpC * t );
            dydt = inpA * cos( t ) - inpB * inpC * cos ( inpC * t );
            tempDelta = delta/maxAbs(dxdt, dydt);
            t += tempDelta;
            sum+=tempDelta;
            count++;
        }
        nSteps = count + 1;
    }

    k = 1;
    t = 0;
    tt = 0;
    dxdt = 1;
    dydt = 1;

    for (i = 0; i < nSteps; i++){

        if(dF){
            dxdt = -inpA * sin( t ) + inpB * inpC * sin ( inpC * t );
            dydt = inpA * cos( t ) - inpB * inpC * cos ( inpC * t );
            k = 1/maxAbs(dxdt, dydt);
        }

        tempDelta = delta * k;
        tt = t + tempDelta;

        x = inpA * cos( t ) - inpB * cos ( inpC * t );
        y = inpA * sin( t ) - inpB * sin ( inpC * t );
        z = cos ( sqrt( x * x + y * y ) );
        t = t + tempDelta;

        xyz.setElem(0, x );
        xyz.setElem(1, y );
        xyz.setElem(2, z );

        x = inpA * cos( tt ) - inpB * cos ( inpC * tt );
        y = inpA * sin( tt ) - inpB * sin ( inpC * tt );
        z = cos ( sqrt( x * x + y * y ) );

        xxyyzz.setElem(0, x );
        xxyyzz.setElem(1, y );
        xxyyzz.setElem(2, z );


        if (flag) {
            rotateX ( xyz , beta);
            rotateX ( xxyyzz, beta );

            rotateY ( xyz, alpha );
            rotateY ( xxyyzz, alpha );
        }

        if (!flag){
            rotateY ( xyz , alpha);
            rotateY ( xxyyzz, alpha );

            rotateX ( xyz, beta );
            rotateX ( xxyyzz, beta );
        }

        xyz.numMul(scale);
        xxyyzz.numMul(scale);

        vecSum(xyz, xyz0);
        vecSum(xxyyzz, xyz0);

        getShadowXY(light_point, xyz, xyz_s);
        getShadowXY(light_point, xxyyzz, xxyyzz_s);

        p.setPen(Qt::darkMagenta);
        p.drawLine( xyz.getElem(0), xyz.getElem(1), xxyyzz.getElem(0), xxyyzz.getElem(1) );

        p.setPen(Qt::darkGray);
        p.drawLine( xyz_s.getElem(0), xyz_s.getElem(1), xxyyzz_s.getElem(0), xxyyzz_s.getElem(1) );

    }
}
Ejemplo n.º 3
0
int imProcessBrinkThreshold(const imImage* image, imImage* dest, bool white_is_255 )
{
	imImage *src = imImageDuplicate( image );
     
	if ( !white_is_255 )
		imProcessNegative( src, src );
	
	int i, j;
	int Topt = 0;

	double p[MAX_GRAY];				// pmf (i.e. normalized histogram)
	unsigned long histo[MAX_GRAY];			// from imlib: "Histogram is always 256 positions long"

	double m_f[MAX_GRAY];			// first foreground moment
	double m_b[MAX_GRAY];			// first background moment

	double tmp0[MAX_GRAY][MAX_GRAY];			
	double tmp1[MAX_GRAY][MAX_GRAY];			
	double tmp3[MAX_GRAY][MAX_GRAY];			
	double tmp4[MAX_GRAY][MAX_GRAY];			

	double tmpVec1[MAX_GRAY];
	double tmpVec2[MAX_GRAY];

	imCalcGrayHistogram(src, histo, NON_CUMULATIVE);  	// gray histogram computed

	double invHistSum = 1.0 / vecSum(histo, MAX_GRAY);	// inverse of the sum

	for (i = 0; i < MAX_GRAY; ++i)
		p[i] = histo[i] * invHistSum;		// equivalent to dividing by the sum, but faster!


	m_f[0] = 0.0;
	for (i = 1; i < MAX_GRAY; ++i)			// m_f = cumsum(g .* p).'; 
		m_f[i] = i * p[i] + m_f[i - 1];

	memcpy(m_b, m_f, VEC_DBL_SZ);		// m_b = m_f(end) - m_f; 

	for (i = 0; i < MAX_GRAY; ++i)
		m_b[i] = m_f[MAX_GRAY - 1] - m_b[i];		

	/****************** END OF BINAR_ASHLEY PORTION, START OF ENTROPY_BRINK********************************/

	for (i = 0; i < MAX_GRAY; ++i)		// tmp0 = m_f_rep ./ g_rep;
	{
		for (j = 0; j < MAX_GRAY; ++j)
		{
			tmp0[i][j] = m_f[j] / i;

			if ((m_f[j] == 0) || (i == 0)) 
			{
				tmp1[i][j] = 0.0;				// replace inf or NaN values with 0
				tmp3[i][j] = 0.0;
			}
			else
			{
				tmp1[i][j] = log(tmp0[i][j]);
				tmp3[i][j] = log(1.0 / tmp0[i][j]);;
			}
			tmp4[i][j] = p[i] * (m_f[j] * tmp1[i][j] + i * tmp3[i][j]);
		}
	}

	diagCumSum(tmpVec1, tmp4);	// tmpVec is now the diagonal of the cumulative sum of tmp4

	// same operation but for background moment, NOTE: tmp1 through tmp4 get overwritten
	for (i = 0; i < MAX_GRAY; ++i)		
	{
		for (j = 0; j < MAX_GRAY; ++j)
		{
			tmp0[i][j] = m_b[j] / i;	// tmpb0 = m_b_rep ./ g_rep;

			if ((m_b[j] == 0) || (i == 0)) 
			{
				tmp1[i][j] = 0.0;				// replace inf or NaN values with 0
				tmp3[i][j] = 0.0;
			}
			else
			{
				tmp1[i][j] = log(tmp0[i][j]);
				tmp3[i][j] = log(1.0 / tmp0[i][j]);;
			}
			tmp4[i][j] = p[i] * (m_b[j] * tmp1[i][j] + i * tmp3[i][j]);
		}
	}

	sumMinusDiagCumSum(tmpVec2, tmp4);		// sum columns, subtract diagonal of cumsum of tmp4 

	for (i = 0; i < MAX_GRAY; ++i)
		tmpVec1[i] += tmpVec2[i];

	Topt = calcTopt(m_f, m_b, tmpVec1);		// DO I NEED TO ADD ONE?

	imProcessThreshold(src, dest, Topt, true);
	imProcessBitwiseNot(dest, dest);		// HACK ALERT: HAVE TO FLIP BITS

	return Topt;
}
void Phy::resolveCollision(Contact* c) {
	Circle* circle = c->circle;
	Box* box = &c->axis.player[c->player];
	box->position = Vec2(c->axis.position.x + c->axis.x_offset, c->axis.position.y + c->axis.player[c->player].y_offset);

	if (!c->axis.collide) return;

	Vec2 direction;

	if (circle->velocity.equal(0, 0)) {
		direction = c->normal.normalise().scale(-1);
	}
	else {
		direction = circle->velocity.normalise();
	}

	if (vecDot(direction, c->normal) > 0) direction = direction.scale(-1); // return;

																		   //std::cout << "OKK\n";

	Vec2 temp; //unused variable, delete from calculateNormal after debugging



	while (absf(c->penetration) > 1) {
		if (!direction.equal(0, 0)) {
			Vec2 error = direction.scale(c->penetration * -1);
			circle->position = vecSum(circle->position, error);
		}

		c->normal = calculateNormal(circle, box, &temp);
		c->penetration = circle->radius - sqrt(c->normal.lengthSquared());
	}

	c->normal = c->normal.normalise();

	Vec2 velAlongNormal = c->normal.scale(vecDot(c->normal, c->axis.velocity));


	//std::cout << c->axis.velocity.x << " " << c->axis.velocity.y << " " << sqrt(c->axis.velocity.lengthSquared()) << " DING\n";
	//std::cout << circle->velocity.x << " " << circle->velocity.y << " " << sqrt(circle->velocity.lengthSquared()) << "BANG\n";

	//std::cout << velAlongNormal.x << " " << velAlongNormal.y << "DOOM\n";

	circle->velocity = vecSum(c->normal.scale(vecDot(c->normal, circle->velocity) * -2), circle->velocity).scale(bounce_ratio);

	circle->velocity = vecSum(circle->velocity, velAlongNormal);

	if (c->normal.y == 0) {
		if (absf(c->axis.angular_velocity) < 10) {
			circle->velocity = circle->velocity.scale((450 + c->axis.angle)*(450 - c->axis.angle) / 202500);
		}
		else {
		//	circle->velocity.y /= 1;
		}
	}

	//std::cout << c->normal.x << " " << c->normal.y << " " << sqrt(c->normal.lengthSquared()) << "BOOM\n";

	//std::cout << circle->velocity.x << " " << circle->velocity.y << " " << sqrt(circle->velocity.lengthSquared()) << "BOING\n";


}
Vec2 Phy::Axis::getPosition() {
	return vecSum(position, Vec2(x_offset, 0));
}