Quantcast

Jump to content

» «
Photo

Apply Forces and Momentums to Entity/Object

3 replies to this topic
LeFix
  • LeFix

    Burning Tube Man

  • Members
  • Joined: 13 Jul 2015
  • Germany

#1

Posted A week ago Edited by LeFix, A week ago.

I recently looked at the existing documentations and descriptions about these natives: (15.05.17)

http://www.dev-c.com...8ff00fc7eff559e

known as APPLY_FORCE_TO_ENTITY_CENTER_OF_MASS

// 0x18FF00FC7EFF559E 0x28924E98

http://www.dev-c.com...5f68be9613e2d18

known as APPLY_FORCE_TO_ENTITY

// 0xC5F68BE9613E2D18 0xC1C0855A

https://github.com/c...pting/Entity.cs

(ScriptHookVDotNet)

There are some mistakes and the descriptions are confusing.

As far as I know this is right:

APPLY_FORCE_TO_ENTITY_CENTER_OF_MASS(Entity entity, int forceFlags, float x, float y, float z, BOOL p5, BOOL isDirectionRel, BOOL isAmountRel, BOOL p8)
{ invoke<Void>(0x18FF00FC7EFF559E, entity, forceType, x, y, z, p5, isDirectionRel, isAmountRel, p8); }

APPLY_FORCE_TO_ENTITY(Entity entity, int forceFlags, float x, float y, float z, float offsetX, float offsetY, float offsetZ, int boneIndex, BOOL isDirectionRel, BOOL ignoreUpVec, BOOL isAmountRel, BOOL p12, BOOL p13)
{ invoke<Void>(0xC5F68BE9613E2D18, entity, forceFlags, x, y, z, offsetX, offsetY, offsetZ, boneIndex, isDirectionRel, ignoreUpVec, isAmountRel, p12, p13); }

forceFlags
the only "documentation" about this is from ScriptHookVDotNet
public enum ForceType
{
        MinForce,
        MaxForceRot,
        MinForce2,
        MaxForceRot2,
        ForceNoRot,
        ForceRotPlusForce
}


Imo it doesn't make any sense because these are bitflags.
Only the three lowest bits work, if higher bits are set to 1 my script didn't apply any forces.

First bit (lowest): Strong force Flag, seems to be a simple multiplier
Second bit: Unkown Flag (Didn't see a difference, but didn't stop working compared to higher bits)
Third bit: Momentum Flag=1 (the vector (x,y,z) is a momentum and not a force anymore)

 

offsetX, offsetY, offsetZ
These parameters are frequently found as xRot, yRot, zRot but are the offset of the force attack point.
For solid bodies there's no such thing like a attack point for momentums (called free momentum in german) so the offset is needless then.

isDirectionRel determines if the direction of the force vector is in global/world coordinates or in relative coordinates (e.g. the thrust from a jet engine is body fixed)

ignoreUpVec could someone explain this one for me, any code I've found sets this TRUE-

isAmountRel enables mass multiplier

In other words if this is set to FALSE, the force has a constant value (Newton) for all objects. A heavy object will accelerate slower than a light object.

If this is set to TRUE the force will be multiplied with the mass, so as a result the acceleration is a constant value for all objects.

p8 (Center of mass function) whenever I set this to TRUE my scripts stops running. -> Always false (at least for me)

p5 (Center of mass function) didn't see a difference if set to TRUE or FALSE, (didn't check for complex entity like peds with mutliple bones)

p12 is always set to FALSE, didn't test it.

p13 is always set to TRUE, didn't test it as well.

 

Please share your knowledge! (Imo these are very important methods for many modders)

  • ikt, CamxxCore, kagikn and 3 others like this

LeFix
  • LeFix

    Burning Tube Man

  • Members
  • Joined: 13 Jul 2015
  • Germany

#2

Posted A week ago

I have a related thread about object physics here:

http://gtaforums.com...-object-physics


LeFix
  • LeFix

    Burning Tube Man

  • Members
  • Joined: 13 Jul 2015
  • Germany

#3

Posted A week ago Edited by LeFix, A week ago.

Momentums behave super weird, I don't understand what's happening behind the scences.

Applying an arbitrary momentum to an object often leads to a pendulum-like motion. (Rotation gets faster then stops and spins backwards again and so on)

This occurs with global and local momentum (isDirRel setting), but maybe my description is (partly) wrong for momentums or there's another relevant parameter.

I guess I have to implement them as pairs of opposite forces.

 

Edit:

Meanwhile I implemented the workaround and it works absolutely fine for body fixed momentums.

