Hello everyone,
I'm trying to do a little bit of raycasting in C#, to check whether my player is currently standing on top of a Plattform and is ready to jump.
sadly, I can't find anything detailed about raycasting anywhere. I've searched this forum, as well as the C# Api.
I found this post about it, which seemed promising, but the attached template a user kindly put together is no longer available, and I don't really understand what's going on in the first answer.
Does someone know how to raycast in C#?
kind regards,
Brotato
Re: Raycasting in C#
#2I tried to do some tests (based on Third Person Shooter template).
Result as expected. The red line is the direction of the raycast, and the green ball is the point of intersection.
Result is very strange and sometimes does not work at all
Also, I did not find a way to determine the entity with which the ray intersected.
All my code from Player.cs
Code: Select all
private void Raycast(Vector3 start, Vector3 dir, float range)
{
Vector3 end = start + dir * range;
CryEngine.Debugging.DebugDraw.Line(start, end, new Color(1, 0, 0), 10f);
uint s_rayFlags = unchecked((uint)((int)geom_flags.geom_colltype_ray << (int)rwi_flags.rwi_colltype_bit) | (int)rwi_flags.rwi_colltype_any | (10 & (int)rwi_flags.rwi_pierceability_mask) | ((int)geom_flags.geom_colltype14 << (int)rwi_flags.rwi_colltype_bit));
IPhysicalWorld.SRWIParams rayParams = new IPhysicalWorld.SRWIParams();
rayParams.org = start;
rayParams.dir = dir * range;
rayParams.objtypes = (int)entity_query_flags.ent_all;
rayParams.flags = s_rayFlags;
rayParams.nMaxHits = 1;
rayParams.hits = new ray_hit();
//IPhysicalEntity pActor = Global.gEnv.pGame.GetIGameFramework().GetClientActor().GetEntity().GetPhysics();
//rayParams.AllocateSkipEnts(1);
//rayParams.SetSkipEnt (0, pActor);
int hits = Global.gEnv.pPhysicalWorld.RayWorldIntersection(rayParams);
if (hits > 0)
{
Vector3 _hit = rayParams.hits.pt;
Vector3 _hitNormal = rayParams.hits.n;
CryEngine.Debugging.DebugDraw.Sphere(_hit, 0.1f, new Color(0, 1, 0), 10.0f);
}
rayParams.DeleteSkipEnts ();
}
Code: Select all
private void Raycast_v2(Vector3 start, Vector3 dir, float range)
{
Vector3 end = start + dir * range;
CryEngine.Debugging.DebugDraw.Line(start, end, new Color(1, 0, 0), 10f);
uint rayFlags = unchecked((uint)((int)geom_flags.geom_colltype_ray << (int)rwi_flags.rwi_colltype_bit) | (int)rwi_flags.rwi_colltype_any | (10 & (int)rwi_flags.rwi_pierceability_mask) | ((int)geom_flags.geom_colltype14 << (int)rwi_flags.rwi_colltype_bit));
ray_hit _hit = new ray_hit();
int result = Global.gEnv.pPhysicalWorld.RayWorldIntersection(start, end, (int)entity_query_flags.ent_all, rayFlags, _hit, 1);
//Global.CryLog("Hit: " + ray_hit.dist.ToString());
if (_hit.dist != -1.0f)
{
CryEngine.Debugging.DebugDraw.Sphere(_hit.pt, 0.1f, new Color(0, 1, 0), 10.0f);
}
}
All my code from Player.cs
Re: Raycasting in C#
#3Thank you very much, lorik_f9!
it helped me to understand a little bit more whats going on in detail. But, I also asked this question in the cryengine slack, and someone pointed me to:
which is a wrapper around your approach, and is perfect for my usecase. Maybe you'll find it helpful as well.
kind regards,
Brotato
it helped me to understand a little bit more whats going on in detail. But, I also asked this question in the cryengine slack, and someone pointed me to:
Code: Select all
Cryengine.Physics.Raycast()
kind regards,
Brotato
Re: Raycasting in C#
#4I missed this :) Now everything is much simpler.Code: Select all
Cryengine.Physics.Raycast()
Code: Select all
private void Raycast_test(Vector3 start, Vector3 dir, float range)
{
Vector3 end = start + dir * range;
Debugging.DebugDraw.Line(start, end, new Color(1, 0, 0), 10f);
Physics.Ray ray = new Physics.Ray();
ray.Direction = dir;
ray.Origin = start;
ray.Length = range;
RaycastHit _hit = new RaycastHit();
Physics.Raycast(ray, out _hit);
if (_hit.Distance != -1f)
{
Debugging.DebugDraw.Sphere(_hit.Point, 0.1f, new Color(0, 1, 0), 10.0f);
if (_hit.Entity != null)
{
Debugging.DebugDraw.Text(_hit.Point, _hit.Entity.Name, 2.0f, new Color(0, 1, 0), 10.0f);
}
}
}