About Store Forum Documentation Contact



Post Reply 
[SOLVED] Need more explain about Ray-casting
Author Message
tipforeveryone Offline
Bronze Supporter

Post: #1
[SOLVED] Need more explain about Ray-casting
I am making my bullet object with ray tracing from the head of bullet object.

Code:
void Hit_Check()
//This function is put in bullet update() function
{
   PhysHit hitInfo;
   Vec rayStart = matrix().pos;      
   Vec rayEnd = matrix().pos + matrix.orn().z * 20 * Time.d();
      
   if(
      Physics.ray(rayStart, rayEnd, &hitInfo, IndexToFlag(AG_GUN)) ||
      Physics.ray(rayStart, rayEnd, &hitInfo, IndexToFlag(AG_TERRAIN))
   ) {
      SoundPlay(UID(xxx)); //Play sound when bullet ray hit something
   }
}

There are 3 objects I wanna test.

1: a box object with box collision shape with actor group = AG_TERRAIN
   

2: a building with static mesh collision shape, actor group = AG_TERRAIN
   

3:a gun with Convex collision shape, actor group = AG_GUN //my custom group
   

and this video show how diffirent bullet with its ray interact with above objects.
Drawn red line is the start/end of ray
I realized that are some differences bettween how ray casting check contact

with AG_TERRAIN, collision occured whenever ray "touch" the object, but with AG_GUN, collision occurred when the ray finishes going throuh the object, at the start of the ray, and building with staticmesh collision shape doesn't let the ray touch its structure inside, ray stop right at the bounding box of building

https://www.youtube.com/watch?v=yMs1DRc8TE0

I just want the ray to check collision right at the moment it touch other objects, and how to solve the problem of that building shape

...sorry for my english, i don't know how good my explaination about my problem...
(This post was last modified: 09-22-2020 03:24 AM by tipforeveryone.)
08-11-2020 07:36 AM
Find all posts by this user Quote this message in a reply
RedcrowProd Offline
Member

Post: #2
RE: Need more explain about Ray-casting
Hi, you shouldnt do 2 raycast but only one with both actor grp, since it is a flag system ( you can add both grp into the flag using actor1 | actor2), obviously the second raycast would override the physhit that you are passing

How are you destroying the actor ?
I think that is probably why you are seeing discrepancy in the behavior
(This post was last modified: 08-11-2020 06:05 PM by RedcrowProd.)
08-11-2020 05:51 PM
Find all posts by this user Quote this message in a reply
tipforeveryone Offline
Bronze Supporter

Post: #3
RE: Need more explain about Ray-casting
You mean:
Physics.ray(rayStart, rayEnd, &hitInfo, IndexToFlag(AG_GUN | AG_TERRAIN))
??

I tried this but my bullet ray ignore AG_GUN, it doesnt work, please explain more.
.....
To destroy bullet actor, I make an variable hitDetected = true to mark when bullet ray hits something, then use

REPA(Bullets){
if(Bullets[i].hitDetected) Bullet.remove(i);
//Bullet is a Game.ObjMap
}

There is still unclear my problems about building's static mesh shape vs ray and why ray doesnt interact at the first touch. did you visit my youtube video link ?
(This post was last modified: 08-11-2020 06:26 PM by tipforeveryone.)
08-11-2020 06:26 PM
Find all posts by this user Quote this message in a reply
RedcrowProd Offline
Member

Post: #4
RE: Need more explain about Ray-casting
Index to flag is one func that take an int, you need to do indextoflag(actor1) | indextoflag(actor2) to transform both int to flag then add then together

Code:
void Hit_Check()
//This function is put in bullet update() function
{
   PhysHit hitInfo;
   Vec rayStart = matrix().pos;      
   Vec rayEnd = matrix().pos + matrix.orn().z * 20 * Time.d();
      
   if(
      Physics.ray(rayStart, rayEnd, &hitInfo, IndexToFlag(AG_GUN) |IndexToFlag(AG_TERRAIN)) {
      SoundPlay(UID(xxx)); //Play sound when bullet ray hit something
   }
}

Can you check which actor has been found as a collision ?

it could be messed up with complex shape, but i dont really see why, as this uses physix to check ?
You might need to check the source
(This post was last modified: 08-12-2020 04:54 PM by RedcrowProd.)
08-12-2020 12:01 AM
Find all posts by this user Quote this message in a reply
tipforeveryone Offline
Bronze Supporter

Post: #5
RE: Need more explain about Ray-casting
After one month, I still stuck with this ray-casting problem, but now I find out something new, it is prolem too I guess. Let me explain my situation.

First, check out my code, I will simplize it

Vec p1 = myActor.matrix().pos; //Get position of my bullet
Vec p2 = p1 + myActor.orn().z * someValue; //because actor's local z is normalized so that I multiply it by someValue to make the ray longer
if (Physics.ray(p1, p2, &someHitInfo))
{//code if the ray contacts something}

I expected that the ray will cast from position of my bullet to it forward vector (local Z axis) but something is wrong. Somehow, the ray end point is always on the global Z axis, watch my video below, and see how my bullet interacts with my vehicle

https://youtu.be/QJoPGWt5jXE

And here is my graphic about this problem, I hope this is all because of my bad math comprehension, I am really not good at it

   
(This post was last modified: 09-19-2020 05:24 PM by tipforeveryone.)
09-19-2020 05:21 PM
Find all posts by this user Quote this message in a reply
tipforeveryone Offline
Bronze Supporter

Post: #6
RE: Need more explain about Ray-casting
My bad, I was not careful. Physics.ray()'s second parameter is Vec &move, not the end of the ray like other engines.

my code should be:
if(Physics.ray(p1, p2 - p1, &hitInfo)){...}
09-21-2020 03:00 AM
Find all posts by this user Quote this message in a reply
Post Reply