Second Case: BallZ: So, A new beginning is so!
so, alright so after the exciting part 2 finale of our last project where we successfully managed to finish everything, I now bring you a new project, so I thought "hey! why don't I do a game?" and so that became my plan, and as for what game I have no clue, but we will get there.
so, I got the Godot engine and had no idea what da heel I was doing.
so, I applied my idea of how to figure stuff out and that was to make an idea of what I want, so I did. so, after sitting and thinking for a while I remembered a game, I played waaay back when, and this game involved you shooting balls at targets,
so, after playing around with the engine's programming language which is called GDscript I managed to see that it wasn't as evil looking as C++ and was decently close enough to python so I felt fine.
So, the next thing to do was plan out how I wanted the ball to do things and what those things were and that was the hard part so I decided I will get there later.
So at first I thought maybe you should be able to control the ball depending on where you click the screen, so I thought maybe I should make a wave that expands and then touches the ball to move it, so after a while of doing it I decided it was ugly and changing the direction of the ball was slow, so I decided that maybe I should just make it so that you click the screen and the ball reacts depending on the distance, which makes the ball react way faster and seems more fun.
and as such after several days of hard grueling torturous work we managed to get this marvel:
you click and it moves here is some of le code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | extends RigidBody2D # Inherit for physics simulation var force_base = 100 # Base force applied during push (adjustable) # Called on scene entry (unused in this example) func _ready(): pass # Called every frame func _process(_delta): if Input.is_action_just_pressed("mouse_click_left"): var force = force_base var max_distance = 700 # Maximum distance for full force # Get object and click positions var ball_position = self.transform.origin var click_position = get_global_mouse_position() # Calculate distance between ball and click var distance = (ball_position - click_position).length() # Distance-based force adjustment (0-1 multiplier) var force_multiplier = 1.0 - (distance / max_distance) force_multiplier = clamp(force_multiplier, 0.0, 1.0) # Ensure 0-1 range # Calculate normalized force direction with distance adjustment var force_direction = (ball_position - click_position).normalized() * force_multiplier # Apply central impulse with adjusted force self.apply_central_impulse(Vector2(force_direction * force)) |
- Class Inheritance: We extend the
RigidBody2D
class, enabling the object to interact with the physics engine (important for realistic push behavior). - Base Force: The
force_base
variable controls the starting push strength. You can easily adjust this value in your game! - Click Detection: The
_process
function checks for left mouse clicks usingInput.is_action_just_pressed
. - Force Calculation:
- The initial force is set to
force_base
. - The
max_distance
defines the range for applying full force. - The distance between the ball (
ball_position
) and the click (click_position
) is calculated. - The
force_multiplier
considers distance, ranging from 1 (full force) to 0 (minimum force) atmax_distance
. Theclamp
function ensures the multiplier stays within the intended range (0-1).
- The initial force is set to
- Force Direction: The force direction vector points from the ball to the click, normalized for consistent push direction. It's then multiplied by the
force_multiplier
to account for distance-based force adjustment. - Impulse Application: Finally,
apply_central_impulse
applies a push to the object using the calculated force direction and adjusted magnitude.
This code effectively creates a click-to-push mechanic that considers the click distance. This approach enhances the player's interaction with your 2D Godot game, making it more intuitive and engaging.
Feel free to experiment with the force_base
and max_distance
values to customize the push behavior in your game! Leave a comment below if you have any questions or want to explore more Godot features for 2D game development.
anyways I need to go figure out what im gonna do with this knowledge
Comments
Post a Comment