1 /** 2 Kinematic body 2D node. 3 4 Copyright: 5 Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. 6 Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) 7 Copyright (c) 2017-2018 Godot-D contributors 8 9 License: $(LINK2 https://opensource.org/licenses/MIT, MIT License) 10 11 12 */ 13 module godot.kinematicbody2d; 14 import std.meta : AliasSeq, staticIndexOf; 15 import std.traits : Unqual; 16 import godot.d.traits; 17 import godot.core; 18 import godot.c; 19 import godot.d.bind; 20 import godot.d.reference; 21 import godot.globalenums; 22 import godot.object; 23 import godot.classdb; 24 import godot.physicsbody2d; 25 import godot.kinematiccollision2d; 26 /** 27 Kinematic body 2D node. 28 29 Kinematic bodies are special types of bodies that are meant to be user-controlled. They are not affected by physics at all; to other types of bodies, such as a character or a rigid body, these are the same as a static body. However, they have two main uses: 30 $(B Simulated motion:) When these bodies are moved manually, either from code or from an $(D AnimationPlayer) (with $(D AnimationPlayer.playbackProcessMode) set to "physics"), the physics will automatically compute an estimate of their linear and angular velocity. This makes them very useful for moving platforms or other AnimationPlayer-controlled objects (like a door, a bridge that opens, etc). 31 $(B Kinematic characters:) KinematicBody2D also has an API for moving objects (the $(D moveAndCollide) and $(D moveAndSlide) methods) while performing collision tests. This makes them really useful to implement characters that collide against a world, but don't require advanced physics. 32 */ 33 @GodotBaseClass struct KinematicBody2D 34 { 35 package(godot) enum string _GODOT_internal_name = "KinematicBody2D"; 36 public: 37 @nogc nothrow: 38 union { /** */ godot_object _godot_object; /** */ PhysicsBody2D _GODOT_base; } 39 alias _GODOT_base this; 40 alias BaseClasses = AliasSeq!(typeof(_GODOT_base), typeof(_GODOT_base).BaseClasses); 41 package(godot) __gshared bool _classBindingInitialized = false; 42 package(godot) static struct GDNativeClassBinding 43 { 44 __gshared: 45 @GodotName("_direct_state_changed") GodotMethod!(void, GodotObject) _directStateChanged; 46 @GodotName("get_floor_normal") GodotMethod!(Vector2) getFloorNormal; 47 @GodotName("get_floor_velocity") GodotMethod!(Vector2) getFloorVelocity; 48 @GodotName("get_safe_margin") GodotMethod!(double) getSafeMargin; 49 @GodotName("get_slide_collision") GodotMethod!(KinematicCollision2D, long) getSlideCollision; 50 @GodotName("get_slide_count") GodotMethod!(long) getSlideCount; 51 @GodotName("is_on_ceiling") GodotMethod!(bool) isOnCeiling; 52 @GodotName("is_on_floor") GodotMethod!(bool) isOnFloor; 53 @GodotName("is_on_wall") GodotMethod!(bool) isOnWall; 54 @GodotName("is_sync_to_physics_enabled") GodotMethod!(bool) isSyncToPhysicsEnabled; 55 @GodotName("move_and_collide") GodotMethod!(KinematicCollision2D, Vector2, bool, bool, bool) moveAndCollide; 56 @GodotName("move_and_slide") GodotMethod!(Vector2, Vector2, Vector2, bool, long, double, bool) moveAndSlide; 57 @GodotName("move_and_slide_with_snap") GodotMethod!(Vector2, Vector2, Vector2, Vector2, bool, long, double, bool) moveAndSlideWithSnap; 58 @GodotName("set_safe_margin") GodotMethod!(void, double) setSafeMargin; 59 @GodotName("set_sync_to_physics") GodotMethod!(void, bool) setSyncToPhysics; 60 @GodotName("test_move") GodotMethod!(bool, Transform2D, Vector2, bool) testMove; 61 } 62 /// 63 pragma(inline, true) bool opEquals(in KinematicBody2D other) const 64 { return _godot_object.ptr is other._godot_object.ptr; } 65 /// 66 pragma(inline, true) typeof(null) opAssign(typeof(null) n) 67 { _godot_object.ptr = n; return null; } 68 /// 69 pragma(inline, true) bool opEquals(typeof(null) n) const 70 { return _godot_object.ptr is n; } 71 /// 72 size_t toHash() const @trusted { return cast(size_t)_godot_object.ptr; } 73 mixin baseCasts; 74 /// Construct a new instance of KinematicBody2D. 75 /// Note: use `memnew!KinematicBody2D` instead. 76 static KinematicBody2D _new() 77 { 78 static godot_class_constructor constructor; 79 if(constructor is null) constructor = _godot_api.godot_get_class_constructor("KinematicBody2D"); 80 if(constructor is null) return typeof(this).init; 81 return cast(KinematicBody2D)(constructor()); 82 } 83 @disable new(size_t s); 84 /** 85 86 */ 87 void _directStateChanged(GodotObject arg0) 88 { 89 Array _GODOT_args = Array.make(); 90 _GODOT_args.append(arg0); 91 String _GODOT_method_name = String("_direct_state_changed"); 92 this.callv(_GODOT_method_name, _GODOT_args); 93 } 94 /** 95 Returns the surface normal of the floor at the last collision point. Only valid after calling $(D moveAndSlide) or $(D moveAndSlideWithSnap) and when $(D isOnFloor) returns `true`. 96 */ 97 Vector2 getFloorNormal() const 98 { 99 checkClassBinding!(typeof(this))(); 100 return ptrcall!(Vector2)(GDNativeClassBinding.getFloorNormal, _godot_object); 101 } 102 /** 103 Returns the linear velocity of the floor at the last collision point. Only valid after calling $(D moveAndSlide) or $(D moveAndSlideWithSnap) and when $(D isOnFloor) returns `true`. 104 */ 105 Vector2 getFloorVelocity() const 106 { 107 checkClassBinding!(typeof(this))(); 108 return ptrcall!(Vector2)(GDNativeClassBinding.getFloorVelocity, _godot_object); 109 } 110 /** 111 112 */ 113 double getSafeMargin() const 114 { 115 checkClassBinding!(typeof(this))(); 116 return ptrcall!(double)(GDNativeClassBinding.getSafeMargin, _godot_object); 117 } 118 /** 119 Returns a $(D KinematicCollision2D), which contains information about a collision that occurred during the last call to $(D moveAndSlide) or $(D moveAndSlideWithSnap). Since the body can collide several times in a single call to $(D moveAndSlide), you must specify the index of the collision in the range 0 to ($(D getSlideCount) - 1). 120 $(B Example usage:) 121 122 123 for i in get_slide_count(): 124 var collision = get_slide_collision(i) 125 print("Collided with: ", collision.collider.name) 126 127 128 */ 129 Ref!KinematicCollision2D getSlideCollision(in long slide_idx) 130 { 131 checkClassBinding!(typeof(this))(); 132 return ptrcall!(KinematicCollision2D)(GDNativeClassBinding.getSlideCollision, _godot_object, slide_idx); 133 } 134 /** 135 Returns the number of times the body collided and changed direction during the last call to $(D moveAndSlide) or $(D moveAndSlideWithSnap). 136 */ 137 long getSlideCount() const 138 { 139 checkClassBinding!(typeof(this))(); 140 return ptrcall!(long)(GDNativeClassBinding.getSlideCount, _godot_object); 141 } 142 /** 143 Returns `true` if the body collided with the ceiling on the last call of $(D moveAndSlide) or $(D moveAndSlideWithSnap). Otherwise, returns `false`. 144 */ 145 bool isOnCeiling() const 146 { 147 checkClassBinding!(typeof(this))(); 148 return ptrcall!(bool)(GDNativeClassBinding.isOnCeiling, _godot_object); 149 } 150 /** 151 Returns `true` if the body collided with the floor on the last call of $(D moveAndSlide) or $(D moveAndSlideWithSnap). Otherwise, returns `false`. 152 */ 153 bool isOnFloor() const 154 { 155 checkClassBinding!(typeof(this))(); 156 return ptrcall!(bool)(GDNativeClassBinding.isOnFloor, _godot_object); 157 } 158 /** 159 Returns `true` if the body collided with a wall on the last call of $(D moveAndSlide) or $(D moveAndSlideWithSnap). Otherwise, returns `false`. 160 */ 161 bool isOnWall() const 162 { 163 checkClassBinding!(typeof(this))(); 164 return ptrcall!(bool)(GDNativeClassBinding.isOnWall, _godot_object); 165 } 166 /** 167 168 */ 169 bool isSyncToPhysicsEnabled() const 170 { 171 checkClassBinding!(typeof(this))(); 172 return ptrcall!(bool)(GDNativeClassBinding.isSyncToPhysicsEnabled, _godot_object); 173 } 174 /** 175 Moves the body along the vector `rel_vec`. The body will stop if it collides. Returns a $(D KinematicCollision2D), which contains information about the collision. 176 If `test_only` is `true`, the body does not move but the would-be collision information is given. 177 */ 178 Ref!KinematicCollision2D moveAndCollide(in Vector2 rel_vec, in bool infinite_inertia = true, in bool exclude_raycast_shapes = true, in bool test_only = false) 179 { 180 checkClassBinding!(typeof(this))(); 181 return ptrcall!(KinematicCollision2D)(GDNativeClassBinding.moveAndCollide, _godot_object, rel_vec, infinite_inertia, exclude_raycast_shapes, test_only); 182 } 183 /** 184 Moves the body along a vector. If the body collides with another, it will slide along the other body rather than stop immediately. If the other body is a $(D KinematicBody2D) or $(D RigidBody2D), it will also be affected by the motion of the other body. You can use this to make moving and rotating platforms, or to make nodes push other nodes. 185 This method should be used in $(D Node._physicsProcess) (or in a method called by $(D Node._physicsProcess)), as it uses the physics step's `delta` value automatically in calculations. Otherwise, the simulation will run at an incorrect speed. 186 `linear_velocity` is the velocity vector in pixels per second. Unlike in $(D moveAndCollide), you should $(I not) multiply it by `delta` — the physics engine handles applying the velocity. 187 `up_direction` is the up direction, used to determine what is a wall and what is a floor or a ceiling. If set to the default value of `Vector2(0, 0)`, everything is considered a wall. This is useful for topdown games. 188 If `stop_on_slope` is `true`, body will not slide on slopes when you include gravity in `linear_velocity` and the body is standing still. 189 If the body collides, it will change direction a maximum of `max_slides` times before it stops. 190 `floor_max_angle` is the maximum angle (in radians) where a slope is still considered a floor (or a ceiling), rather than a wall. The default value equals 45 degrees. 191 If `infinite_inertia` is `true`, body will be able to push $(D RigidBody2D) nodes, but it won't also detect any collisions with them. If `false`, it will interact with $(D RigidBody2D) nodes like with $(D StaticBody2D). 192 Returns the `linear_velocity` vector, rotated and/or scaled if a slide collision occurred. To get detailed information about collisions that occurred, use $(D getSlideCollision). 193 */ 194 Vector2 moveAndSlide(in Vector2 linear_velocity, in Vector2 up_direction = Vector2(0, 0), in bool stop_on_slope = false, in long max_slides = 4, in double floor_max_angle = 0.785398, in bool infinite_inertia = true) 195 { 196 checkClassBinding!(typeof(this))(); 197 return ptrcall!(Vector2)(GDNativeClassBinding.moveAndSlide, _godot_object, linear_velocity, up_direction, stop_on_slope, max_slides, floor_max_angle, infinite_inertia); 198 } 199 /** 200 Moves the body while keeping it attached to slopes. Similar to $(D moveAndSlide). 201 As long as the `snap` vector is in contact with the ground, the body will remain attached to the surface. This means you must disable snap in order to jump, for example. You can do this by setting `snap` to `(0, 0)` or by using $(D moveAndSlide) instead. 202 */ 203 Vector2 moveAndSlideWithSnap(in Vector2 linear_velocity, in Vector2 snap, in Vector2 up_direction = Vector2(0, 0), in bool stop_on_slope = false, in long max_slides = 4, in double floor_max_angle = 0.785398, in bool infinite_inertia = true) 204 { 205 checkClassBinding!(typeof(this))(); 206 return ptrcall!(Vector2)(GDNativeClassBinding.moveAndSlideWithSnap, _godot_object, linear_velocity, snap, up_direction, stop_on_slope, max_slides, floor_max_angle, infinite_inertia); 207 } 208 /** 209 210 */ 211 void setSafeMargin(in double pixels) 212 { 213 checkClassBinding!(typeof(this))(); 214 ptrcall!(void)(GDNativeClassBinding.setSafeMargin, _godot_object, pixels); 215 } 216 /** 217 218 */ 219 void setSyncToPhysics(in bool enable) 220 { 221 checkClassBinding!(typeof(this))(); 222 ptrcall!(void)(GDNativeClassBinding.setSyncToPhysics, _godot_object, enable); 223 } 224 /** 225 Checks for collisions without moving the body. Virtually sets the node's position, scale and rotation to that of the given $(D Transform2D), then tries to move the body along the vector `rel_vec`. Returns `true` if a collision would occur. 226 */ 227 bool testMove(in Transform2D from, in Vector2 rel_vec, in bool infinite_inertia = true) 228 { 229 checkClassBinding!(typeof(this))(); 230 return ptrcall!(bool)(GDNativeClassBinding.testMove, _godot_object, from, rel_vec, infinite_inertia); 231 } 232 /** 233 Extra margin used for collision recovery in motion functions (see $(D moveAndCollide), $(D moveAndSlide), $(D moveAndSlideWithSnap)). 234 If the body is at least this close to another body, it will consider them to be colliding and will be pushed away before performing the actual motion. 235 A higher value means it's more flexible for detecting collision, which helps with consistently detecting walls and floors. 236 A lower value forces the collision algorithm to use more exact detection, so it can be used in cases that specifically require precision, e.g at very low scale to avoid visible jittering, or for stability with a stack of kinematic bodies. 237 */ 238 @property double collisionSafeMargin() 239 { 240 return getSafeMargin(); 241 } 242 /// ditto 243 @property void collisionSafeMargin(double v) 244 { 245 setSafeMargin(v); 246 } 247 /** 248 If `true`, the body's movement will be synchronized to the physics frame. This is useful when animating movement via $(D AnimationPlayer), for example on moving platforms. Do $(B not) use together with $(D moveAndSlide) or $(D moveAndCollide) functions. 249 */ 250 @property bool motionSyncToPhysics() 251 { 252 return isSyncToPhysicsEnabled(); 253 } 254 /// ditto 255 @property void motionSyncToPhysics(bool v) 256 { 257 setSyncToPhysics(v); 258 } 259 }