2022-01-26 05:22 EST
 All Projects | FSSCP: wxFRED

View Issue Details
IDProjectCategoryView StatusLast Update
0002664FSSCPmath-relatedpublic2012-11-23 21:14
Reporterniffiwan
Assigned Toniffiwan
PrioritynormalSeveritymajorReproducibilityalways
StatusresolvedResolutionfixed
Product Version3.6.14 RC6
Target Version3.6.16Fixed in Version3.6.15
Summary0002664: shockwaves uses target objects radius to calculate damage
DescriptionWhen calculating the distance at which shockwaves will do damage to a given target, the target of the shockwave is treated as being a sphere. For any model with a shape that is not perfect sphere, this method will have inaccuracies. It's particularly noticeable with the Karuna from Blueplanet2 as the one of the Karunas primary weapons is the Apocalyse missile (which has a shockwave), and the Karuna model is long and thin. Any target of the Karuna that is close and not directly fore/aft of the ship will cause the Karuna to take damage from it's own weapons, even though "logically" the ship is outside the shockwave outer radius.
Steps To ReproduceSome discussion has occurred in this forum post.

http://www.hard-light.net/forums/index.php?topic=80146.msg1615180#msg1615180

I've attached a mission from qwadtep which showcases the issue. In theory none of the Karunas should take damage from their own missiles, however either 2 or 3 do, depending on which Karuna model is in use.
Additional InformationThe obvious solution is to be to be more accurate with the distance calculation. My concern is how this will affect performance, as I believe that collision detection is the current bottleneck in FSO.

One idea would be to calculate a cylinder for shockwave distance calculations - which is still an approximation but could be accurate enough for most cases. I think this would require two more pieces of information, radius & world position we have, we'd need length and orientation of the cylinder. I think I came across a existing function somewhere which will calculate the closest point on a cylinder to a given world position? (of course I can't find said function now...)

