public override RayCastHit RayCast(Vector2 origin, Vector2 destination) { if (destination == origin) { return(null); } origin /= 100.0f; destination /= 100.0f; rayOrigin = origin; rayCurBest = float.MaxValue; rayActorHit = null; try { world.RayCast(Nearest, origin, destination); } catch (Exception e) { return(new RayCastHit()); } RayCastHit hit = new RayCastHit(); hit.actor = rayActorHit; hit.worldImpact = hitPosition * 100.0f; hit.worldNormal = hitNormal; hit.Distance = (hit.worldImpact - (origin * 100)).Length(); return(hit); }
public override RayCastHit RayCast(Vector2 origin, Vector2 destination) { if (destination == origin) { return null; } origin /= 100.0f; destination /= 100.0f; rayOrigin = origin; rayCurBest = float.MaxValue; rayActorHit = null; try { world.RayCast(Nearest, origin, destination); } catch (Exception e) { return new RayCastHit(); } RayCastHit hit = new RayCastHit(); hit.actor = rayActorHit; hit.worldImpact = hitPosition * 100.0f; hit.worldNormal = hitNormal; hit.Distance = (hit.worldImpact - (origin*100)).Length(); return hit; }
public void Move(Vector2 dir) { if (dir == Vector2.Zero) { return; // not interested in zero movement } body.IgnoreRayCast = true; Vector2 rayStart = position; if (dir.X > 0) { rayStart.X += dim.X / 2.0f - 1; } else if (dir.X < 0) { rayStart.X -= dim.X / 2.0f - 1.0f; } if (dir.Y > 0) { rayStart.Y += dim.Y / 2.0f - 1.0f; } else if (dir.Y < 0) { rayStart.Y -= dim.Y / 2.0f - 1.0f; } RayCastHit hit = world.RayCast(position, rayStart + dir); if (hit != null) { if (hit.actor == null) { body.Position += dir; } else { Vector2 hitPoint = hit.worldImpact; Vector2 targetPos = rayStart + dir; dir = targetPos - hitPoint; float leftPart = hitPoint.X - dim.X / 2.0f; float rightPart = hitPoint.X + dim.X / 2.0f; float leftOverlap = leftPart - hitPoint.X; float rightOverlap = rightPart - hitPoint.X; body.Position += dir;// hitPoint;// -new Vector2(dim.X / 2.0f, 0);// dir; // body.PosX = leftPart; dir *= 1.0001f; if (hitPoint.X > (rayStart + dir).X && dir.X < 0) { RayCastHit sidehit = world.RayCast(rayStart, new Vector2(dir.X, 0) + rayStart); if (sidehit.actor != null) { body.PosX = sidehit.worldImpact.X + dim.X / 2.0f; } } else if (hitPoint.X < (rayStart + dir).X && dir.X > 0) { RayCastHit sidehit = world.RayCast(rayStart, new Vector2(dir.X, 0) + rayStart); if (sidehit.actor != null) { body.PosX = sidehit.worldImpact.X - dim.X / 2.0f; } } if (hitPoint.Y > (rayStart + dir).Y && dir.Y < 0) { RayCastHit sidehit = world.RayCast(rayStart, new Vector2(0, dir.Y) + rayStart); if (sidehit.actor != null) { body.PosY = sidehit.worldImpact.Y + dim.Y / 2.0f; } } else if (hitPoint.Y < (rayStart + dir).Y && dir.Y > 0) { RayCastHit sidehit = world.RayCast(rayStart, new Vector2(0, dir.Y) + rayStart); if (sidehit.actor != null) { body.PosY = sidehit.worldImpact.Y - dim.Y / 2.0f; } } } } position = body.Position; }