About Store Forum Documentation Contact



Post Reply 
How to properly check for collision between actors
Author Message
Outdoordoor Offline
Member

Post: #1
How to properly check for collision between actors
I have a character that shoots projectiles, and I need to detect collision with enemy characters and decrease enemy's health. Right now I'm using this code in the projectile update function:
Code:
PhysHit  physHit;
   int enemyIndex = -1;
   if(Physics.sweep(projActor.box(), direction, &physHit))
   {
      if(physHit.group = EnemyGroup) enemyIndex = (uintptr)physHit.user;
   }
   if(projActor.cuts())
   {
      if(enemyIndex != -1) baseEnemyCont[enemyIndex].UpdateHealth(-10);
      projActor.del();
   }

It is supposed to get index of an enemy in the container and then update that enemy's health by index.

However, this code does not work properly, and it won't work if I add different containers for enemies, so I need another solution.

What would be a better way to implement this?
10-20-2023 06:28 PM
Find all posts by this user Quote this message in a reply
tipforeveryone Offline
Bronze Supporter

Post: #2
RE: How to properly check for collision between actors
Physics.ray() is the go! I use it in my FPS game too, and it works perfectly. Use a ray casting for the direction of flying bullet
10-20-2023 09:19 PM
Find all posts by this user Quote this message in a reply
Outdoordoor Offline
Member

Post: #3
RE: How to properly check for collision between actors
The problem is that my projectiles don't always travel in a straight line. For example, you can throw a projectile in the air, and it will fall onto the enemy because of gravity. In some cases it can even roll around on the ground and damage enemies it touches.

Can raycasts still be used in these cases?
10-21-2023 05:37 AM
Find all posts by this user Quote this message in a reply
Zervox Offline
Member

Post: #4
RE: How to properly check for collision between actors
if you are inheriting from the base game objects, you can do something like

Code:
Game.Obj &obj, Game.Obj &user
{
   if(Chr       *chr  =CAST(Chr      , &obj))chrcontainer.find(chr);
   if(enemy *enemy=CAST(enemy, &obj))enemycontainer.find(enemy);
}
if I remember correctly.
then use that pointer to find it inside the correct container.

there is also the option to use reportContact/reportTrigger and using PhysCutsCallback or PhysHitsCallback
(This post was last modified: 10-22-2023 01:58 AM by Zervox.)
10-22-2023 01:57 AM
Find all posts by this user Quote this message in a reply
Post Reply