R2Point operator/(const R2Point& point, double a) { assert(a != 0); return R2Point(point.X() / a, point.Y() / a); }
R2Line:: R2Line(const R2Point& point, const R2Vector& vector, RNBoolean normalized) : vector(vector) { if (!normalized) this->vector.Normalize(); normal = R2Vector(this->vector.Y(), -(this->vector.X())); c = -(normal.X()*point.X() + normal.Y()*point.Y()); }
R2Line:: R2Line(const R2Point& point1, const R2Point& point2) : vector(point2 - point1) { this->vector.Normalize(); normal = R2Vector(this->vector.Y(), -(this->vector.X())); c = -(normal.X()*point1.X() + normal.Y()*point1.Y()); }
R2Point operator*(const R3Matrix& a, const R2Point& p) { // Multiply matrix by point RNCoord x = a.m[0][0] * p.X() + a.m[0][1] * p.Y() + a.m[0][2]; RNCoord y = a.m[1][0] * p.X() + a.m[1][1] * p.Y() + a.m[1][2]; return R2Point(x, y); }
void R2Line:: Mirror(const R2Line& line) { // Mirror line over another line R2Point p = (normal * -c).Point(); p.Mirror(line); vector.Mirror(line); normal = R2Vector(vector.Y(), -(vector.X())); c = -(normal.X()*p.X() + normal.Y()*p.Y()); }
RNLength R2Distance(const R2Point& point, const R2Box& box) { // Find axial distances from point to box RNLength dx, dy; if (RNIsGreater(point.X(), box.XMax())) dx = point.X() - box.XMax(); else if (RNIsLess(point.X(), box.XMin())) dx = box.XMin()- point.X(); else dx = 0.0; if (RNIsGreater(point.Y(), box.YMax())) dy = point.Y() - box.YMax(); else if (RNIsLess(point.Y(), box.YMin())) dy = box.YMin()- point.Y(); else dy = 0.0; // Return distance between point and closest point in box if (dy == 0.0) return dx; else if (dx == 0.0) return dy; else return sqrt(dx*dx + dy*dy); }
R2Point operator*(const R2Point& point, double a) { return R2Point(point.X() * a, point.Y() * a); }
R2Point operator-(const R2Point& point, const R2Vector& vector) { return R2Point(point.X() - vector.X(), point.Y() - vector.Y()); }
R2Point operator-(const R2Point& point) { return R2Point(-point.X(), -point.Y()); }
void R2Line:: Reposition(const R2Point& point) { // Set point on line c = -(normal.X()*point.X() + normal.Y()*point.Y()); }
void Rails::Update(double dt) { time -= dt; if (time < 0) { int i = -1; int j = -1; time += 1.0/MOVE_SPEED*(double)GetWidth()/TERRAIN_DPS;//*GetWidth()/(double)TERRAIN_SIZE;//TERRAIN_SIZE/(double)TERRAIN_DPS;//*coeff;//(double)GetWidth()*coeff; float greatest = 0; R2Point previousLocation = oldLocation; R2Point firstLocation = currentLocation; R2Point secondLocation = currentLocation; for (int k = 0; k < 5; k++) { float greatest = 0; for (i = -1; i <= 1; i++) { for (j = -1; j <= 1; j++) { if (i == 0 && j == 0) continue; if (params.railMap->Pixel(currentLocation.X()+i,currentLocation.Y()+j)[0] > greatest) { R2Point tempLocation = R2Point(currentLocation.X()+i,currentLocation.Y()+j); if (tempLocation.X() != oldLocation.X() || tempLocation.Y() != oldLocation.Y()) { if (tempLocation.X() != oldoldLocation.X() || tempLocation.Y() != oldoldLocation.Y()) { greatest = params.railMap->Pixel(currentLocation.X()+i,currentLocation.Y()+j)[0]; nextLocation = tempLocation; } } } } } if (k==0) secondLocation = nextLocation; oldoldLocation = oldLocation; oldLocation = currentLocation; currentLocation = nextLocation; } for (i = -1; i <= 1; i++) { for (j = -1; j <= 1; j++) { if (i == 0 && j == 0) continue; if (params.railMap->Pixel(currentLocation.X()+i,currentLocation.Y()+j)[2] > .5) { globals.levelStatus = 1; globals.gsmgr->Stop(); } } } R3Vector v = R3Vector(-(currentLocation.X()-firstLocation.X()), 0, currentLocation.Y()-firstLocation.Y()); targetDirection = acos(-R3zaxis_vector.Dot(v)/v.Length()); targetDirection = targetDirection - (int)(targetDirection/6.28); if (v.X() < 0) { targetDirection = 6.28-targetDirection; } oldoldLocation = previousLocation; oldLocation = firstLocation; currentLocation = secondLocation; } //double coeff = (sqrt(2) - 1)*abs((firstLocation.X()-currentLocation.X())*(currentLocation.Y()-firstLocation.Y())) + 1; //printf("coeff %f\n", coeff); float closeness = 0; if (targetDirection < 0) closeness = (-currentDirection + targetDirection); else closeness = (-currentDirection + targetDirection); currentDirection += ROLL_SPEED*dt * closeness;//abs(closeness)*closeness; if (abs(currentDirection - targetDirection) > .05) { /* if (currentDirection < targetDirection) currentDirection += ROLL_SPEED*dt; if (currentDirection > targetDirection) currentDirection -= ROLL_SPEED*dt; */ } //currentDirection = targetDirection;//abs(closeness)*closeness; globals.player->SetDirection(currentDirection); }
RNLength R2Distance(const R2Point& point, const R2Line& line) { // Return distance from point to line RNLength d = point.X() * line.A() + point.Y() * line.B() + line.C(); return (d < 0.0) ? -d : d; }
RNLength R2SignedDistance(const R2Line& line, const R2Point& point) { // Return signed distance from point to line return (point.X()*line.A() + point.Y()*line.B() + line.C()); }