void ENTITY::APPLY_FORCE_TO_ENTITY_CENTER_OF_MASS(Entity entity, Vektor3D force, bool isDirRel, bool isStrong, bool isMassRel)
{
	int forceFlags = 0u;
	if (isStrong) forceFlags |= (1u << 0); //Set first bit

	//LAST BOOL HAS TO BE FALSE (SCRIPT STOPS RUNNING)
	ENTITY::APPLY_FORCE_TO_ENTITY_CENTER_OF_MASS(entity, forceFlags, force.x, force.y, force.z, FALSE, isDirRel, isMassRel, FALSE);
}
void ENTITY::APPLY_FORCE_TO_ENTITY(Entity entity, Vektor3D force, Vektor3D offset, int boneIndex, bool isDirRel, bool isStrong, bool isMassRel)
{
	int forceFlags = 0u;
	if (isStrong) forceFlags |= (1u << 0); //Set first bit

	ENTITY::APPLY_FORCE_TO_ENTITY(entity, forceFlags, force.x, force.y, force.z, offset.x, offset.y, offset.z, boneIndex, isDirRel, TRUE, isMassRel, FALSE, TRUE);
}
void ENTITY::APPLY_MOMENTUM_TO_ENTITY(Entity entity, Vektor3D momentum, int boneIndex, bool isStrong, bool isMassRel)
{
	//This method has no isDirRel setting!
	//For the workaround I would have to pass or read the objects rotation to calculate the global force pair.
	//And I don't need global momentums for my mod...

	float momSqr = momentum.getNormSquared();

	//Why is a zero momentum vector stopping body rotation?
	//Following code produces zero vectors (force and offset) which shouldn't stop object roation.
	if (momSqr < 0.0001f*0.0001f) return; //Workaround: Don't apply forces at all when momentum is near zero.


	// FIRST: get an arbitrary perpendicular vector to momentum
	Vektor3D offset = Vektor3D(0.0f, momentum.z, -momentum.y); //Crossproduct with (1/0/0)

	//Check if (1/0/0) was a good choice (almost parallel is bad)
        //0.7f (~1/sqrt(2)) close to zero has probably a better performance
	if (offset.getNormSquared() < 0.7f*momSqr) {
		offset = Vektor3D(-momentum.z, 0.0f, momentum.x); //Crossproduct with (0/1/0) has to be better than (1/0/0)
	}
	offset.normalize();


	// SECOND: calculate the force vector(s)
	Vektor3D force = momentum.cross(offset);


	// THIRD: force flags, momentums behave strange can't properly use them yet
	int forceFlags = 0u;
	if (isStrong) forceFlags |= (1u << 0); //Set first bit
	//forceFlags |= (1u << 2); //Set third bit


	// FOURTH: Apply Forces which are physically the same as the desired momentum
	ENTITY::APPLY_FORCE_TO_ENTITY(entity, force*(+1.0f), offset*(+0.5f), boneIndex, true, isStrong, isMassRel);
	ENTITY::APPLY_FORCE_TO_ENTITY(entity, force*(-1.0f), offset*(-0.5f), boneIndex, true, isStrong, isMassRel);
	//ENTITY::APPLY_FORCE_TO_ENTITY_CENTER_OF_MASS(entity, forceFlags, momentum.x, momentum.y, momentum.z, FALSE, isDirRel, isMassRel, FALSE); //y u no work
}

LeFix
  • LeFix

    Burning Tube Man

  • Members
  • Joined: 13 Jul 2015
  • Germany

#4

Posted A week ago Edited by LeFix, A week ago.

I checked the isStrong force flag and so far it looks like it its a simple factor which is applied. -> 100

Two identical objects should have the same acceleration wih these settings:

Object A -> force:1, isStrong:false

Object B -> force:0.01, isStrong: true

 

Edit:

The differences are really small and they vary during multiple runs. (Randomly)

When I do the same with two identical objects and apply identical forces (isStrong setting as well) there are some small velocity differences too(!)

(Something like floating point errors I assume)

 

I also checked the collision behavior by applying opposite forces to two identical objects which push them against each other in air.

(One force isStrong=true multiplied with 0.01f, the other isStrong=false)

Even then there's no real difference, they just stay at the same spot)

 

Thought the force calculations might be in a specific order so two different force types interact in a specific way, no they don't.

 

Unit of the applied force?

I already mentioned (other thread?) that objects (with no drag) will fall with an acceleration of 10m/s^2 (gravity setting 1.0=default)

This is 10 Newtons/Kilogram

First i thought applying a force (x=0;y=0;z=-10; isStrong=false, isMassRel=true) would accelerate any object the same way like the inbuilt gravity does, but this only accelerates them with 5m/s^2!

So the vector used for applying a force to an entity has the unit 0.5Newtons(/Kilograms if isMassRel=true);

  • ikt likes this




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users