How to change this angle?

Godot Version

3

I want that ball to move at random angles but not to be fixed angle like this, i want it to face to up.

extends KinematicBody2D

var speed = 750
var increase_amount = 10
var velocity = Vector2()
var collided = false

signal score_increment

func start(pos):
	position = pos
	var angle = rand_range(-PI / 6, PI / 6)
	if randf() > 0.5:
		angle = rand_range(5 * PI / 6, 7 * PI / 6)
	rotation = angle
	velocity = Vector2(speed, 0).rotated(rotation)
	collided = false

func _ready():
	randomize()
	# Ensure the ball starts moving when the game starts
	start(position)

func _physics_process(delta):
	if velocity != Vector2():  # Check if velocity is initialized
		var collision = move_and_collide(velocity * delta)
		if collision:
			velocity = velocity.bounce(collision.normal)
			if collision.collider.is_in_group("Paws") and not collided:
				print("Collided with Paw")
				emit_signal("score_increment")
				speed += increase_amount
				velocity = Vector2(speed, 0).rotated(rotation)
				collided = true
				print(speed)
		else:
			collided = false
	else:
		print("Velocity not initialized")

# Call this function to start the ball movement with initial position
func init_ball(start_pos: Vector2):
	start(start_pos)

But its not facing down every time i just want to face only up.

Well, the start function seems to randomize, does it? Is the start random or not?

When else do you want it to randomize? After a collision?

yes its starts random, and yes i would like to change angle after collision to make it face not down every time

This might have been my fault you probably have to change this line:

velocity = Vector2(speed, 0).rotated(rotation)
# to this
velocity = velocity.normalized() * speed
1 Like

now it not even moving :sweat_smile:

Can you print out the velocity before that?

1 Like

like this ?

func start(pos):
	position = pos
	var angle = rand_range(-PI / 6, PI / 6)
	if randf() > 0.5:
		angle = rand_range(5 * PI / 6, 7 * PI / 6)
	rotation = angle
	print(velocity)
	velocity = velocity.normalized() * speed
	collided = false

no in the physics_process-method after:

if collision:
			velocity = velocity.bounce(collision.normal)
			print(velocity)

just for testing purposes

1 Like

— Debugging process started —
Godot Engine v3.5.3.stable.official.6c814135b - https://godotengine.org
OpenGL ES 3.0 Renderer: GeForce GTX 550 Ti/PCIe/SSE2
Async. shader compilation: OFF

Velocity not initialized
Body entered:LeftPaw
Body entered:RightPaw
Body entered:Borders
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
Velocity not initialized
— Debugging process stopped —

i think that has to be “==” and not “!=”:

if velocity != Vector2():
1 Like

okay
— Debugging process started —
Godot Engine v3.5.3.stable.official.6c814135b - https://godotengine.org
OpenGL ES 3.0 Renderer: GeForce GTX 550 Ti/PCIe/SSE2
Async. shader compilation: OFF

Body entered:LeftPaw
Body entered:RightPaw
Body entered:Borders
— Debugging process stopped —

But it collided right?

yes but how i dont understand cuz its not moving :sweat_smile:


its on the center but colliding

Put a print here:

var collision = move_and_collide(velocity * delta)
print(collision)

collision has to be a value != null. I have to go now but maybe this helps

1 Like

okay no problem, thanks(yes its = null)
[Object:null]
Body entered:LeftPaw
Body entered:RightPaw
Body entered:Borders
[Object:null]
[Object:null]
[Object:null]

ah i see now thats body entered isnt my ball thats my deatharea

extends Area2D


func _ready():
	connect("body_entered", self, "_on_Area2D_body_entered")

func _on_Area2D_body_entered(body):
	print("Body entered:", body.name)
	if body.is_in_group("Ball"):
		print("coll")
		body.queue_free()
	
		

Just add the above lines from your start function to your physics process right before you set the velocity

1 Like

what is this :joy:


now its not rotating and its even more ugly thats before :sweat_smile:

extends KinematicBody2D

var speed = 750
var increase_amount = 10
var velocity = Vector2()
var collided = false

signal score_increment

func start(pos):
	position = pos
	velocity = Vector2(speed, 0).rotated(rotation)
	collided = false

func _ready():
	randomize()
	# Ensure the ball starts moving when the game starts
	start(position)

func _physics_process(delta):
	if velocity != Vector2():  # Check if velocity is initialized
		var collision = move_and_collide(velocity * delta)
		if collision:
			var angle = rand_range(-PI / 6, PI / 6)
			if randf() > 0.5:
				angle = rand_range(5 * PI / 6, 7 * PI / 6)
			rotation = angle
	
			velocity = velocity.bounce(collision.normal)
			if collision.collider.is_in_group("Paws") and not collided:
				print("Collided with Paw")
				emit_signal("score_increment")
				speed += increase_amount
				velocity = Vector2(speed, 0).rotated(rotation)
				collided = true
				print(speed)
		else:
			collided = false
	else:
		print("Velocity not initialized")

# Call this function to start the ball movement with initial position
func init_ball(start_pos: Vector2):
	start(start_pos)

Cant i move at random direction not random angle and fixing my mouse angle ?

:sweat_smile: well, I will need to take a deeper look, but cannot right now. If you are still having issues I will look at it on several hours when I can focus on it.

okay, no problem, thanks

Guys i made it like this, and i think it works pretty fine but of course without @herrspaten and @nutlike4693 it would be harder.Thanks so much :+1: :+1: :+1: :+1: :+1:

var speed = 750
var increase_amount = 10
var rotation_speed = 2 # Adjust this value for rotation speed
var velocity = Vector2()
var collided = false

signal score_increment

func start(pos):
	position = pos
	# Ensure the ball starts moving at an angle not too close to horizontal
	var min_angle = deg2rad(30)
	var max_angle = deg2rad(360)
	var initial_angle = rand_range(min_angle, max_angle)
	
	velocity = Vector2(speed, 0).rotated(initial_angle)
	
	# Check if velocity is too horizontal and reassign if necessary
	while abs(velocity.angle()) < min_angle or abs(velocity.angle()) > max_angle:
		initial_angle = rand_range(min_angle, max_angle)
		velocity = Vector2(speed, 0).rotated(initial_angle)
	
	collided = false

func _ready():
	randomize()
	# Ensure the ball starts moving when the game starts
	start(position)

func _physics_process(delta):
	if velocity != Vector2():  # Check if velocity is initialized
		# Move the ball and check for collision
		var collision = move_and_collide(velocity * delta)
		if collision:
			# Reflect the velocity based on the collision normal
			velocity = velocity.bounce(collision.normal)
			if collision.collider.is_in_group("Paws") and not collided:
				print("Collided with Paw")
				emit_signal("score_increment")
				speed += increase_amount
				# Maintain direction but increase speed
				velocity = velocity.normalized() * speed
				collided = true
				print(speed)
		else:
			collided = false

		# Apply rotation
		rotation += rotation_speed * delta

	else:
		print("Velocity not initialized")

# Call this function to start the ball movement with initial position
func init_ball(start_pos: Vector2):
	start(start_pos)

2 Likes