Smooth look_at() in Godot - godot

I am working in GDScript, and I'm trying to get the player to aim their weapon in the direction of the mouse cursor. The look_at() function is great, but it's not the smooth movement I'm looking for.
So, instead I've been experimenting with the lerp() math operation to make it smooth... but its very jittery and buggy movement which is obviously not ideal.
func _process(delta):
`rotation_degrees = lerp(rotation_degrees,rad2deg(get_angle_to(get_global_mouse_position())),aim_speed)
`
1 See the image of my code here.
I can't work out if I'm doing something really stupid as I'm fairly new to Godot. Any help would be massively appreciated!

Please use_physics_process() instead of _process()
_process runs at the fastest possible rate whenever it gets the CPU. That makes it jittery. It is not ideal for player movements since you will need consistency in your frames.
_physics_process() is consistent as it is frame independent and is ideal for player movement.
if you want more details I recommend you check this video by Godot Tutorials
Btw look_at() should work just fine :)

Related

How to get the position of a different object in gdscript?

I have a player, and I want to be able to detect when they are near a ball so they can press 'E' to kick it.
I'm new to godot and I'm probably just dumb but I can't figure out how to get the position of another object.
I have 3 separate scenes: MainScene(Node2D), Player(KinematicBody2D), and Ball(RigidBody2D). I want to attach a script to one of the scenes that detects how close the Player is to the Ball so they can kick it.
I'm not sure if I should attach the script to the MainScene, the Player scene, or the Ball scene and to be honest, I'm not entirely sure how the code should look. I'm not very familiar with the functions and the node/scene hierarchy is a little confusing.
I am familiar with Python, I just think I'm overwhelmed with this engine (its my first time using a game engine) and I'm having a bit of a tough time grasping it.
Any help would be greatly appreciated!
Side-quest: If you are feeling extra helpful I also need to figure out how to stop a RigidBody2D object from moving until the player hits 'E' on it to kick it!
For this you want an Area2D, which is purpose-built for "detecting nearby objects" without interacting with them physics-wise. Have an Area2D as a child of the Player node and connect the Player node to that Area2D's body_entered signal. Here's a tutorial on using Area2D for further info; this tutorial also links to various other tutorials that could be helpful.
i think you maybe use function
get_overlapping_bodies()
(of Area2D node) for detect another areas or body

Motion framework - create repeating animation? (CosmicMind)

Firstly, thank you for the great frameworks (Material and Motion) and generally awesome documentation.
I have been migrating to various Material and Motion classes in my app and enjoying it, but i have a situation I can't quite figure out an elegant way to handle with Motion.
I like to have a repeating background pulse animation that triggers on an IBAction and stops on an IBAction, but motion animations are 'one shot', and im not sure how to trigger a long running animation (unknown duration, repeating) and keep a handle on it, then stop it.
Is this a paradigm supported by Motion?
Thank you

Persisting drawn frames in Gloss

I've been messing around with Gloss for a little bit, getting a feel for it, and I really like it!
I'm having one problem though, and that is that I'm not sure how to persist drawings for more than one frame. I can draw one Picture at a time, but I'm not sure how to keep them on the screen. I can always keep a list of Pictures and display those, but that seems like it could be very inefficient, so I wanted to check if there was a better way to do it. I've been going through the documentation and haven't found anything.
Any help would be great! Thanks!
Unless Gloss has grown significantly in the past year, you can't. The mental model is simple, you have some code that mutates the state, and every period you have something that renders the new state. No one is saying this is efficient, but this was developed as an educational library.

How to read Location of a view whilst it is being animated

I am animating a small space ship (derived from UIView) and periodically (whilst in animation) send it a PointF to check if this is near the space ship's current position.
However, when reading out the Frame position of the View it keeps returning the starting position before the animation started.
I think this is by design but it is causing me big problems since the space ship(s) should move independently along Paths and it is very tricky for me to do this by hand.
Is there another way - and/or has anyone some sample code?
Not sure of a workaround for your issue, but I have some suggestions on game development for iOS.
Your problem is one of the reasons why using GUI frameworks like UIKit/CoreGraphics for games isn't a good idea. For both performance reasons, as well as the fact as they aren't designed for it.
If you are looking for a simple framework for making games on iOS, have you looked at MonoGame? If you are doing lots of animations, we also use XNA Tweener along with MonoGame to get some lifelike animations.
PS - check out our game here.

