4.2.2
Hello everyone,
I’m building a top-down fishing game and have my fish moving in random directions created with the RNG and Vector2’s. Essentially, I’m creating a new vector and using that and a constant speed to set the fish’s velocity, then using acos(dot product) to find the angle at which the sprite needs to rotate the match the new direction. However, the sprite and the movement never seem to match up. Here is what I have so far.
const SPEED = 10
var rng = RandomNumberGenerator.new()
var oldVector = Vector2(1, 0)
func randomize_movement():
var randX = rng.randf_range(-1, 1)
var randY = rng.randf_range(-1, 1)
var direction = Vector2(randX, randY)
var angleToRotate = acos(direction.dot(oldVector))
parent.velocity = direction * SPEED
spriteNode.rotate(angleToRotate)
oldVector = direction
I’ve tried looking for answers but have come up short. I don’t use global positioning as the sprite’s movement is meant to be random, angleToRotateseems correct but the movement itself is way off from where the sprite is pointing. I’ve tried doing the math using online calculators and the math maths, but the direction vector doesn’t match up with the angle. I’m kind of at a loss, and any help would be appreciated. Thanks!
I am not quite sure what the dot product does but you could also use the build in look_at() function, which rotates the Node towards given global coordinates, to always rotate towards the movement direction:
I just tried your method and it doesn’t work, though I agree that it seems simpler and my answer probably lies somewhere there. var angleToRotate = acos(direction.dot(oldVector)) essentially returns the angle between two vectors (in radians, of course). I tried using that to manually turn the sprite but I feel I’m either missing something or it doesn’t work the way I think it’s supposed to.
I’ve also just tried normalizing the direction with Vector2(randX, randY).normalized()
The sprite is simply turning in random directions.
Isn’t the sprite supposed to turn into random directions? I mean you are basically generating a random angle, depending on your new random direction vector.
I think it has to do with how my sprite is positioned now that I’m fiddling with the look_at function. I thought by turning the node in the editor I wouldn’t be messing with its orientation (according to Godot) but that seems to unfortunately be the case.
My only thought now would be to re-upload the sprite oriented the correct way, which is a hassle but worth keeping in mind in the future.