API Question Get relativ x / y distance

Topper

Addon Developer
Addon Developer
Donator
Joined
Mar 28, 2008
Messages
671
Reaction score
32
Points
43
Hello,

I need to get the relative x / y distance (in m) to a target surface point.

Relative means, that if the point is directly in front, then x is 0 and y = distance.
If the point is on the left hand, x = -distance, y = 0 and so on...

But this code will not work so whats wrong with it?

Code:
{
    VECTOR3 v3Loc, v3Rel;
    double r = oapiGetSize(v->GetGravityRef()); // v = oapiGetFocusInterface()
    
    //targetPoint => Position of base pad 4 of brighton beach
    //targetPoint.x = -0.58284397722227876;        // -33.34 (long)
    //targetPoint.y = 0.71729529872535591;         // 41.10 (lat)
    
    OBJHANDLE gb = v->GetGravityRef();
    double r_g = oapiGetSize(gb);
    double r_deg = oapiOrthodome(pos.x,pos.y,targetPoint.x, targetPoint.y);  // pos => position of focus vessel
    double r_m = r_deg * r_g;    //Distance to position in m (works fine)    
    oapiEquToLocal(v->GetHandle(), targetPoint.x, targetPoint.y, r_m, &v3Loc);
    v->Local2Rel(v3Loc, v3Rel);
    sprintf(oapiDebugString(),"Rel x: %f  Rel y: %f  Rel z: %f  distanceToBasePad: %f",v3Rel.x, v3Rel.y, v3Rel.z, r_m);
}
 
Last edited:
It looks like you you are trying to convert equatorial coordinates into local.

If so...

Code:
  VECTOR3 global_pos, local_pos; // Declare Vectors
  OBJHANDLE rBody = GetSurfaceRef () // Get Reference body
  double radius = oapiGetSize (rBody); // Get radius of reference body
  
  double tgt_lat = *whatever*; // target latitude 
  double tgt_lng = *whatever*; // target longitude

  oapiEquToGlobal (rBody, tgt_lng, tgt_lat, radius, &global_pos); // Get equatorial position in clobal coordinate frame
  Global2Local (global_pos, local_pos); // Convert global frame to local
 
Exactly that's it...
 
Ok because I want to the "horizontal downrange offset", I had to do some extentions.

Code:
 	VECTOR3 global_pos, local_pos;
	double r_g = oapiGetSize(v->GetGravityRef()); // v = oapiGetFocusInterface()
	double r_deg = oapiOrthodome(pos.x,pos.y,targetPoint.x, targetPoint.y);	
	oapiEquToGlobal (v->GetGravityRef(), targetPoint.x, targetPoint.y, r_g, &global_pos);
	v->Global2Local (global_pos, local_pos); // Convert global frame to local		
	v->HorizonRot(local_pos,deltaPos); //Performce a rotation to get horizontal offset and North is pointing into z direction
	double heading;
	oapiGetHeading(v->GetHandle(),&heading);	
	double x, z;
	//Performce a 2D rotation so that nose is pointing into z direction
	x =  deltaPos.x * cos(-heading) - deltaPos.z * sin(heading);
	z = -deltaPos.x * sin(-heading) + deltaPos.z * cos(heading);
	deltaPos.x = x;
	deltaPos.z = z;
	distanceToBasePad = r_deg * r_g;	//Distance to target in m
 
Last edited:
Back
Top