About Store Forum Documentation Contact



Post Reply 
Need some example code for Physics Ray
Author Message
tipforeveryone Offline
Bronze Supporter

Post: #1
Need some example code for Physics Ray
Here is my needs and code expect, code in tutorial does not provide clear information I need, please give me more clue:

Code:
ArrowClass myArrow;
//ArrowClass derived from Game.Item
//This arrow object which flies at a constant speed and has a ray cast as below
PhysHit hitInfo;
Vec rayStart = myArrow.pos();
Vec rayEnd = myArrow.pos() + myArrow.orn().z * 3;
if(Physics.ray(rayStart, rayEnd, hitInfo)
{
    //Hit detection code here
}

How can I get information of ray contact?
I found ActorInfor in document but don't know how to use it
Hit contact will be an object which is created by using Game.Item
And how to make the ray ignore contact from arrow itself smile

Thanks !
07-27-2020 04:26 AM
Find all posts by this user Quote this message in a reply
Esenthel Offline
Administrator

Post: #2
RE: Need some example code for Physics Ray
You have hitInfo.user and hitInfo.obj
which are Actor.user Actor.obj


For ignoring some actors use
static void ray( &pos, &move, PhysHitCallback &callback , groups=~0);
07-27-2020 05:09 AM
Find all posts by this user Quote this message in a reply
RedcrowProd Offline
Member

Post: #3
RE: Need some example code for Physics Ray
One thing i normally do is ignore all actor but the one i am looking for, this might be world & neighbors actor for example.

World would stop and freeze the actor of the arrow for a certain amount of time if you want to keep it alive

Actor you do damage and get ride of the arrow

I will share some of the code i have for raycasting tomorrow when i am on my computer, but i think you got it almost right, its just a matter of having the * of the obj in the user parameters of the actor, read the type of actor being touched and converting it to the right class by setting a switch enum of actor and then accomplish what you want there smile
(This post was last modified: 07-27-2020 06:26 AM by RedcrowProd.)
07-27-2020 06:25 AM
Find all posts by this user Quote this message in a reply
tipforeveryone Offline
Bronze Supporter

Post: #4
RE: Need some example code for Physics Ray
Thank you guys for quick reply, but it is still kind of confusing for me, give me a look on the code is the best way which can help me to understand. Looking foward !
07-27-2020 07:45 AM
Find all posts by this user Quote this message in a reply
Fex Offline
Gold Supporter

Post: #5
RE: Need some example code for Physics Ray
Code:
ArrowClass myArrow;
Byte AG_ARROW = 12; //can choose any # between 1..15 see ACTOR_GROUP enum
myArrow.actor.group(AG_ARROW);
UInt actor_groups=~(IndexToFlag(AG_ARROW)); //~ makes it ignore those AG
PhysHit hitInfo;
Vec rayStart = myArrow.pos();
Vec rayEnd = myArrow.pos() + myArrow.orn().z * 3;
if(Physics.ray(rayStart, rayEnd, &hitInfo, actor_groups)
{
    //Hit detection code here
}
07-27-2020 08:28 PM
Find all posts by this user Quote this message in a reply
RedcrowProd Offline
Member

Post: #6
RE: Need some example code for Physics Ray
for the hit detection code something similar to this should work:
Code:
if(Physics.ray(rayStart, rayEnd, &hitInfo, actor_groups)
{
       if(hitInfo.group==AG_YOURACTORGROUP)
       {
           YOURCLASSOFACTORGROUP*t_Target = (YOURCLASSOFACTORGROUP*)phys_hit.obj;

              using t_Target you can proceed and use the class hit by raycast
        }
}

dont forget to set .obj(&T).user(&T); to your actor when you create the class that you are looking to get with raycast

something like this

obj.actor.create(*obj.base()->phys(), 1, obj.scale, true).group(AG_YOURACTORGROUP).obj(&T).user(&T);
(This post was last modified: 07-28-2020 12:17 AM by RedcrowProd.)
07-28-2020 12:16 AM
Find all posts by this user Quote this message in a reply
tipforeveryone Offline
Bronze Supporter

Post: #7
RE: Need some example code for Physics Ray
It works, thanks for your support!

But it is a new problem, when I increase "speed" of my arrow (I test for my bullet which is a much faster projectile) if, it is harder for the ray to detect contact.

is there any solution for this ?
07-30-2020 11:14 AM
Find all posts by this user Quote this message in a reply
Esenthel Offline
Administrator

Post: #8
RE: Need some example code for Physics Ray
probably need to make this cover larger distance
Vec rayEnd = myArrow.pos() + myArrow.orn().z * 3;
07-30-2020 12:49 PM
Find all posts by this user Quote this message in a reply
RedcrowProd Offline
Member

Post: #9
RE: Need some example code for Physics Ray
You could do ccd as well i believe ?
Under actor

ccd (); Actor& ccd ( on ); // get/set continuous collision detection

This is normally for high speed detection
(This post was last modified: 07-31-2020 06:46 AM by RedcrowProd.)
07-31-2020 04:23 AM
Find all posts by this user Quote this message in a reply
tipforeveryone Offline
Bronze Supporter

Post: #10
RE: Need some example code for Physics Ray
As my understanding,
ccd (continuous collision detection) is used for actor with physic shape moved by force,
but my case is I am using ray-casting from the tip of the bullet, and the bullet itself move forward using amatrix.
will ccd apply for the ray ? this confused me somehow grin

PS: wow it was a long time until now, I can continue with esenthel
(This post was last modified: 08-11-2020 12:45 AM by tipforeveryone.)
08-11-2020 12:43 AM
Find all posts by this user Quote this message in a reply
RedcrowProd Offline
Member

Post: #11
RE: Need some example code for Physics Ray
Yes you are right actually, ccd would be more for actor based check. Just something to keep in mind if you ever need it smile
08-11-2020 06:08 PM
Find all posts by this user Quote this message in a reply
Post Reply