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

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

Related

Godot: Position2D node not sending proper location

I am new to Godot and am currently making a roguelike game. It's in the very beginning stages, but I am stuck on one specific part. I have a system set up so that when you click on a certain space, the player will move to the position 2D node associated with that space. However, whenever I actually try it out, after clicking in the area the player moves to what appears to be a set area instead of where I want.
With this problem, I have no real idea how to fix it or what to search for on the internet because I am new to Godot and game development. I was hoping someone could answer my question directly here.

Smooth look_at() in 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 :)

Collisions in Authoritative Server for Socket IO game

I am trying to make an io game using Phaser and NodeJS/Socket.io. I realized to prevent cheating I need to make an authoritative server that runs the game.
While trying to architect the server, I wasn't sure what to do for collisions. Right now my idea is to divide the canvas into tiles and check each tile for collisions but that still seems like alot of work for each update. On the flip side I could loop through all attack sprites and look for overlaps with specific tiles, and then check the sprites in those tiles. I am afraid this is too slow, but I might be wrong. Does anyone have any better ideas?
And does anyone have any recommendations for server side physics engines so that maybe I don't have to do the math for collisions and movement?
Good thinking. The best way to do it afaik is to use a method called binary space partitioning, where you split your world up into chunks, then check for collision of objects only within those chunks. If you have an object on one side of your world, for instance, you will not need to check for a collision between that object and another object on the other side of your world in a different chunk.
If you haven't done it before, you might not want to write your own physics engine since there are so many options available. Even Bullet3D has a web port known as Ammojs. Iirc there is a version that works with node, but you'll have to do some work with multithreading or replace the webworkers to get it working right.
Here is a blog post by someone else who got a socket and node game up and running with Box2d as the physics engine:
http://paal.org/blog/2012/07/06/running-box2d-on-server-with-node-js-via-socket-io/
https://www.youtube.com/watch?v=5ty6wA2wSX4
Keep in mind, even if you're running it on an authoritative server at say, 60fps, at some point you might want to run it on the client as well, especially if the client is updating between renders from the server. You MIGHT have to write some prediction or interpolation code so that the animations don't look super janky.
Hope this helped.

Phaser - Moving sprites with another sprite

I am starting with phaser and I want to make a character and some rocks, I want these rocks to be moved by this character.
My intention is that when the character is touching the rock he can't go trough it and he can move it in the direction he is pointing.
Any idea how can I do it? Tell me some functions or Objects to use.
Depending on the physics system used, You will need to use a collision function. In Arcade physics this code would be in your update function:
game.physics.arcade.collide(player, rock, pushRock);
Then you would declare a function pushRock, that will handle what happens when your player collides/touches the rock. In your case, push the rock in the direction that the player is facing.
I recommend looking here and searching around, where you might find some more help or examples.

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.

Resources