New FBI tag, collisionSphereScale

New FBI tag, collisionSphereScale

Discuss the source code and development of Spring Engine in general from a technical point of view. Patches go here too.

Moderator: Moderators

Post Reply
User avatar
FLOZi
MC: Legacy & Spring 1944 Developer
Posts: 6242
Joined: 29 Apr 2005, 01:14

New FBI tag, collisionSphereScale

Post by FLOZi »

Scales the size of a unit's model's collisionsphere, s3o or 3do (though there is no real need to use it with s3o models, unless you are too lazy to edit the model)

Defaults to 0.5 for aircraft (to maintain backwards compatibility with current mods balance) and 1 for all other units. Many thanks to trepan and tvo.

Code: Select all

Index: ExternalAI/AICallback.cpp

===================================================================

--- ExternalAI/AICallback.cpp	(revision 3391)

+++ ExternalAI/AICallback.cpp	(working copy)

@@ -1080,14 +1080,14 @@

 float CAICallback::GetUnitDefRadius(int def)
 {
 	UnitDef *ud = &unitDefHandler->unitDefs[def];
-	S3DOModel* mdl = modelParser->Load3DO(ud->model.modelpath,ud->canfly?0.5f:1,0);
+	S3DOModel* mdl = modelParser->Load3DO(ud->model.modelpath,ud->collisionSphereScale,0);
 	return mdl->radius;
 }
 
 float CAICallback::GetUnitDefHeight(int def)
 {
 	UnitDef *ud = &unitDefHandler->unitDefs[def];
-	S3DOModel* mdl = modelParser->Load3DO(ud->model.modelpath,ud->canfly?0.5f:1,0);
+	S3DOModel* mdl = modelParser->Load3DO(ud->model.modelpath,ud->collisionSphereScale,0);
 	return mdl->height;
 }
 
Index: Sim/Units/UnitDef.h

===================================================================

--- Sim/Units/UnitDef.h	(revision 3391)

+++ Sim/Units/UnitDef.h	(working copy)

@@ -103,6 +103,7 @@

 	int armorType;
 
 	UnitModelDef model;