2D platformers: why make the physics dependent on the framerate?

"Super Meat Boy" is a difficult platformer that recently came out for PC, requiring exceptional control and pixel-perfect jumping. The physics code in the game is dependent on the framerate, which is locked to 60fps; this means that if your computer can't run the game at full speed, the physics will go insane, causing (among other things) your character to run slower and fall through the ground. Furthermore, if vsync is off, the game runs extremely fast.
Could those experienced with 2D game programming help explain why the game was coded this way? Wouldn't a physics loop running at a constant rate be a better solution? (Actually, I think a physics loop is used for parts of the game, since some of the entities continue to move normally regardless of the framerate. Your character, on the other hand, runs exactly [fps/60] as fast.)
What bothers me about this implementation is the loss of abstraction between the game engine and the graphics rendering, which depends on system-specific things like the monitor, graphics card, and CPU. If, for whatever reason, your computer can't handle vsync, or can't run the game at exactly 60fps, it'll break spectacularly. Why should the rendering step in any way influence the physics calculations? (Most games nowadays would either slow down the game or skip frames.) On the other hand, I understand that old-school platformers on the NES and SNES depended on a fixed framerate for much of their control and physics. Why is this, and would it be possible to create a patformer in that vein without having the framerate dependency? Is there necessarily a loss of precision if you separate the graphics rendering from the rest of the engine?
Thank you, and sorry if the question was confusing.
There are no reasons why physics should depend on the framerate and this is clearly a bad design.
I've once tried to understand why people do this. I did a code review for a game written by another team in the company, and I didn't see it from the beginning but they used a lot of hardcoded value of 17 in their code. When I ran the game on debug mode with the FPS shown, I saw it, FPS was exactly 17! I look over the code again and now it's clear: the programmers assumed that the game will always have a 17 FPS constant frame rate. If the FPS was greater than 17, they did a sleep to make the FPS be exactly 17. Of course, they did nothing if the FPS was smaller than 17 the game just went crazy (like when played at 2 FPS and driving a car in the game, the game system alerted me: "Too Fast! Too Fast!").
So I write an email asking why they hardcoded this value and use it their physics engine and they replied that this way they keep the engine simpler. And i replied again, Ok, but if we run the game on a device that is incapable of 17 FPS, your game engine runs very funny but not as expected. And they said that will fix the issue until the next code review.
After 3 or 4 weeks I get a new version of the source code so I was really curious to find out what they did with the FPS constant so first thing i do is search through code after 17 and there are only a couple matches, but one of them was not something i wanted to see:
final static int FPS = 17;
So they removed all the hardcoded 17 value from all the code and used the FPS constant instead. And their motivation: now if I need to put the game on a device that can only do 10 FPS, all i need to do is to set that FPS constant to 10 and the game will work smooth.
In conclusion, sorry for writing such a long message, but I wanted to emphasize that the only reason why anyone will do such a thing is the bad design.
Here's a good explanation on why your timestep should be kept constant: http://gafferongames.com/game-physics/fix-your-timestep/
Additionally, depending on the physics engine, the system may get unstable when the timestep changes. This is because some of the data that is cached between frames is timestep-dependant. For example, the starting guess for an iterative solver (which is how constraints are solved) may be far off from the answer. I know this is true for Havok (the physics engine used by many commericial games), but I'm not sure which engine SMB uses.
There was also an article in Game Developer Magazine a few months ago, illustrating how a jump with the same initial velocity but different timesteps was achieved different max heights with different frame rates. There was a supporting anecdote from a game (Tony Hawk?) where a certain jump could be made when running on the NTSC version of the game but not the PAL version (since the framerates are different). Sorry I can't find the issue at the moment, but I can try to dig it up later if you want.
They probably needed to get the game done quickly enough and decided that they would cover sufficient user base with the current implementation.
Now, it's not really that hard to retrofit independence, if you think about it during development, but I suppose they could go down some steep holes.
I think it's unnecessary, and I've seen it before (some early 3d-hw game used the same thing, where the game went faster if you looked at the sky, and slower if you looked at the ground).
It just sucks. Bug the developers about it and hope that they patch it, if they can.

Resources