Why are you dividing here? The more time has passed since the last frame the more should the object move in one frame. In your case the opposite would happen. You might see some differences in player movement speed when going to Project > Project settings > General > Force fps and testing it with different values. Multiplication would be the appropriate operation.
But physically speaking :
Translate()
wouldn’t take time into account?Oh wait! Okay, I get it.
Yeah, you know the velocity and the time passed and you want to know how much space should be traveled. So you multiply the velocity by the time passed, like in the second equation in your post.
Now, it doesn’t move.
Nevermind. It works now.
You have to change the speed to something higher as it currently is very low. Try setting it to something above 1.
Position.y
without creating issues.using Godot;
using System;
public class Player : Area2D
{
private float _velocityY = 250.0f;
private float _screenSizeY;
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
_screenSizeY = GetViewport().Size.y;
}
// // Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(float delta)
{
float deltaY;
if (Input.IsKeyPressed((int)KeyList.W))
{
deltaY = -_velocityY * delta; // px = px/s * s
deltaY = (0 + deltaY < 0) ? deltaY : 0.0f;
GetNode<Area2D>("/root/Pong/Player1").Translate(new Vector2(0.0f, deltaY)); // Player1 moves up
}
else if (Input.IsKeyPressed((int)KeyList.S))
{
deltaY = _velocityY * delta;
deltaY = (_screenSizeY + deltaY > _screenSizeY) ? deltaY : 0.0f;
GetNode<Area2D>("/root/Pong/Player1").Translate(new Vector2(0.0f, deltaY)); // Player1 moves down
}
if (Input.IsKeyPressed((int)KeyList.Up))
{
deltaY = -_velocityY * delta;
deltaY = (0 + deltaY < 0) ? deltaY : 0.0f;
GetNode<Area2D>("/root/Pong/Player2").Translate(new Vector2(0.0f, deltaY)); // Player2 moves up
}
else if (Input.IsKeyPressed((int)KeyList.Down))
{
deltaY = _velocityY * delta;
deltaY = (_screenSizeY + deltaY > _screenSizeY) ? deltaY : 0.0f;
GetNode<Area2D>("/root/Pong/Player2").Translate(new Vector2(0.0f, deltaY)); // Player2 moves down
}
}
}
Maybe I’ll have to use a signal.
Have you taken a look at this node?
also using a KinematicBody2D be better than using a Area2d node which would allow for proper collision
or you could use a proper clamping function from c# or godot
or you could directly move the node position like in the “your first game” example
The method used in the example will only prevent the object’s origin from going beyond the screen. One will have take into account the height of the object, by subtracting/adding the object’s height relative to the origin to the min and max values.
With KinematicBody2D
you could do stuff like move_and_collide
.
Clamp()
, but it didn’t work. No matter what I did, it would either just not move at all or completely ignore my instructions about the screen limit.You could, but the players will not be able to collide with anything if they don’t have a collision shape. That’s why Godot generally warns one that a collision shape should be added after creating a KinematicBody.
Update: The ball can finally bounce!
Ball.cs
using Godot;
using System;
public class Ball : KinematicBody2D
{
[Export]
private float _speed = 4.0f;
private Vector2 _initialPosition;
private Vector2 _direction;
private Random _randomNumber;
private int[] _possibleDirection;
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
_initialPosition = Position;
_randomNumber = new Random((int) DateTime.Now.Ticks); // uses time as seed
_possibleDirection = new int[] {-1, 1}; // -1 for left and 1 for right
_direction = new Vector2(_possibleDirection[_randomNumber.Next(0, _possibleDirection.Length)], 0); // if index = 0, then go left. if index = 1, go right.
}
// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(float delta)
{
KinematicCollision2D collision = MoveAndCollide(_direction * _speed);
if (collision != null && _direction.y == 0) // if the ball collides for the first time, reflect in another way
{
_randomNumber = new Random((int) DateTime.Now.Ticks);
_direction = new Vector2(-_direction.x, _possibleDirection[_randomNumber.Next(0, _possibleDirection.Length)]);
_direction = _direction.Normalized();
MoveAndCollide(_direction * _speed);
}
else if (collision != null && _direction.y != 0) // otherwise, just reflect
{
_direction = collision.Remainder.Bounce(collision.Normal);
_direction = _direction.Normalized();
MoveAndCollide(_direction * _speed);
}
}
public void Reset()
{
Position = _initialPosition;
}
}
Now, I’m trying to figure out how to signal a score.
Wall.cs
using Godot;
using System;
public class Wall : Node2D
{
[Signal]
public delegate void PlayerOneScored(int scorePoint);
[Signal]
public delegate void PlayerTwoScored(int scorePoint);
private int _scoreOne, _scoreTwo;
public int ScoreOne
{
get { return _scoreOne; }
set { _scoreOne = value; }
}
public int ScoreTwo
{
get { return _scoreTwo; }
set { _scoreTwo = value; }
}
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
_scoreOne = 0;
_scoreTwo = 0;
}
public void OnLeftWallBodyEntered()
{
++_scoreTwo; // increment, then output
EmitSignal("PlayerTwoScored", _scoreTwo);
/* One point for player 2 */
}
public void OnRightWallBodyEntered()
{
++_scoreOne;
EmitSignal("PlayerOneScored", _scoreOne);
/* One point for player 1 */
}
}
HUD.cs
using Godot;
using System;
public class HUD : CanvasLayer
{
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
GetNode("/root/Pong/Wall").Connect("PlayerOneScored", this, "OnPlayerOneScored");
GetNode("/root/Pong/Wall").Connect("PlayerTwoScored", this, "OnPlayerTwoScored");
}
public void OnPlayerOneScored()
{
// GetNode<Label>("ScoreOne").Text;
}
public void OnPlayerTwoScored()
{
}
}
*double post
From what I’ve seen in this case and before, I feel like many people who are starting programming hit a wall at some point where they really struggle to push through in order to increase their understanding of programming and learning the programming mindset. I think you just need to push through, and if you can’t find guidance on the parts that aren’t clicking for you. Moving to a different language, just moves when you hit the wall, as even in python you really need to understand things as your program grows, otherwise you end up with a really hacked together unmaintainable mess of a script.
Like a map or an actual mod?
https://developer.valvesoftware.com/wiki/Half-Life:_Alyx_Workshop_Tools/Modding/Source2mod
I’ve used both programming languages and in my opinion, C++ is easier to use but harder to learn, while C# is easier to learn but a bit harder to use.
Maybe its because C++ gives you a lot more control than C#.
I didn’t start out with C# so it’s a bit hard for me to say, but wouldn’t beginners find it easier exactly because you don’t need to think about a bunch of extra details? So it would be faster to create realworld programs that do something complex as you have slightly less mental load from the technical implementation side? I guess there are some C# pitfalls if you don’t understand what it’s doing but, but I thought those would be minor.
That’s why I said that C# is easier to learn, but once you really get to using it it can get annoying.
Well I do remember getting into a bit of a mess in terms of memory use when using WinForms, but I think that was more related to how badly I was using WinForms. In the past few years since moving Thrive over to C#, I’ve really liked it and haven’t had any problems regarding anything that could be solved by using C++.