Beispiel #1
0
//void CEntity::Hit			(float perc, Fvector &dir, CObject* who, s16 element,Fvector position_in_object_space, float impulse, ALife::EHitType hit_type) 
void	CEntity::Hit		(SHit* pHDS)
{

//	if (bDebug)				Log("Process HIT: ", *cName());

	// *** process hit calculations
	// Calc impulse
	Fvector					vLocalDir;
	float					m = pHDS->dir.magnitude();
	VERIFY					(m>EPS);
	
	// convert impulse into local coordinate system
	Fmatrix					mInvXForm;
	mInvXForm.invert		(XFORM());
	mInvXForm.transform_dir	(vLocalDir,pHDS->dir);
	vLocalDir.invert		();

	// hit impulse
	if(pHDS->impulse) HitImpulse				(pHDS->impulse,pHDS->dir,vLocalDir); // @@@: WT
	
	// Calc amount (correct only on local player)
	float lost_health = CalcCondition(pHDS->damage());

	// Signal hit
	if(BI_NONE!=pHDS->bone())	HitSignal(lost_health,vLocalDir,pHDS->who,pHDS->boneID);

	// If Local() - perform some logic
	if (Local() && !g_Alive() && !AlreadyDie() && (m_killer_id == ALife::_OBJECT_ID(-1))) {
		KillEntity	(pHDS->whoID);
	}
	//must be last!!! @slipch
	inherited::Hit(pHDS);
}
void FfbGetFeedbackValue(int16_t* axisPosition, int16_t* out)
{
	int32_t x_y_force[2] = {0,0};
	
	for (uint8_t id = 0; id <= MAX_EFFECTS; id++)
	{
		TEffectState effect = gEffectStates[id];

		if ((effect.state & MEFFECTSTATE_PLAYING) && ((effect.elapsedTime <= effect.duration)||(effect.duration == USB_DURATION_INFINITE)) && !devicePaused)
		{
			switch (effect.effectType)
			{
			case USB_EFFECT_CONSTANT:
				{
					int32_t tforce = ApplyEnvelope(effect, (int32_t)effect.magnitude);
					ApplyDirection(effect, tforce, x_y_force);
				}
				break;
			case USB_EFFECT_RAMP:
				{
					int32_t end = effect.end * 2;
					int32_t start = effect.start * 2;
					uint32_t elapsedTime = effect.elapsedTime;
					int32_t duration = effect.duration;
					
					int32_t temp = (end - start) * elapsedTime;
					temp /= duration;
					temp += start;
					ApplyDirection(effect, temp, x_y_force);
				}
				break;
			case USB_EFFECT_SQUARE:
				{
					int32_t offset = effect.offset * 2;
					uint32_t magnitude = effect.magnitude;
					uint32_t elapsedTime = effect.elapsedTime;
					uint32_t phase = effect.phase;
					uint32_t period = effect.period;

					int32_t max = offset + magnitude;
					int32_t min = offset - magnitude;
					uint32_t phasetime = (phase * period) / 255;
					uint32_t time = elapsedTime + phasetime;
					uint32_t reminder = time % period;
					int32_t tempforce;
					if (reminder > (period / 2)) tempforce = min;
					else tempforce = max;
					ApplyDirection(effect, ApplyEnvelope(effect, tempforce), x_y_force);
				}
				break;
			case USB_EFFECT_SINE:
				{
					float offset = effect.offset * 2;
					float magnitude = effect.magnitude;
					float phase = effect.phase;
					float time = effect.elapsedTime;
					float period = effect.period;
					
					float angle = ((time / period) + (phase / 255) * period) * 2 * PI;
					float sine = FfbSin(angle);
					float tempforce = sine * magnitude;
					tempForce += offset;
					ApplyDirection(effect, ApplyEnvelope(effect, (int32_t)tempforce), x_y_force);
				}
				break;
			case USB_EFFECT_TRIANGLE:
				{
					float offset = effect.offset*2;
					float magnitude = effect.magnitude;
					float elapsedTime=effect.elapsedTime;
					uint32_t phase=effect.phase;
					uint32_t period = effect.period;
					float periodF = effect.period;
					
					float max = offset + magnitude;
					float min = offset - magnitude;
					uint32_t phasetime = (phase * period) / 255;
					uint32_t time = elapsedTime + phasetime;
					float reminder = time % period;
					float slope = ((max - min)*2) / periodF;
					float tempforce = 0;
					if (reminder > (periodF / 2)) tempforce = slope * (periodF - reminder);
					else tempforce = slope * reminder;
					tempforce += min;
					ApplyDirection(effect, ApplyEnvelope(effect, tempforce), x_y_force);
				}

				break;
			case USB_EFFECT_SAWTOOTHDOWN:
				{
					float offset = effect.offset*2;
					float magnitude = effect.magnitude;
					float elapsedTime=effect.elapsedTime;
					float phase=effect.phase;
					uint32_t period = effect.period;
					float periodF = effect.period;
					
					float max = offset + magnitude;
					float min = offset - magnitude;
					int32_t phasetime = (phase * period) / 255;
					uint32_t time = elapsedTime + phasetime;
					float reminder = time % period;
					float slope = (max - min) / periodF;
					float tempforce = 0;
					tempforce = slope * (period - reminder);
					tempforce += min;
					ApplyDirection(effect, ApplyEnvelope(effect, tempforce), x_y_force);
				}

				break;
			case USB_EFFECT_SAWTOOTHUP:
				{
					float offset = effect.offset*2;
					float magnitude = effect.magnitude;
					float elapsedTime=effect.elapsedTime;
					uint32_t phase=effect.phase;
					uint32_t period = effect.period;
					float periodF = effect.period;
					
					float max = offset + magnitude;
					float min = offset - magnitude;
					int32_t phasetime = (phase * period) / 255;
					uint32_t time = elapsedTime + phasetime;
					float reminder = time % period;
					float slope = (max - min) / periodF;
					float tempforce = 0;
					tempforce = slope * reminder;
					tempforce += min;
					ApplyDirection(effect, ApplyEnvelope(effect, tempforce), x_y_force);
				}

				break;
			case USB_EFFECT_SPRING:
				{
					int32_t axis[2] = {NORMALIZE_RANGE(axisPosition[0]),NORMALIZE_RANGE(axisPosition[1])};
					int32_t temp[2]={0,0};
					CalcCondition(effect,temp,axis);
					x_y_force[0] += -temp[0];
					x_y_force[1] += -temp[1];
				}
				break;
			case USB_EFFECT_DAMPER:
				{
					int32_t speed[2] = {NORMALIZE_RANGE(axisPosition[0]) - NORMALIZE_RANGE(oldAxisPosition[0]), NORMALIZE_RANGE(axisPosition[1]) - NORMALIZE_RANGE(oldAxisPosition[1])};
					int32_t temp[2]={0,0};
					CalcCondition(effect,temp,speed);
					x_y_force[0] += -temp[0];
					x_y_force[1] += -temp[1];				
				}
				break;
			case USB_EFFECT_INERTIA:
				{
					int32_t speed[2] = {NORMALIZE_RANGE(axisPosition[0]) - NORMALIZE_RANGE(oldAxisPosition[0]), NORMALIZE_RANGE(axisPosition[1]) - NORMALIZE_RANGE(oldAxisPosition[1])};
					int32_t acceleration[2] = {speed[0] - oldSpeed[0], speed[1] - oldSpeed[1]};
					int32_t temp[2] = {0, 0};
					memset((void*)&effect.positiveSaturation, INERTIA_FORCE, 4);
					memset((void*)&effect.deadBand, INERTIA_DEADBAND, 2);
					CalcCondition(effect,temp,speed);
					inertiaT[0] += temp[0];
					inertiaT[1] += temp[1];
					x_y_force[0] += inertiaT[0];
					x_y_force[1] += inertiaT[1];
					if (speed[0] == 0) inertiaT[0] = 0;
					if (speed[1] == 0) inertiaT[1] = 0; 
					                               
				}
				break;
			case USB_EFFECT_FRICTION:
				{
					int32_t speed[2] = {NORMALIZE_RANGE(axisPosition[0]) - NORMALIZE_RANGE(oldAxisPosition[0]), NORMALIZE_RANGE(axisPosition[1]) - NORMALIZE_RANGE(oldAxisPosition[1])};
					int32_t temp[2] = {0, 0};
					memset((void*)&effect.positiveSaturation, FRICTION_FORCE, 4);
					memset((void*)&effect.deadBand, FRICTION_DEADBAND, 2);
					CalcCondition(effect,temp,speed);
					x_y_force[0] += -temp[0];
					x_y_force[1] += -temp[1];				
				}
				break;
			case USB_EFFECT_CUSTOM:
				break;
			}
		gEffectStates[id].elapsedTime += WHEEL_SAMPLE_RATE_MS;
		}
	}
	
	uint32_t gain = deviceGain.gain;
	for(uint32_t i = 0;i < 2; i++)
	{
	  x_y_force[i] *= gain;
	  x_y_force[i] /= 255;
		out[i] = x_y_force[i] > 255 ? 255 : x_y_force[i];
		out[i] = x_y_force[i] < -255 ? -255 : x_y_force[i];
	}
	
	oldSpeed[0] = axisPosition[0] - oldAxisPosition[0];
	oldSpeed[1] = axisPosition[1] - oldAxisPosition[1];

}