Another idea would be to use something similar to the weapon impact calculations, i.e. use the lod0 model with all its polygons. Obviously this gives the most accurate results, but is it too computationally expensive?
TagsNo tags attached.
Attached Files
• test-range.fs2 (14,882 bytes) 2012-06-09 23:53
• mantis_2664.patch (6,003 bytes) 2012-11-19 04:11
```Index: fs2_open/code/ai/aicode.cpp
===================================================================
--- fs2_open/code/ai/aicode.cpp    (revision 9359)
+++ fs2_open/code/ai/aicode.cpp    (working copy)
@@ -1834,28 +1834,7 @@ int is_ignore_object(ai_info *aip, int objnum, int just_the_original = 0)
return 0;
}

-/**
- * Given a ship with bounding box and a point, find the closest point on the bbox
- */
-int get_nearest_bbox_point(object *ship_obj, vec3d *start, vec3d *box_pt)
-{
-	vec3d temp, rf_start;
-	polymodel *pm;
-	pm = model_get(Ship_info[Ships[ship_obj->instance].ship_info_index].model_num);

-	// get start in ship rf
-	vm_vec_sub(&temp, start, &ship_obj->pos);
-	vm_vec_rotate(&rf_start, &temp, &ship_obj->orient);
-
-	// find box_pt
-	int inside = project_point_onto_bbox(&pm->mins, &pm->maxs, &rf_start, &temp);
-
-	// get box_pt in world rf
-	vm_vec_unrotate(box_pt, &temp, &ship_obj->orient);
-
-	return inside;
-}

typedef struct eval_nearest_objnum {
Index: fs2_open/code/ship/ship.cpp
===================================================================
--- fs2_open/code/ship/ship.cpp    (revision 9359)
+++ fs2_open/code/ship/ship.cpp    (working copy)
@@ -61,6 +61,7 @@
#include "ship/shipcontrails.h"
#include "weapon/beam.h"
#include "math/staticrand.h"
+#include "math/fvi.h"
#include "missionui/missionshipchoice.h"
#include "hud/hudartillery.h"
#include "species_defs/species_defs.h"
@@ -17417,3 +17418,32 @@ bool ship_has_sound(object *objp, GameSoundsIndex id)
return true;
}

+/**
+ * Given a ship with bounding box and a point, find the closest point on the bbox
+ *
+ * @param ship_obj Object that has the bounding box (should be a ship)
+ * @param start World position of the point being compared
+ * @param box_pt OUTPUT PARAMETER: closest point on the bbox to start
+ *
+ * @return point is inside bbox, TRUE/1
+ * @return point is outside bbox, FALSE/0
+ */
+int get_nearest_bbox_point(object *ship_obj, vec3d *start, vec3d *box_pt)
+{
+	vec3d temp, rf_start;
+	polymodel *pm;
+	pm = model_get(Ship_info[Ships[ship_obj->instance].ship_info_index].model_num);
+
+	// get start in ship rf
+	vm_vec_sub(&temp, start, &ship_obj->pos);
+	vm_vec_rotate(&rf_start, &temp, &ship_obj->orient);
+
+	// find box_pt
+	int inside = project_point_onto_bbox(&pm->mins, &pm->maxs, &rf_start, &temp);
+
+	// get box_pt in world rf
+	vm_vec_unrotate(box_pt, &temp, &ship_obj->orient);
+
+	return inside;
+}
Index: fs2_open/code/ship/ship.h
===================================================================
--- fs2_open/code/ship/ship.h    (revision 9359)
+++ fs2_open/code/ship/ship.h    (working copy)
@@ -1975,4 +1975,16 @@ bool ship_has_sound(object *objp, GameSoundsIndex id);
*/
int get_default_player_ship_index();

+/**
+ * Given a ship with bounding box and a point, find the closest point on the bbox
+ *
+ * @param ship_obj Object that has the bounding box (should be a ship)
+ * @param start World position of the point being compared
+ * @param box_pt OUTPUT PARAMETER: closest point on the bbox to start
+ *
+ * @return point is inside bbox, TRUE/1
+ * @return point is outside bbox, FALSE/0
+ */
+int get_nearest_bbox_point(object *ship_obj, vec3d *start, vec3d *box_pt);
+
#endif
Index: fs2_open/code/weapon/weapons.cpp
===================================================================
--- fs2_open/code/weapon/weapons.cpp    (revision 9359)
+++ fs2_open/code/weapon/weapons.cpp    (working copy)
@@ -5589,7 +5589,7 @@ void weapon_do_electronics_effect(object *ship_objp, vec3d *blast_pos, int wi_in
}

/**
- * Calculate teh damage for an object based on the location of an area-effect
+ * Calculate the damage for an object based on the location of an area-effect
* explosion.
*
* @param objp			Object pointer ship receiving blast effect
@@ -5607,23 +5607,33 @@ void weapon_do_electronics_effect(object *ship_objp, vec3d *blast_pos, int wi_in
*/
int weapon_area_calc_damage(object *objp, vec3d *pos, float inner_rad, float outer_rad, float max_blast, float max_damage, float *blast, float *damage, float limit)
{
-	float			dist, max_dist, min_dist;
+	float dist;
+	vec3d box_pt;
+
+	// if object receiving the blast is a ship, use the bbox for distances
+	// otherwise use the objects radius
+	// could possibly exclude SIF_SMALL_SHIP (& other small objects) from using the bbox
+	if (objp->type == OBJ_SHIP) {
+		int inside = get_nearest_bbox_point(objp, pos, &box_pt);
+		if (inside) {
+			dist = 0.0001f;
+		} else {
+			dist = vm_vec_dist_quick(pos, &box_pt);
+		}
+	} else {
+		dist = vm_vec_dist_quick(&objp->pos, pos) - objp->radius;
+	}

-	dist = vm_vec_dist_quick(&objp->pos, pos);
-	if ( (dist > max_dist) || (dist > (limit+objp->radius)) ) {
+	if ( (dist > outer_rad) || (dist > limit) ) {
return -1;	// spheres don't intersect at all
}

-	if ( dist < (inner_rad+objp->radius) ) {
+	if ( dist < inner_rad ) {
// damage is maximum within inner radius
*damage = max_damage;
*blast = max_blast;
} else {
-		min_dist = dist - objp->radius;
-		Assert(min_dist < outer_rad);
-

// this means the inner and outer radii are basically equal... and since we aren't within the inner radius,
@@ -5635,8 +5645,7 @@ int weapon_area_calc_damage(object *objp, vec3d *pos, float inner_rad, float out
// AL 2-24-98: drop off damage relative to square of distance
*damage = max_damage * dist_to_outer_rad_squared/total_dist_squared;
-
-		*blast =  (min_dist - outer_rad) * max_blast /(inner_rad - outer_rad);
+		*blast =  (dist - outer_rad) * max_blast /(inner_rad - outer_rad);
}

return 0;
```
mantis_2664.patch (6,003 bytes) 2012-11-19 04:11
• mantis_2664_puresvn.patch (5,481 bytes) 2012-11-23 04:11
```Index: code/weapon/weapons.cpp
===================================================================
--- code/weapon/weapons.cpp	(revision 9370)
+++ code/weapon/weapons.cpp	(working copy)
@@ -5589,7 +5589,7 @@
}

/**
- * Calculate teh damage for an object based on the location of an area-effect
+ * Calculate the damage for an object based on the location of an area-effect
* explosion.
*
* @param objp			Object pointer ship receiving blast effect
@@ -5607,23 +5607,33 @@
*/
int weapon_area_calc_damage(object *objp, vec3d *pos, float inner_rad, float outer_rad, float max_blast, float max_damage, float *blast, float *damage, float limit)
{
-	float			dist, max_dist, min_dist;
+	float dist;
+	vec3d box_pt;

-	dist = vm_vec_dist_quick(&objp->pos, pos);
-	if ( (dist > max_dist) || (dist > (limit+objp->radius)) ) {
+	// if object receiving the blast is a ship, use the bbox for distances
+	// otherwise use the objects radius
+	// could possibly exclude SIF_SMALL_SHIP (& other small objects) from using the bbox
+	if (objp->type == OBJ_SHIP) {
+		int inside = get_nearest_bbox_point(objp, pos, &box_pt);
+		if (inside) {
+			dist = 0.0001f;
+		} else {
+			dist = vm_vec_dist_quick(pos, &box_pt);
+		}
+	} else {
+		dist = vm_vec_dist_quick(&objp->pos, pos) - objp->radius;
+	}
+
+	if ( (dist > outer_rad) || (dist > limit) ) {
return -1;	// spheres don't intersect at all
}

-	if ( dist < (inner_rad+objp->radius) ) {
+	if ( dist < inner_rad ) {
// damage is maximum within inner radius
*damage = max_damage;
*blast = max_blast;
} else {
-		min_dist = dist - objp->radius;
-		Assert(min_dist < outer_rad);
-

// this means the inner and outer radii are basically equal... and since we aren't within the inner radius,
@@ -5635,8 +5645,7 @@
// AL 2-24-98: drop off damage relative to square of distance
*damage = max_damage * dist_to_outer_rad_squared/total_dist_squared;
-
-		*blast =  (min_dist - outer_rad) * max_blast /(inner_rad - outer_rad);
+		*blast =  (dist - outer_rad) * max_blast /(inner_rad - outer_rad);
}

return 0;
Index: code/ai/aicode.cpp
===================================================================
--- code/ai/aicode.cpp	(revision 9370)
+++ code/ai/aicode.cpp	(working copy)
@@ -1834,30 +1834,9 @@
return 0;
}

-/**
- * Given a ship with bounding box and a point, find the closest point on the bbox
- */
-int get_nearest_bbox_point(object *ship_obj, vec3d *start, vec3d *box_pt)
-{
-	vec3d temp, rf_start;
-	polymodel *pm;
-	pm = model_get(Ship_info[Ships[ship_obj->instance].ship_info_index].model_num);

-	// get start in ship rf
-	vm_vec_sub(&temp, start, &ship_obj->pos);
-	vm_vec_rotate(&rf_start, &temp, &ship_obj->orient);

-	// find box_pt
-	int inside = project_point_onto_bbox(&pm->mins, &pm->maxs, &rf_start, &temp);

-	// get box_pt in world rf
-	vm_vec_unrotate(box_pt, &temp, &ship_obj->orient);
-
-	return inside;
-}
-
-
typedef struct eval_nearest_objnum {
int	objnum;
object *trial_objp;
Index: code/ship/ship.cpp
===================================================================
--- code/ship/ship.cpp	(revision 9370)
+++ code/ship/ship.cpp	(working copy)
@@ -61,6 +61,7 @@
#include "ship/shipcontrails.h"
#include "weapon/beam.h"
#include "math/staticrand.h"
+#include "math/fvi.h"
#include "missionui/missionshipchoice.h"
#include "hud/hudartillery.h"
#include "species_defs/species_defs.h"
@@ -17435,3 +17436,32 @@
return true;
}

+/**
+ * Given a ship with bounding box and a point, find the closest point on the bbox
+ *
+ * @param ship_obj Object that has the bounding box (should be a ship)
+ * @param start World position of the point being compared
+ * @param box_pt OUTPUT PARAMETER: closest point on the bbox to start
+ *
+ * @return point is inside bbox, TRUE/1
+ * @return point is outside bbox, FALSE/0
+ */
+int get_nearest_bbox_point(object *ship_obj, vec3d *start, vec3d *box_pt)
+{
+	vec3d temp, rf_start;
+	polymodel *pm;
+	pm = model_get(Ship_info[Ships[ship_obj->instance].ship_info_index].model_num);
+
+	// get start in ship rf
+	vm_vec_sub(&temp, start, &ship_obj->pos);
+	vm_vec_rotate(&rf_start, &temp, &ship_obj->orient);
+
+	// find box_pt
+	int inside = project_point_onto_bbox(&pm->mins, &pm->maxs, &rf_start, &temp);
+
+	// get box_pt in world rf
+	vm_vec_unrotate(box_pt, &temp, &ship_obj->orient);
+
+	return inside;
+}
Index: code/ship/ship.h
===================================================================
--- code/ship/ship.h	(revision 9370)
+++ code/ship/ship.h	(working copy)
@@ -1975,4 +1975,16 @@
*/
int get_default_player_ship_index();

+/**
+ * Given a ship with bounding box and a point, find the closest point on the bbox
+ *
+ * @param ship_obj Object that has the bounding box (should be a ship)
+ * @param start World position of the point being compared
+ * @param box_pt OUTPUT PARAMETER: closest point on the bbox to start
+ *
+ * @return point is inside bbox, TRUE/1
+ * @return point is outside bbox, FALSE/0
+ */
+int get_nearest_bbox_point(object *ship_obj, vec3d *start, vec3d *box_pt);
+
#endif
```
mantis_2664_puresvn.patch (5,481 bytes) 2012-11-23 04:11

 Relationships
 Relationships