+	float collisionSphereScale;
 
 	struct UnitDefWeapon {
 		UnitDefWeapon(std::string name,WeaponDef* def,int slavedTo,float3 mainDir,float maxAngleDif,unsigned int badTargetCat,unsigned int onlyTargetCat,float fuelUse)
Index: Sim/Units/UnitDefHandler.cpp

===================================================================

--- Sim/Units/UnitDefHandler.cpp	(revision 3391)

+++ Sim/Units/UnitDefHandler.cpp	(working copy)

@@ -641,7 +641,9 @@

 	ud.smoothAnim = !!atoi(tdfparser.SGetValueDef("0", "UNITINFO\\SmoothAnim").c_str());
 	ud.canLoopbackAttack = !!atoi(tdfparser.SGetValueDef("0", "UNITINFO\\CanLoopbackAttack").c_str());
 	ud.levelGround = !!atoi(tdfparser.SGetValueDef("1", "UNITINFO\\LevelGround").c_str());
-
+        ud.collisionSphereScale = atof(tdfparser.SGetValueDef(ud.canfly?"0.5f":"1", "UNITINFO\\CollisionSphereScale").c_str());  	//this is a hack to make aircrafts less likely to collide and get hit by nontracking weapons, by default to preserve old behaviour
+	
+	
 	ud.seismicRadius=atoi(tdfparser.SGetValueDef("0", "UNITINFO\\seismicDistance").c_str());
 	ud.seismicSignature=atoi(tdfparser.SGetValueDef("-1", "UNITINFO\\seismicSignature").c_str());
 	if(ud.seismicSignature==-1)
Index: Sim/Units/UnitLoader.cpp

===================================================================

--- Sim/Units/UnitLoader.cpp	(revision 3391)

+++ Sim/Units/UnitLoader.cpp	(working copy)

@@ -273,7 +273,7 @@

 //		unit->mainDamageType=unit->weapons.front()->damageType;
 
 	//unit->model=unitModelLoader->GetModel(ud->model.modelname,side);
-	unit->model = modelParser->Load3DO((ud->model.modelpath).c_str(),ud->canfly?0.5f:1,side); 	//this is a hack to make aircrafts less likely to collide and get hit by nontracking weapons
+	unit->model = modelParser->Load3DO((ud->model.modelpath).c_str(),ud->collisionSphereScale,side);
 	unit->SetRadius(unit->model->radius);
 
 	if(ud->floater)
And the link

Image

Default aircraft 0.5 scale vs 10 scale.
User avatar
ILMTitan
Spring Developer
Posts: 410
Joined: 13 Nov 2004, 08:35

Post by ILMTitan »

Looks good to me, but I will let someone more familiar with this section of code have the final say (e.g. someone else commit it).

P.S. I know that grammatically, the period should be on the inside of the parenthesis (like this.) As a programmer, however, that just looks wrong, and I can't bring my self to do it.
User avatar
FLOZi
MC: Legacy & Spring 1944 Developer
Posts: 6242
Joined: 29 Apr 2005, 01:14

Post by FLOZi »

Cheers. Any idea who is? Tobi?
Tobi
Spring Developer
Posts: 4598
Joined: 01 Jun 2005, 11:36

Post by Tobi »

Well, I'm no expert on that part of the code either - probably only SJ is, Yeha might be - but I'd just commit it if it looks right (which it does) :P
User avatar
smoth
Posts: 22309
Joined: 13 Jan 2005, 00:46

Post by smoth »

what about offset as well? Some times the hitsphere can be offset incorrectly as well.

Nice patch though, I am happy to see someone do it.
User avatar
Argh
Posts: 10920
Joined: 21 Feb 2005, 03:38

Post by Argh »

The offset would be very nice for 3DO mods, where it's being set incorrectly.
User avatar
FLOZi
MC: Legacy & Spring 1944 Developer
Posts: 6242
Joined: 29 Apr 2005, 01:14

Post by FLOZi »

I considered that myself. Seen as there is a demand for it I'll look into it. (never really had the issue myself).
User avatar
Argh
Posts: 10920
Joined: 21 Feb 2005, 03:38

Post by Argh »

It's a pretty big deal for 3DO mods, really. There are a lot of models where the scale or position or both are being set up wrong. This will allow 3DO almost all of the functionality of S3O, although it will never be as pretty.
User avatar
FLOZi
MC: Legacy & Spring 1944 Developer
Posts: 6242
Joined: 29 Apr 2005, 01:14

Post by FLOZi »

Is it just the y position, or full on (x,y,z)?

The code that I *think* is doing it sets the x and z to 0.

edit: Infact, that could be a hack around the problem someone has added. Please check that if you have had x,y,z offset difficulties before that the same behaviour is still happening.

edit2: Unless you actually see some circumstance where you would want non-0 x,y offsets? :?
User avatar
rattle
Damned Developer
Posts: 8278
Joined: 01 Jun 2006, 13:15

Post by rattle »

smoth wrote:what about offset as well? Some times the hitsphere can be offset incorrectly as well.

Nice patch though, I am happy to see someone do it.
An option to use the origin of a piece in combination with offsets would be good.
User avatar
FLOZi
MC: Legacy & Spring 1944 Developer
Posts: 6242
Joined: 29 Apr 2005, 01:14

Post by FLOZi »

rattle wrote:
smoth wrote:what about offset as well? Some times the hitsphere can be offset incorrectly as well.

Nice patch though, I am happy to see someone do it.
An option to use the origin of a piece in combination with offsets would be good.
And considerably less trivial (atleast if you want the hitsphere location to update with the piece's location - and if it doesn't then there's no difference to just setting x,y,z offset globally).
User avatar
rattle
Damned Developer
Posts: 8278
Joined: 01 Jun 2006, 13:15

Post by rattle »

The reason for this is when you want the sphere at the model's 0, 0, 0 coordinate for instance and just offset it in the same manner like s3os.
At the moment I think it uses the model's center as origin which causes troubles for non-symmetrical models.
User avatar
FLOZi
MC: Legacy & Spring 1944 Developer
Posts: 6242
Joined: 29 Apr 2005, 01:14

Post by FLOZi »

rattle wrote:The reason for this is when you want the sphere at the model's 0, 0, 0 coordinate for instance and just offset it in the same manner like s3os.
At the moment I think it uses the model's center as origin which causes troubles for non-symmetrical models.
Ok dokey, x,y,z it is. Now to ponder the best way to do it; alter the current function to accept new arguments or overload it.
User avatar
FLOZi
MC: Legacy & Spring 1944 Developer
Posts: 6242
Joined: 29 Apr 2005, 01:14

Post by FLOZi »

delete this post
User avatar
FLOZi
MC: Legacy & Spring 1944 Developer
Posts: 6242
Joined: 29 Apr 2005, 01:14

Post by FLOZi »

Triple post ftw! :o

Code: Select all

Index: Rendering/UnitModels/3DModelParser.cpp
===================================================================
--- Rendering/UnitModels/3DModelParser.cpp	(revision 3396)
+++ Rendering/UnitModels/3DModelParser.cpp	(working copy)
@@ -28,6 +28,15 @@
 		return unit3doparser->Load3DO(name,scale,side);
 }
 
+S3DOModel* C3DModelParser::Load3DO(string name,float scale,int side,float3 offsets)
+{
+	StringToLowerInPlace(name); // replication sucks i know, but this seems the safest way
+	if(name.find(".s3o")!=string::npos) 
+		return units3oparser->Load3DO(name,scale,side);
+	else
+		return unit3doparser->Load3DO(name,scale,side,offsets);
+}
+
 LocalS3DOModel *C3DModelParser::CreateLocalModel(S3DOModel *model, vector<struct PieceInfo> *pieces)
 {
 	if(model->rootobject3do)
Index: Rendering/UnitModels/3DModelParser.h
===================================================================
--- Rendering/UnitModels/3DModelParser.h	(revision 3396)
+++ Rendering/UnitModels/3DModelParser.h	(working copy)
@@ -72,6 +72,7 @@
 	~C3DModelParser(void);
 
 	S3DOModel* Load3DO(string name,float scale=1,int side=1);
+	S3DOModel* Load3DO(string name,float scale,int side,float3 offsets);
 	LocalS3DOModel *CreateLocalModel(S3DOModel *model, vector<struct PieceInfo> *pieces);
 
 	C3DOParser* unit3doparser;
Index: Rendering/UnitModels/3DOParser.cpp
===================================================================
--- Rendering/UnitModels/3DOParser.cpp	(revision 3396)
+++ Rendering/UnitModels/3DOParser.cpp	(working copy)
@@ -248,6 +248,17 @@
 	return model;
 }
 
+S3DOModel* C3DOParser::Load3DO(string name,float scale,int team, float3 offsets)
+{
+	S3DOModel *model = C3DOParser::Load3DO(name, scale, team);
+	model->rootobject3do->relMidPos.x=offsets.x;
+	model->rootobject3do->relMidPos.y=offsets.y;
+	model->rootobject3do->relMidPos.z=offsets.z;
+	model->relMidPos = model->rootobject3do->relMidPos;
+
+	return model;
+}
+
 void C3DOParser::GetVertexes(_3DObject* o,S3DO* object)
 {
 	curOffset=o->OffsetToVertexArray;
Index: Rendering/UnitModels/3DOParser.h
===================================================================
--- Rendering/UnitModels/3DOParser.h	(revision 3396)
+++ Rendering/UnitModels/3DOParser.h	(working copy)
@@ -94,6 +94,7 @@
 	C3DOParser();
 	virtual ~C3DOParser();
 	S3DOModel* Load3DO(string name,float scale=1,int side=1);
+	S3DOModel* Load3DO(string name,float scale,int side, float3 offsets);
 	LocalS3DOModel *CreateLocalModel(S3DOModel *model, vector<struct PieceInfo> *pieces);
 	
 private:
Index: Sim/Units/Unit.cpp
===================================================================
--- Sim/Units/Unit.cpp	(revision 3396)
+++ Sim/Units/Unit.cpp	(working copy)
@@ -923,6 +923,13 @@
 
 	//model=unitModelLoader->GetModel(model->name,newteam);
 	model = modelParser->Load3DO(model->name.c_str(), 1, newteam);
+
+	if (unitDef->useCSOffset) {
+		model = modelParser->Load3DO(model->name.c_str(),unitDef->collisionSphereScale,newteam, unitDef->collisionSphereOffset);
+	}
+	else {
+		model = modelParser->Load3DO(model->name.c_str(),unitDef->collisionSphereScale,newteam);
+	}
 	delete localmodel;
 	localmodel = modelParser->CreateLocalModel(model, &cob->pieces);
 
Index: Sim/Units/UnitDef.h
===================================================================
--- Sim/Units/UnitDef.h	(revision 3396)
+++ Sim/Units/UnitDef.h	(working copy)
@@ -104,6 +104,8 @@
 
 	UnitModelDef model;
 	float collisionSphereScale;
+	float3 collisionSphereOffset;
+	bool useCSOffset;
 
 	struct UnitDefWeapon {
 		UnitDefWeapon(std::string name,WeaponDef* def,int slavedTo,float3 mainDir,float maxAngleDif,unsigned int badTargetCat,unsigned int onlyTargetCat,float fuelUse)
Index: Sim/Units/UnitDefHandler.cpp
===================================================================
--- Sim/Units/UnitDefHandler.cpp	(revision 3396)
+++ Sim/Units/UnitDefHandler.cpp	(working copy)
@@ -646,12 +646,23 @@
 	ud.smoothAnim = !!atoi(tdfparser.SGetValueDef("0", "UNITINFO\\SmoothAnim").c_str());
 	ud.canLoopbackAttack = !!atoi(tdfparser.SGetValueDef("0", "UNITINFO\\CanLoopbackAttack").c_str());
 	ud.levelGround = !!atoi(tdfparser.SGetValueDef("1", "UNITINFO\\LevelGround").c_str());
-
+		
 	// aircraft collision sizes default to half their visual size, to
 	// make them less likely to collide or get hit by nontracking weapons
 	const char* defScale = ud.canfly ? "0.5" : "1";
 	ud.collisionSphereScale = atof(tdfparser.SGetValueDef(defScale, "UNITINFO\\CollisionSphereScale").c_str());
 
+	float3 cso (0, 0, 0);
+	const char* strCSOffset = tdfparser.SGetValueDef("default", "UNITINFO\\CollisionSphereOffset").c_str();
+	if (sscanf(strCSOffset, "%f %f %f", &cso.x, &cso.y, &cso.z) == 3) { 
+		ud.useCSOffset = true;
+		ud.collisionSphereOffset = cso;
+	}
+	else {
+		ud.useCSOffset = false;
+	}
+
+
 	ud.seismicRadius=atoi(tdfparser.SGetValueDef("0", "UNITINFO\\seismicDistance").c_str());
 	ud.seismicSignature=atoi(tdfparser.SGetValueDef("-1", "UNITINFO\\seismicSignature").c_str());
 	if(ud.seismicSignature==-1)
Index: Sim/Units/UnitLoader.cpp
===================================================================
--- Sim/Units/UnitLoader.cpp	(revision 3396)
+++ Sim/Units/UnitLoader.cpp	(working copy)
@@ -268,7 +268,12 @@
 	if(ud->tidalGenerator>0)
 		unit->energyTickMake += ud->tidalGenerator*readmap->tidalStrength;
 
-	unit->model = modelParser->Load3DO((ud->model.modelpath).c_str(),ud->collisionSphereScale,side);
+	if (ud->useCSOffset) {
+		unit->model = modelParser->Load3DO((ud->model.modelpath).c_str(),ud->collisionSphereScale,side, ud->collisionSphereOffset);
+	}
+	else {
+		unit->model = modelParser->Load3DO((ud->model.modelpath).c_str(),ud->collisionSphereScale,side);
+	}
 	unit->SetRadius(unit->model->radius);
 
 	if(ud->floater)

link.

CollisionSphereOffset=<x> <y> <z>;

Offsets a 3do's (and only a 3do) collision sphere by the given vector.
Post Reply

Return to “Engine”