### Initialize Gut Doubled Script Values and Load Tools Source: https://gitlab.com/snopek-games/sg-physics-2d/-/blob/main/projects/godot-4/addons/gut/double_templates/script_template.txt Initializes a dictionary with values required for the Gut Doubled script, including paths, IDs, and singleton names. It then loads and instantiates the 'double_tools.gd' script from the GUT addon with these values. This setup is crucial for enabling the doubling functionality within the testing framework. ```gdscript var __gutdbl_values = { double = self, thepath = '{path}', subpath = '{subpath}', stubber = {stubber_id}, spy = {spy_id}, gut = {gut_id}, from_singleton = '{singleton_name}', is_partial = {is_partial}, } var __gutdbl = load('res://addons/gut/double_tools.gd').new(__gutdbl_values) ``` -------------------------------- ### Using Fixed-Point Math with Constants in Godot Source: https://gitlab.com/snopek-games/sg-physics-2d/-/blob/main/README.md Demonstrates how to use `const` variables for fixed-point numbers in Godot's GDScript to ensure deterministic calculations. This approach is crucial for game logic involving fractional components that require precision. It shows a basic example of multiplying a vector with a fixed-point constant. ```gdscript const ONE_POINT_FIVE = 98304 func _physics_process(delta: float) -> void: var some_var = vector.mul(ONE_POINT_FIVE) ``` -------------------------------- ### SGCharacterBody2D: Deterministic Character Movement and Collision Source: https://context7.com/snopek-games/sg-physics-2d/llms.txt Illustrates how to use SGCharacterBody2D for player-controlled character movement, including horizontal input, gravity, and jumping using fixed-point arithmetic. It covers the use of move_and_slide() for automatic collision response and provides examples of checking collision states like being on the floor, ceiling, or wall. It also shows how to process detailed collision information and an alternative method using move_and_collide() for custom collision handling. ```gdscript extends SGCharacterBody2D const SPEED: int = 196608 # 3.0 in fixed-point const GRAVITY: int = 32768 # 0.5 in fixed-point const JUMP_FORCE: int = 983040 # 15.0 in fixed-point var velocity: SGFixedVector2 func _ready(): velocity = SGFixed.vector2(0, 0) # Set up direction for floor detection (negative Y = up) up_direction = SGFixed.vector2(0, SGFixed.NEG_ONE) func _physics_process(delta: float) -> void: # Handle horizontal input var x_input: int = 0 if Input.is_action_pressed("ui_right"): x_input = SGFixed.ONE elif Input.is_action_pressed("ui_left"): x_input = SGFixed.NEG_ONE velocity.x = SGFixed.mul(x_input, SPEED) # Apply gravity velocity.y += GRAVITY # Handle jump if Input.is_action_just_pressed("ui_up") and is_on_floor(): velocity.y = -JUMP_FORCE # Move with automatic sliding along surfaces move_and_slide() # Check collision state if is_on_floor(): var floor_normal: SGFixedVector2 = get_floor_normal() var floor_angle: int = get_floor_angle() # In fixed-point radians print("On floor, angle: %s" % SGFixed.to_float(floor_angle)) elif is_on_ceiling(): print("Hit ceiling!") elif is_on_wall(): print("Against wall") # Process all collisions from move_and_slide for i in get_slide_count(): var collision: SGKinematicCollision2D = get_slide_collision(i) print("Hit: %s" % collision.collider.name) print("Normal: %s" % collision.normal.to_float()) print("Remainder: %s" % collision.remainder.to_float()) # Alternative: move_and_collide for custom collision handling func custom_movement(motion: SGFixedVector2) -> void: var collision: SGKinematicCollision2D = move_and_collide(motion) if collision: # Manual slide calculation var slide_motion: SGFixedVector2 = collision.remainder.slide(collision.normal) move_and_collide(slide_motion) # Rotation with collision detection func rotate_with_collision(rotation_amount: int) -> void: var collided: bool = rotate_and_slide(rotation_amount) if collided: print("Rotation blocked by collision") ``` -------------------------------- ### Compiling SG Physics 2D from Source (Godot 4) Source: https://gitlab.com/snopek-games/sg-physics-2d/-/blob/main/README.md Provides command-line instructions for compiling the SG Physics 2D project from source for Godot 4 using SCons. This process involves updating Git submodules and then building both debug and release versions of the GDExtension. ```bash git submodule update --init --recursive scons target=template_debug scons target=template_release ``` -------------------------------- ### Compile Godot 3 Release Export Templates with SG Physics 2D Source: https://gitlab.com/snopek-games/sg-physics-2d/-/blob/main/README.md This command compiles the release export templates for Godot 3, incorporating the SG Physics 2D module. Substitute PLATFORM with your target operating system and PATH with the full path to the SG Physics 2D source code. This ensures your exported game includes the custom physics engine. ```bash scons platform=PLATFORM tools=no target=release production=yes custom_modules=/PATH/sg-physics-2d/src ``` -------------------------------- ### Compile Godot 3 Editor with SG Physics 2D Module Source: https://gitlab.com/snopek-games/sg-physics-2d/-/blob/main/README.md This command compiles the Godot 3 editor with the SG Physics 2D module integrated. Replace PLATFORM with your target OS (e.g., windows, osx, x11) and PATH with the absolute path to the SG Physics 2D source code. This process requires the SCons build tool. ```bash scons platform=PLATFORM tools=yes target=release_debug custom_modules=/PATH/sg-physics-2d/src ``` -------------------------------- ### SGFixed Singleton: Fixed-Point Math Utilities (GDScript) Source: https://context7.com/snopek-games/sg-physics-2d/llms.txt Demonstrates the usage of the SGFixed singleton for various fixed-point math operations in GDScript. This includes type conversions, arithmetic, trigonometry, and creation of fixed-point vector and transform types. Note that `from_float` should be avoided in gameplay code due to potential precision issues. ```gdscript extends Node func _ready(): # Constants (fixed-point representation) # SGFixed.ONE = 65536 (represents 1.0) # SGFixed.HALF = 32768 (represents 0.5) # SGFixed.TWO = 131072 (represents 2.0) # SGFixed.NEG_ONE = -65536 (represents -1.0) # SGFixed.PI = 205887 (represents ~3.14159) # SGFixed.TAU = 411774 (represents ~6.28318) # Convert between types (DON'T use from_float in gameplay code!) var fixed_val: int = SGFixed.from_int(5) # 327680 (5 * 65536) var from_float: int = SGFixed.from_float(2.5) # 163840 (2.5 * 65536) var back_to_float: float = SGFixed.to_float(fixed_val) # 5.0 var back_to_int: int = SGFixed.to_int(fixed_val) # 5 # String conversions (useful for loading config data) var from_str: int = SGFixed.from_string("1.5") # 98304 var to_str: String = SGFixed.format_string(98304) # "1.5" # Multiplication and division (REQUIRED for fixed-point math) var a: int = SGFixed.from_int(3) # 196608 var b: int = SGFixed.from_int(4) # 262144 var product: int = SGFixed.mul(a, b) # 786432 (equals 12.0) var quotient: int = SGFixed.div(a, b) # 49152 (equals 0.75) # Trigonometry (all angles in fixed-point radians) var sin_val: int = SGFixed.sin(SGFixed.PI_DIV_2) # 65536 (sin(90deg) = 1.0) var cos_val: int = SGFixed.cos(0) # 65536 (cos(0) = 1.0) var tan_val: int = SGFixed.tan(SGFixed.PI_DIV_4) # 65536 (tan(45deg) = 1.0) var angle: int = SGFixed.atan2(SGFixed.ONE, SGFixed.ONE) # PI_DIV_4 (45deg) # Degree/radian conversion var deg_90: int = SGFixed.from_int(90) var rad_90: int = SGFixed.deg_to_rad(deg_90) # ~PI_DIV_2 var back_deg: int = SGFixed.rad_to_deg(SGFixed.PI) # ~180 in fixed-point # Other math functions var sqrt_val: int = SGFixed.sqrt(SGFixed.from_int(4)) # 131072 (2.0) var pow_val: int = SGFixed.pow(SGFixed.TWO, SGFixed.TWO) # 262144 (4.0) var floor_val: int = SGFixed.floor(SGFixed.from_float(2.7)) # 131072 (2.0) var ceil_val: int = SGFixed.ceil(SGFixed.from_float(2.1)) # 196608 (3.0) var round_val: int = SGFixed.round(SGFixed.from_float(2.5)) # 196608 (3.0) var moved: int = SGFixed.move_toward(0, SGFixed.ONE, SGFixed.HALF) # 32768 (0.5) # Create fixed-point types var vec: SGFixedVector2 = SGFixed.vector2(65536, 131072) # (1.0, 2.0) var transform: SGFixedTransform2D = SGFixed.transform2d(0, vec) # rotation=0, origin=(1,2) var rect: SGFixedRect2 = SGFixed.rect2(vec, SGFixed.vector2(65536, 65536)) ``` -------------------------------- ### Compile Godot 3 Debug Export Templates with SG Physics 2D Source: https://gitlab.com/snopek-games/sg-physics-2d/-/blob/main/README.md This command compiles the debug export templates for Godot 3, including the SG Physics 2D module. Ensure you replace PLATFORM with your target OS and PATH with the correct path to the SG Physics 2D source code. This is necessary for exporting games that utilize the physics module. ```bash scons platform=PLATFORM tools=no target=debug custom_modules=/PATH/sg-physics-2d/src ``` -------------------------------- ### Create Tilemap Collisions with SGPhysics2DServer (GDScript) Source: https://context7.com/snopek-games/sg-physics-2d/llms.txt Demonstrates how to create static collision shapes for a tilemap using SGPhysics2DServer. It iterates through used tilemap cells, creates rectangular shapes, attaches them to static bodies, sets their positions, and adds them to the physics world. Resources are tracked for cleanup. ```gdscript extends Node2D var resources: Array = [] # Track created resources for cleanup func _ready(): create_tilemap_collision() func _exit_tree(): # Clean up all created physics resources for rid in resources: SGPhysics2DServer.free_rid(rid) func create_tilemap_collision() -> void: var tilemap: TileMap = $TileMap var tile_width: int = 16 var tile_height: int = 16 var extents: SGFixedVector2 = SGFixed.vector2( SGFixed.from_int(tile_width / 2), SGFixed.from_int(tile_height / 2) ) var shape_transform: SGFixedTransform2D = SGFixed.transform2d(0, extents) # Get tiles that should have collision for tile_pos in tilemap.get_used_cells(0): # Create rectangle shape var shape: RID = SGPhysics2DServer.shape_create(SGPhysics2DServer.SHAPE_RECTANGLE) SGPhysics2DServer.rectangle_set_extents(shape, extents) SGPhysics2DServer.shape_set_transform(shape, shape_transform) resources.push_back(shape) # Create static body var body: RID = SGPhysics2DServer.collision_object_create( SGPhysics2DServer.OBJECT_BODY, SGPhysics2DServer.BODY_STATIC ) SGPhysics2DServer.collision_object_add_shape(body, shape) # Set position var world_pos: SGFixedVector2 = SGFixed.vector2( SGFixed.from_int(int(tile_pos.x) * tile_width), SGFixed.from_int(int(tile_pos.y) * tile_height) ) SGPhysics2DServer.collision_object_set_transform( body, SGFixed.transform2d(0, world_pos) ) # Set data for deterministic sorting (IMPORTANT!) SGPhysics2DServer.collision_object_set_data( body, "tile-%sx%s" % [int(tile_pos.x), int(tile_pos.y)] ) resources.push_back(body) # Add to world SGPhysics2DServer.world_add_collision_object( SGPhysics2DServer.get_default_world(), body ) ``` -------------------------------- ### SGFixedVector2: Create, Access, and Convert Vectors (GDScript) Source: https://context7.com/snopek-games/sg-physics-2d/llms.txt Demonstrates how to create SGFixedVector2 instances, access their fixed-point components, and convert between fixed-point and standard Godot Vector2 types. Note that float conversions are for display only and not recommended for gameplay logic. ```gdscript extends Node func _ready(): # Create vectors var v1: SGFixedVector2 = SGFixed.vector2(65536, 131072) # (1.0, 2.0) var v2: SGFixedVector2 = SGFixed.vector2(196608, 262144) # (3.0, 4.0) # Access components (fixed-point integers) var x: int = v1.x # 65536 var y: int = v1.y # 131072 v1.x = SGFixed.from_int(5) # Set x to 5.0 # Convert to/from float (for display only, NOT gameplay!) var float_vec: Vector2 = v1.to_float() # Vector2(5.0, 2.0) v1.from_float(Vector2(1.0, 1.0)) # Sets to (65536, 65536) ``` -------------------------------- ### Manage Fixed-Point Transforms with SGFixedNode2D (GDScript) Source: https://context7.com/snopek-games/sg-physics-2d/llms.txt Illustrates the usage of SGFixedNode2D for managing physics properties using fixed-point numbers. It covers setting `fixed_position`, `fixed_rotation`, and `fixed_scale`, and emphasizes the importance of calling `sync_to_physics_engine()` after any transform changes. It also shows how to update the position and rotation in `_physics_process`. ```gdscript extends SGFixedNode2D func _ready(): # Set position using fixed-point values fixed_position = SGFixed.vector2( SGFixed.from_int(100), # X = 100 SGFixed.from_int(50) # Y = 50 ) # Set rotation (in fixed-point radians) fixed_rotation = SGFixed.PI_DIV_4 # 45 degrees # Set scale fixed_scale = SGFixed.vector2(SGFixed.TWO, SGFixed.TWO) # 2x scale # IMPORTANT: Sync to physics engine after any transform change! sync_to_physics_engine() func _physics_process(delta: float) -> void: # Move using fixed-point math var movement: SGFixedVector2 = SGFixed.vector2(65536, 0) # (1, 0) fixed_position = fixed_position.add(movement) # Rotate fixed_rotation += 3277 # ~0.05 radians per frame # Always sync after changes sync_to_physics_engine() # The visual position updates automatically from fixed_position # position, rotation, scale properties reflect the fixed-point values # Getting global transform for physics calculations func get_physics_transform() -> SGFixedTransform2D: return global_fixed_transform ``` -------------------------------- ### SGFixedVector2: Copying Vectors by Reference (GDScript) Source: https://context7.com/snopek-games/sg-physics-2d/llms.txt Highlights the critical difference between assigning SGFixedVector2 variables (which creates a reference) and using the `.copy()` method (which creates an independent copy). Understanding this is essential to avoid unintended modifications. ```gdscript extends Node func _ready(): # Copy (IMPORTANT: vectors are passed by reference!) var original: SGFixedVector2 = SGFixed.vector2(65536, 65536) var reference: SGFixedVector2 = original # Same object! var copy: SGFixedVector2 = original.copy() # Independent copy reference.x = 0 # Also changes original! copy.x = 0 # Does NOT change original ``` -------------------------------- ### Create and Query Detection Area with SGPhysics2DServer (GDScript) Source: https://context7.com/snopek-games/sg-physics-2d/llms.txt Shows how to create a circular detection area using SGPhysics2DServer and query for overlapping bodies. This involves creating a circle shape, attaching it to an area object, positioning it, and adding it to the physics world. The `area_get_overlapping_bodies` function is used for querying. ```gdscript # Create an area for detection func create_detection_area(position: SGFixedVector2, radius: int) -> RID: var shape: RID = create_circle_shape(radius) resources.push_back(shape) var area: RID = SGPhysics2DServer.collision_object_create( SGPhysics2DServer.OBJECT_AREA ) SGPhysics2DServer.collision_object_add_shape(area, shape) SGPhysics2DServer.collision_object_set_transform( area, SGFixed.transform2d(0, position) ) SGPhysics2DServer.collision_object_set_data(area, "detection_area") resources.push_back(area) SGPhysics2DServer.world_add_collision_object( SGPhysics2DServer.get_default_world(), area ) return area # Query area overlaps func check_area_overlaps(area: RID) -> Array: return SGPhysics2DServer.area_get_overlapping_bodies(area) ``` -------------------------------- ### SGFixedTransform2D: Create and Manipulate 2D Transforms Source: https://context7.com/snopek-games/sg-physics-2d/llms.txt Demonstrates the creation and manipulation of SGFixedTransform2D objects, including accessing basis vectors, origin, rotation, and scale. It also shows transform operations like rotation, scaling, translation, and inversion, along with applying transforms to points. This class is crucial for deterministic positioning of physics objects. ```gdscript extends Node func _ready(): # Create transforms var origin: SGFixedVector2 = SGFixed.vector2(65536, 131072) # (1.0, 2.0) var t1: SGFixedTransform2D = SGFixed.transform2d(0, origin) # No rotation var t2: SGFixedTransform2D = SGFixed.transform2d(SGFixed.PI_DIV_4, origin) # 45 deg # Access basis vectors and origin var x_axis: SGFixedVector2 = t1.x # X basis vector var y_axis: SGFixedVector2 = t1.y # Y basis vector var pos: SGFixedVector2 = t1.origin # Translation # Get rotation and scale var rotation: int = t1.get_rotation() # Rotation in fixed-point radians var scale: SGFixedVector2 = t1.get_scale() # Scale as vector # Transform operations var rotated: SGFixedTransform2D = t1.rotated(SGFixed.PI_DIV_2) # Rotate 90 deg var scaled: SGFixedTransform2D = t1.scaled(SGFixed.vector2(SGFixed.TWO, SGFixed.TWO)) var translated: SGFixedTransform2D = t1.translated(SGFixed.vector2(65536, 0)) # Apply transform to a point var point: SGFixedVector2 = SGFixed.vector2(65536, 0) var transformed: SGFixedVector2 = t1.xform(point) # Transform point var inverse_t: SGFixedTransform2D = t1.affine_inverse() # Inverse transform var untransformed: SGFixedVector2 = inverse_t.xform(transformed) # Back to original # Deterministic rotation accumulation (tested over 320 iterations) var t: SGFixedTransform2D = SGFixed.transform2d(0, SGFixed.vector2(0, 0)) for i in range(320): t = t.rotated(SGFixed.PI_DIV_4) # Result is deterministically back to identity assert(t.get_rotation() == 0) ``` -------------------------------- ### Stubbing a Python Method with __gutdbl Source: https://gitlab.com/snopek-games/sg-physics-2d/-/blob/main/projects/godot-4/addons/gut/double_templates/function_template.txt This Python code demonstrates how to stub a method using __gutdbl. It checks if the method should be called with its superclass implementation or if a stubbed return value should be used. This is useful for isolating components during testing. ```python def stub_method(method_name, param_array, super_call): __gutdbl.spy_on(method_name, param_array) if(__gutdbl.should_call_super(method_name, param_array)): return super_call else: return __gutdbl.get_stubbed_return(method_name, param_array) ``` -------------------------------- ### Create Various Shapes with SGPhysics2DServer (GDScript) Source: https://context7.com/snopek-games/sg-physics-2d/llms.txt Provides functions to programmatically create different physics shapes like circles and polygons using SGPhysics2DServer. These shapes can then be added to physics bodies or areas. The functions return the RID of the created shape. ```gdscript # Create shapes programmatically func create_circle_shape(radius: int) -> RID: var shape: RID = SGPhysics2DServer.shape_create(SGPhysics2DServer.SHAPE_CIRCLE) SGPhysics2DServer.circle_set_radius(shape, radius) return shape func create_polygon_shape(points: Array) -> RID: var shape: RID = SGPhysics2DServer.shape_create(SGPhysics2DServer.SHAPE_POLYGON) SGPhysics2DServer.polygon_set_points(shape, points) return shape ``` -------------------------------- ### SGFixedVector2: Vector Arithmetic (GDScript) Source: https://context7.com/snopek-games/sg-physics-2d/llms.txt Illustrates standard vector arithmetic operations like addition, subtraction, scalar multiplication, and division using SGFixedVector2. These operations return new SGFixedVector2 instances, leaving the original vectors unchanged. ```gdscript extends Node func _ready(): var v1: SGFixedVector2 = SGFixed.vector2(65536, 131072) # (1.0, 2.0) var v2: SGFixedVector2 = SGFixed.vector2(196608, 262144) # (3.0, 4.0) # Vector arithmetic (returns new vector) var sum: SGFixedVector2 = v1.add(v2) # Vector addition var diff: SGFixedVector2 = v1.sub(v2) # Vector subtraction var scaled: SGFixedVector2 = v1.mul(SGFixed.TWO) # Scalar multiplication var divided: SGFixedVector2 = v1.div(SGFixed.TWO) # Scalar division ``` -------------------------------- ### SGFixedVector2: Physics Helper Functions (GDScript) Source: https://context7.com/snopek-games/sg-physics-2d/llms.txt Demonstrates specialized physics helper functions for SGFixedVector2, including bounce, reflect, and slide operations. These are useful for simulating interactions with surfaces and other objects. ```gdscript extends Node func _ready(): var v1: SGFixedVector2 = SGFixed.vector2(65536, 65536) # (1.0, 1.0) # Physics helpers var bounced: SGFixedVector2 = v1.bounce(SGFixed.vector2(0, SGFixed.NEG_ONE)) var reflected: SGFixedVector2 = v1.reflect(SGFixed.vector2(0, SGFixed.ONE)) var slid: SGFixedVector2 = v1.slide(SGFixed.vector2(0, SGFixed.ONE)) ``` -------------------------------- ### SGFixedVector2: In-Place Vector Arithmetic (GDScript) Source: https://context7.com/snopek-games/sg-physics-2d/llms.txt Shows how to perform arithmetic operations directly on an SGFixedVector2 instance, modifying it in place. This is generally more efficient than creating new vectors for repeated updates, such as in physics simulations. ```gdscript extends Node func _ready(): var velocity: SGFixedVector2 = SGFixed.vector2(0, 0) velocity.iadd(SGFixed.vector2(65536, 0)) # velocity += (1, 0) velocity.isub(SGFixed.vector2(0, 65536)) # velocity -= (0, 1) velocity.imul(SGFixed.TWO) # velocity *= 2 velocity.idiv(SGFixed.TWO) # velocity /= 2 ``` -------------------------------- ### SGFixedVector2: Interpolation Methods (GDScript) Source: https://context7.com/snopek-games/sg-physics-2d/llms.txt Shows how to perform linear and cubic interpolation between two SGFixedVector2 instances. Interpolation is commonly used for smooth transitions and animations. ```gdscript extends Node func _ready(): var v1: SGFixedVector2 = SGFixed.vector2(65536, 131072) # (1.0, 2.0) var v2: SGFixedVector2 = SGFixed.vector2(196608, 262144) # (3.0, 4.0) # Interpolation var lerped: SGFixedVector2 = v1.linear_interpolate(v2, SGFixed.HALF) var cubic: SGFixedVector2 = v1.cubic_interpolate(v2, v1, v2, SGFixed.HALF) ``` -------------------------------- ### Save and Load Physics State for Rollback Netcode (GDScript) Source: https://context7.com/snopek-games/sg-physics-2d/llms.txt Provides GDScript functions to save and load the physics state of an object. The save function captures position, rotation, and scale into a dictionary. The load function restores these values and critically calls a synchronization function to update the physics engine. ```GDScript func save_state() -> Dictionary: return { "pos_x": fixed_position.x, "pos_y": fixed_position.y, "rotation": fixed_rotation, "scale_x": fixed_scale.x, "scale_y": fixed_scale.y, } func load_state(state: Dictionary) -> void: fixed_position.x = state.pos_x fixed_position.y = state.pos_y fixed_rotation = state.rotation fixed_scale.x = state.scale_x fixed_scale.y = state.scale_y # CRITICAL: Must sync after loading state! sync_to_physics_engine() ``` -------------------------------- ### SGFixedVector2: Vector Properties and Operations (GDScript) Source: https://context7.com/snopek-games/sg-physics-2d/llms.txt Covers common vector properties and operations like calculating magnitude (length), squared magnitude, normalization, and checking if a vector is normalized. These are fundamental for many physics and game development tasks. ```gdscript extends Node func _ready(): var v1: SGFixedVector2 = SGFixed.vector2(65536, 65536) # (1.0, 1.0) # Vector operations var length: int = v1.length() # Vector magnitude var length_sq: int = v1.length_squared() # Squared magnitude (faster) var normalized: SGFixedVector2 = v1.normalized() # Unit vector var is_norm: bool = v1.is_normalized() # Check if unit length ``` -------------------------------- ### Gut Doubled Script Method Check Source: https://gitlab.com/snopek-games/sg-physics-2d/-/blob/main/projects/godot-4/addons/gut/double_templates/script_template.txt A placeholder function intended to allow other parts of the system to check if an object is a 'doubled' object by verifying the presence of this specific method. This is a common pattern in testing frameworks to identify mock or doubled objects. ```gdscript func __gutdbl_check_method__(): pass ``` -------------------------------- ### SGArea2D: Detect Overlapping Bodies and Areas Source: https://context7.com/snopek-games/sg-physics-2d/llms.txt SGArea2D detects overlapping physics bodies and areas. Unlike Godot's Area2D, it requires explicit queries for deterministic results. It can retrieve overlapping bodies, their count, and detailed collision information, as well as overlapping areas and their count. ```gdscript extends SGArea2D func _ready(): # Sync initial position to physics engine sync_to_physics_engine() func _physics_process(delta: float) -> void: # Check for overlapping bodies (SGStaticBody2D, SGCharacterBody2D) var bodies: Array = get_overlapping_bodies() for body in bodies: if body is SGCharacterBody2D: print("Player entered area: %s" % body.name) # Get body count (faster if you only need the count) var body_count: int = get_overlapping_body_count() # Get detailed collision info for bodies var body_collisions: Array = get_overlapping_body_collisions() for collision in body_collisions: var collider = collision.collider var collider_shape = collision.collider_shape var local_shape = collision.local_shape # Check for overlapping areas var areas: Array = get_overlapping_areas() var area_count: int = get_overlapping_area_count() var area_collisions: Array = get_overlapping_area_collisions() for area in areas: print("Overlapping with area: %s" % area.name) # Example: Trigger zone extends SGArea2D var players_in_zone: Array = [] func check_zone() -> void: players_in_zone.clear() for body in get_overlapping_bodies(): if body.is_in_group("players"): players_in_zone.append(body) if players_in_zone.size() > 0: trigger_effect() func trigger_effect() -> void: print("Zone activated by %d players" % players_in_zone.size()) ``` -------------------------------- ### SGRayCast2D: Perform Ray Casting and Collision Tests Source: https://context7.com/snopek-games/sg-physics-2d/llms.txt SGRayCast2D performs deterministic line-segment intersection tests, finding the closest colliding object along a ray. It requires explicit updates before checking collision results and allows configuration of cast direction, collision mask, and exceptions. It can return the collider, collision point, and collision normal. ```gdscript extends SGFixedNode2D @onready var ray_cast: SGRayCast2D = $SGRayCast2D func _ready(): # Configure raycast ray_cast.cast_to = SGFixed.vector2(0, 3276800) # Cast down 50 units ray_cast.collision_mask = 1 # Collision layer 1 ray_cast.collide_with_bodies = true ray_cast.collide_with_areas = false # Add exceptions (objects to ignore) ray_cast.add_exception(self) func _physics_process(delta: float) -> void: # Update raycast calculation (required before checking results!) ray_cast.update_raycast_collision() if ray_cast.is_colliding(): var collider: Object = ray_cast.get_collider() var collision_point: SGFixedVector2 = ray_cast.get_collision_point() var collision_normal: SGFixedVector2 = ray_cast.get_collision_normal() print("Hit: %s at %s" % [collider.name, collision_point.to_float()]) print("Surface normal: %s" % collision_normal.to_float()) # Visual feedback (using float conversion for display only) $Marker.position = collision_point.to_float() else: print("No collision") # Ground detection example func is_grounded() -> bool: ray_cast.update_raycast_collision() return ray_cast.is_colliding() # Multiple raycast queries with different directions func check_surroundings() -> Dictionary: var results = {"left": false, "right": false, "down": false} # Check left ray_cast.cast_to = SGFixed.vector2(-65536 * 10, 0) # -10 units X ray_cast.update_raycast_collision() results.left = ray_cast.is_colliding() # Check right ray_cast.cast_to = SGFixed.vector2(65536 * 10, 0) # +10 units X ray_cast.update_raycast_collision() results.right = ray_cast.is_colliding() # Check down ray_cast.cast_to = SGFixed.vector2(0, 65536 * 10) # +10 units Y ray_cast.update_raycast_collision() results.down = ray_cast.is_colliding() return results # Managing exceptions func configure_exceptions(ignore_list: Array) -> void: ray_cast.clear_exceptions() for obj in ignore_list: ray_cast.add_exception(obj) ``` -------------------------------- ### SGFixedVector2: Rotation Operations (GDScript) Source: https://context7.com/snopek-games/sg-physics-2d/llms.txt Explains how to rotate SGFixedVector2 vectors. It covers creating a new rotated vector and rotating a vector in-place, using fixed-point representations of angles. ```gdscript extends Node func _ready(): var v1: SGFixedVector2 = SGFixed.vector2(65536, 65536) # (1.0, 1.0) # Rotation var rotated: SGFixedVector2 = v1.rotated(SGFixed.PI_DIV_2) # Rotate 90 degrees v1.rotate(SGFixed.PI_DIV_4) # Rotate in-place by 45 degrees ``` -------------------------------- ### SGFixedVector2: Geometric and Angle Operations (GDScript) Source: https://context7.com/snopek-games/sg-physics-2d/llms.txt Details geometric calculations such as dot product, cross product, distance between vectors, and direction vectors. It also includes functions for determining angles in fixed-point radians. ```gdscript extends Node func _ready(): var v1: SGFixedVector2 = SGFixed.vector2(65536, 131072) # (1.0, 2.0) var v2: SGFixedVector2 = SGFixed.vector2(196608, 262144) # (3.0, 4.0) # Geometry operations var dot: int = v1.dot(v2) # Dot product var cross: int = v1.cross(v2) # 2D cross product var distance: int = v1.distance_to(v2) # Distance between vectors var dist_sq: int = v1.distance_squared_to(v2) # Squared distance var direction: SGFixedVector2 = v1.direction_to(v2) # Normalized direction # Angles (in fixed-point radians) var angle: int = v1.angle() # Angle to positive X axis var angle_to: int = v1.angle_to(v2) # Angle between vectors var angle_pt: int = v1.angle_to_point(v2) # Angle of line to X axis ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.