bluecity54 Calculate the trajectory and instantiate a bunch of dot sprites along it.
Why I am not able to create segmented line in godot 4.2.1?
yes but I dont know how I will do it.
bluecity54 Which part you don't know how to do; calculating the trajectory or instantiating sprites along it?
godot has a Line2D
where you can set its texture to a small sprite (like the dot shown in the OP), texture set to repeat and the Line2D
configured to Tile
. That'll draw you an aim line.
For a naive length approach, you could take its position (the aim line) and change its point array to the distance of a some collision, e.g.
var distance = position.distance_to($RayCast2D.get_collision_point())
$Line2D.points[1] = Vector2(0, -distance).rotated($Aimer.rotation)
Where $Aimer
will be the blue ball/player thing. This will give you one line section to start with...
my scene tree like that:
and script code here:
extends Node2D
var ball : Sprite2D
var trajectoryLine : Line2D
var rayCast : RayCast2D
var isDragging : bool = false
func _ready():
ball = $Ball
trajectoryLine = $TrajectoryLine
rayCast = $RayCast
# Connect signals
ball.connect("input_event", Callable(self, "_on_ball_input_event"))
rayCast.connect("input_event", Callable(self, "_on_ray_cast_input_event"))
func _process(delta: float):
if isDragging:
update_trajectory_line()
func _on_ball_input_event(viewport: Viewport, event: InputEvent, shape_idx: int):
if event is InputEventMouseButton and event.pressed:
isDragging = true
update_trajectory_line()
func _on_ray_cast_input_event(viewport: Viewport, event: InputEvent, shape_idx: int):
if event is InputEventMouseButton and event.pressed:
isDragging = false
trajectoryLine.clear_points() # Clear trajectory line when the click is released
func update_trajectory_line():
trajectoryLine.clear_points()
# Update the trajectory line based on the current mouse position
var mouse_position = get_viewport().get_mouse_position()
var direction = ball.global_position.direction_to(mouse_position)
var ray_length = ball.global_position.distance_to(mouse_position)
rayCast.cast_to = direction * ray_length
# Draw trajectory line points
trajectoryLine.add_point(ball.global_position)
trajectoryLine.add_point(rayCast.get_collision_point())
but when I click the ball, trajectory line not work, I dont know I missed, maybe I need draw something in Line2D or?
bluecity54 trajectory line not work
"Not working" is a poor description of the problem. In general you should formulate your question better. For example do you need screen border bounces (and how many) or not. Your code does not calculate that at all although your illustration shows a bounce.
So try to describe the problem more clearly. This can even help you figure it out yourself.
spaceyjase tnx so much my way above it is incorrect? or if ur example is short way can you give me detail if possible
xyz sorry, let me explain more details, When I click the ball to draw prediction line, there is not any predicition line on screen, as I shows above screen, when I click the ball, it will draw the prediction line like that. when I click the ball I have like that errors:
- Edited
bluecity54 Those errors indicate that you're trying to connect to a signal that does not exist, namely Sprite and RayCast2D nodes cannot emit input_event
signal.
xyz how can I solve it?
- Edited
bluecity54 In this particular case it's be better to use _input()
event handler to deal with mouse input instead of input_event
signals;
xyz I update my code here:
extends Node2D
var ball: Sprite2D
var trajectoryLine: Line2D
var rayCast: RayCast2D
var isDragging: bool = false
func _ready():
ball = $Ball
trajectoryLine = $TrajectoryLine
rayCast = $RayCast
func _input(event: InputEvent):
if event is InputEventMouseButton and event.pressed and event.button_index == MOUSE_BUTTON_LEFT:
var ball_rect = Rect2(ball.position.x, ball.position.y, ball.texture.get_size().x, ball.texture.get_size().y)
var global_ball_rect = ball_rect.offset(ball.global_position)
if global_ball_rect.has_point(event.global_position):
isDragging = true
update_trajectory_line()
func _process(delta: float):
if isDragging:
update_trajectory_line()
func update_trajectory_line():
trajectoryLine.clear_points()
# Update the trajectory line based on the current mouse position
var mouse_position = get_viewport().get_mouse_position()
var direction = ball.global_position.direction_to(mouse_position)
var ray_length = ball.global_position.distance_to(mouse_position)
rayCast.cast_to = direction * ray_length
# Draw trajectory line points
trajectoryLine.add_point(ball.global_position)
trajectoryLine.add_point(rayCast.get_collision_point())
and it says Invalid call. Nonexistent function 'offset' in base 'Rect2'. for this line of code var global_ball_rect = ball_rect.offset(ball.global_position)
bluecity54 Are you using ChatGPT to generate the code?
- Edited
xyz ı take help abit from there, not always
- Edited
bluecity54 I strongly recommend to follow through the introductory "your first game" tutorial in Godot's official docs to get the basic understanding of the engine workings and to get into habit of reading/searching the documentation. Patching AI generated code won't get you far in your game making endeavors.
xyz yes but sometimes I need it, because there is not enough godot tutorials, just documents not help, for example how can I solve this problem with documents only, yes documents is well but not enough for like me new in game industry.
- Edited
bluecity54 You solve problems with your thinking and your problem solving skills. Documentation only provides technical knowledge to help with that. If a problem seems too hard to tackle, maybe pick a simpler one at first to build up your problem solving muscles.
Again, you may find doing the official introductory tutorial very helpful in the long run.
I could write you a code snippet that directly solves your problem but then you won't learn anything at all and, sadly, I'd just serve as a replacement for a failed ChatGPT query, which would kinda hurt my fragile mammalian feelings
- Edited
actually I understand codes, I am full stack developer for long time, but I am not used to understand structures in godot engine, that is why I am struggle, I need the my way is correct or not due to scene tree
- Edited
bluecity54 It's hard to determine if "your way" is correct because your code won't even run due to some fundamental errors such as non-existent calls. That's just a tick above having syntax errors. A long time full stack developer should be able to deal with that and at least produce a code that runs. By asking if an algorithm is correct while presenting it via code that struggles with api/syntax, you're kind of putting the cart before the horse, making it very hard to answer your questions.
I'm repeating for the third time. Go through the introductory tutorial - it'll pay off.
- Edited
bluecity54 Here's a very simple version of what you're trying to do. It should be enough to get you started. Adding bounces and dotted lines would be a bit more involved. But first things first:
extends Node2D
func _input(e):
if e is InputEventMouseMotion and e.button_mask:
$line.clear_points()
$line.add_point($ball.global_position)
$line.add_point(e.global_position)
if e is InputEventMouseButton and not e.is_pressed():
$line.clear_points()