Thanks for using Compiler Explorer
Sponsors
Jakt
C++
Ada
Analysis
Android Java
Android Kotlin
Assembly
C
C3
Carbon
C++ (Circle)
CIRCT
Clean
CMake
CMakeScript
COBOL
C++ for OpenCL
MLIR
Cppx
Cppx-Blue
Cppx-Gold
Cpp2-cppfront
Crystal
C#
CUDA C++
D
Dart
Elixir
Erlang
Fortran
F#
GLSL
Go
Haskell
HLSL
Hook
Hylo
IL
ispc
Java
Julia
Kotlin
LLVM IR
LLVM MIR
Modula-2
Nim
Objective-C
Objective-C++
OCaml
OpenCL C
Pascal
Pony
Python
Racket
Ruby
Rust
Snowball
Scala
Solidity
Spice
SPIR-V
Swift
LLVM TableGen
Toit
TypeScript Native
V
Vala
Visual Basic
WASM
Zig
Javascript
GIMPLE
Ygen
csharp source #1
Output
Compile to binary object
Link to binary
Execute the code
Intel asm syntax
Demangle identifiers
Verbose demangling
Filters
Unused labels
Library functions
Directives
Comments
Horizontal whitespace
Debug intrinsics
Compiler
.NET (main)
.NET 6.0.110
.NET 6.0.113
.NET 6.0.116
.NET 7.0.100
.NET 7.0.102
.NET 7.0.105
.NET CoreCLR (main)
.NET CoreCLR 7.0
.NET CoreCLR 8.0
.NET Crossgen2 (main)
.NET Crossgen2 6.0
.NET Crossgen2 7.0
.NET Crossgen2 8.0
.NET IL Disassembler (main)
.NET Mono (main)
.NET NativeAOT (main)
Options
Source code
using System; using System.Collections.Generic; public class WorldInstance { public object MonsterCollisionAccelerator { get; set; } = new FixedSpatialHash(); public void UpdateMonsterCoordinates(HotEntityData entity) { } } public static class CollisionHelper { public static bool StaticCircleCollision(float xPos, float yPos, float radius, HotEntityData entity) { float dx = xPos - entity.XPos; float dy = yPos - entity.YPos; float r = radius + entity.Radius; return (dx * dx + dy * dy) >= (r * r); } } public class HotEntityData { public float XPos { get; set; } public float YPos { get; set; } public float XVelocity { get; set; } public float YVelocity { get; set; } public float Radius { get; set; } } public class FixedSpatialHash { private const float GRID_SIZE = 10.0f; private const float INV_GRID_SIZE = 1.0f / GRID_SIZE; private Dictionary<int, List<HotEntityData>> _map = new(); public void GetPossibleNeighboursAndPerformAction(float xPos, float yPos, float searchDistance, Action<List<HotEntityData>> action) { var x0 = Math.Abs((int)((xPos - searchDistance) * INV_GRID_SIZE) * GRID_SIZE); var x1 = (int)((xPos + searchDistance) * INV_GRID_SIZE) * GRID_SIZE; var y0 = Math.Abs((int)((yPos - searchDistance) * INV_GRID_SIZE) * GRID_SIZE); var y1 = (int)((yPos + searchDistance) * INV_GRID_SIZE) * GRID_SIZE; var x = x0; var y = y0; while (x <= x1) { while (y <= y1) { action.Invoke(_map[GenerateHash(x, y)]); y += GRID_SIZE; } x += GRID_SIZE; y = y0; } } private int GenerateHash(float x, float y) => HashCode.Combine(x, y); } public class Program { private const float MAX_MONSTER_RADIUS = 1.0f; public HotEntityData HotEntityData { get; set; } = new(); public void DoMovementAndCollisionTimeStep(WorldInstance world) { var newXpos = HotEntityData.XPos + HotEntityData.XVelocity; var newYpos = HotEntityData.YPos + HotEntityData.YVelocity; var didCollide = false; var func = (List<HotEntityData> x) => { foreach (var neighbour in x) { if (neighbour != HotEntityData) { didCollide |= CollisionHelper.StaticCircleCollision(newXpos, newYpos, HotEntityData.Radius, neighbour); } } }; ((FixedSpatialHash)(world.MonsterCollisionAccelerator)). GetPossibleNeighboursAndPerformAction(newXpos, newYpos, HotEntityData.Radius + MAX_MONSTER_RADIUS, func); if (!didCollide) world.UpdateMonsterCoordinates(this.HotEntityData); } }
Become a Patron
Sponsor on GitHub
Donate via PayPal
Source on GitHub
Mailing list
Installed libraries
Wiki
Report an issue
How it works
Contact the author
CE on Mastodon
About the author
Statistics
Changelog
Version tree