function bool CheckCollision - for UT

Discussions about Coding and Scripting
Post Reply
MrLoathsome
Inhuman
Posts: 958
Joined: Wed Mar 31, 2010 9:02 pm
Personal rank: I am quite rank.
Location: MrLoathsome fell out of the world!

function bool CheckCollision - for UT

Post by MrLoathsome »

I decided one of my mutators needed to be spawning randomly sized actors last week.

Looked for function CheckCollision, but could not find it.
I wanted to avoid any loops, or anything with any ForEach ... Visible/Colliding/Radius Actors checks or anything like that.

Decided it should be passed the class name you want to spawn (aC), a DrawScale Multiplier value (DSM), and the location vector (V).
It will return True if there is a collision, False if there is room for that actor to spawn with that size at that location.

So this morning I wrote up this bit:

Code: Select all

function bool CheckCollision(class<Actor> aC, float DSM, Vector V)
{
	local Actor aa;

	aa = Spawn(aC,,,V);
	if (aa != None)
	{
		aa.Destroy();
		aa.DrawScale = aa.DrawScale * DSM;
		SetCollisionSize((aa.CollisionRadius * DSM), (aa.CollisionHeight * DSM));
		V.z -= (aa.CollisionHeight * aa.DrawScale);
		aa.SetLocation(V);
		aa = Spawn(aC,,,V);
		if (aa != None)
		{
			aa.Destroy();
			return false;
		}
	}
	return true;
}
It seems to be working perfect with no spawn lag being noticed in testing.
Since I avoided writing an alternate method, got no way to tell which might be faster....

Just to complicate things a bit, one of the mutators I am using this in, need the check performed
after the actor has been spawned successfully at default size.
Want to adjust its size from 1/3 to 2x default size.

So this is how I call the above function. If no room to grow, it just sets the multiplier to 1.
*Note adjustment on DSM to check for 20% larger than actually needed. Figure that would increase
the odds the monster will be able to move, as well as spawn.

Code: Select all

...
     F = FMax(0.3330, (FRand()*2.0000));  // grab a random value for drawscale multiplier
     if (F > 1.0)  // no need for this check for mini-monsters
     {
     	V = monster.Location;  // save our existing actors location
     	monster.SetLocation(vect(0,0,0));  // move existing actor off to vect(0,0,0) for a sec....
     	if (CheckCollision(monster.Class, (F + (F*0.2)), V)) { F = 1.0; }  // check if room for growth.  if not, set F back to 1.0
     	monster.SetLocation(V);  // put the actor back, and continue with setup
     }
... // rest of pawn setup here....
Any thoughts ?
blarg
User avatar
Feralidragon
Godlike
Posts: 5493
Joined: Wed Feb 27, 2008 6:24 pm
Personal rank: Work In Progress
Location: Liandri

Re: function bool CheckCollision - for UT

Post by Feralidragon »

If you want me to be very honest with you, I do not want to sound harsh or anything, but that code doesn't make much sense, and although apparently "works", it will give a ton of problems.

I would sugest you to look at the Translocator code which does what you want, it checks if it can translocate the player first in a much more cleaner and "native" way.

Here's my sugestion for that code of yours (not sure if it works as I didn't test it, but if you should understand it as it's actually more cleaner and simpler):

Code: Select all

function bool CheckCollision(Actor A, float DSM, vector newLoc)
{
local bool isColliding;
local float CRadius, CHeight;

	CRadius = A.CollisionRadius;
	CHeight = A.CollisionHeight;
	A.SetCollisionSize(A.CollisionRadius * DSM, A.CollisionHeight * DSM);
	isColliding = !A.SetLocation(newLoc);
	A.SetCollisionSize(CRadius, CHeight);
	return isColliding;
}
And as for your "main code" setup:

Code: Select all

F = FMax(0.3330, (FRand()*2.0000));
if (F > 1.0 && CheckCollision(monster, (F + (F*0.2), monster.Location)))
	F = 1.0;
Just a sugestion though. You're thinking in optimization, but actually that's heavy (you spawn and destroy 2 pawns in the same function without any need).
MrLoathsome
Inhuman
Posts: 958
Joined: Wed Mar 31, 2010 9:02 pm
Personal rank: I am quite rank.
Location: MrLoathsome fell out of the world!

Re: function bool CheckCollision - for UT

Post by MrLoathsome »

Feralidragon,

I actually agree 100% with your assessment of my above function. I resorted to a brute-force type approach, and
was sort of surprised it even works. Was even more surprised that it wasn't creating a "ton of problems" as I was pretty
much expecting it too. Nothing in the logs either....

Of course a version without those extra spawns is preferable and should be considerably faster.

Your version above is very similar to a few of my earlier attempts on that, but I think I like the
way you did it much better. ( Because it works ! )

Thanks again for the feedback and the functional function.
blarg
Post Reply