Godot Objects are moving at varying speeds - mobile

I am new to Godot and have been working on a little project to help me learn. In this project, I have a wheel that turns when the user touches the screen and a ball that comes from the center of the screen outward. When I run the project, the ball will sometimes go unusually faster for a second or two and the wheel will spin almost instantly. I assume this has something to do with the framerate of the game but how do I make sure that this does not happen when I publish my game to the app store?
(All code is in GDScript)
Code for moving ball(ball.gd):
var movev = Vector2(0,0)
func _process(delta):
position += movev
Code for rotating wheel(Wheel.gd):
var goal = 0
func _process(delta):
if goal > rotation:
rotation += deg2rad(1)
Code for when screen is clicked(World.gd):
onready var Wheel = get_node("Wheel/Center")
func _on_Button_button_down():
Wheel.goal += deg2rad(45)
Here is a video I made showing what I am talking about: Video Link
What is causing this behavior and how do I stabilize it?

The parameter delta tells you the time elapsed. You need to multiply your velocity by it.
Consider, for example:
var movev = Vector2(0,0)
func _process(delta):
position += movev
Here you have a velocity movev, and a position position. Velocity is delta position over delta time. Thus, position and velocity are different units, you should not add them.
If you have delta position over delta time, you need to multiply by delta time to get delta position. And that delta position you can add to your position.
See also: instantaneous velocity.
From Godot documentation:
the delta parameter contains the time elapsed in seconds as a floating-point number since the previous call to _process()
Thus, you code should be:
var movev = Vector2(0,0)
func _process(delta):
position += movev * delta
Similarly for angular velocity:
var goal = 0
func _process(delta):
if goal > rotation:
rotation += deg2rad(1) * delta
Note: you may find that your velocity is too slow or too fast after this change. Remember the units. The parameter delta, as the documentation says, is in seconds. Thus, you should express your angular velocities in angle per second and your linear velocity in position displacement per second. I also remind you that the magnitud of the velocity velocity is speed, which is distance of time.

Related

Physics in 3D world in OPENGL with C language only

I've been trying to code a 3D game where the player shoots an arrow and I wanted to do the equations for the 3D. I know the equations for the 2D world where:
x = v0 * cosθ * t
y = v0 * sinθ * t - 0.5 * g * t^2
But how do I use these equations in my 3D world where I have the Z axis?
Instead of making the arrows follow an explicit curve, I suggest simulating the arrow step by step.
What you need to store is a position (with x,y,z coordinates, starting off at the archer's location) and a velocity (also with x,y,z coordinates, starting off as some constant times the direction the player is looking), and some scene gravity (also with x,y,z coordinates, but it'll probably point straight down).
When the simulation progresses by a timestep of t, add t times the velocity to the position, then add t times gravity to the velocity.
This way, you're free to do more interesting things to the arrow later, like having wind act on it (add t times wind to the velocity) or having air resistance act on it (multiply velocity by t times some value a bit smaller than 1) or redirecting it (change velocity to something else entirely) without having to recalculate the path of the arrow.

Predict Where UI Will Land Based on Velocity (scrollrect unity)

I know this may not be a normal question but I think you can help me figure it out.
Background: I want to create a scrollrect (a scrollable UI element) that snaps onto the elements it's scrolling. So that it always comes to a stop with an element in the center. The scrolling of these scroll rects is based on the velocity your finger was swiping at when it left the screen, and if you input a certain velocity it always moves the same amount (within 1 pixel).
So I figured the smoothest way to create this snapping scroll rect would be to predict where the scroll rect would land & then adjust the deceleration rate so that it landed on the nearest element instead.
So basically I would like to:
Turn this loop into a math function where I can input velocity & get out the movement delta.
Be able to figure out what the deceleration rate should be based on the end movement delta & velocity.
Here's the code that the scroll rect uses for its movement:
protected virtual void LateUpdate()
{
//It's probably easiest if you imagine positionX always starting at 0
//but I'm no expert
m_VelocityX *= Mathf.Pow(m_DecelerationRate, Time.unscaledDeltaTime);
if (Mathf.Abs(m_VelocityX) < 1)
{
m_VelocityX = 0;
}
positionX += m_VelocityX * Time.unscaledDeltaTime;
}
Where LateUpdate is called every frame, and positionX is the x position of the UI I am moving. (it holds the UI elements I want to snap too)
ScrollRect Code
LateUpdate Info
Mathf Info
Time.unscaledDeltaTime Info
And here are some velocities and movement deltas (how far it traveled), where the deceleration rate is 0.135 if that's helpful:
Velocity 500 -> 490
Velocity 350 -> 343
Velocity 200 -> 195
Velocity -200 -> -195
Velocity -400 -> -391
Ty all so much for the help! This math is way to hard for me to wrap my head around but I think it will end up being cool!

how to understand steer force for steering behavior

I read a tutorial of how to implement Seek behavior of steering behavior.The link is here
.And this is the graph to illustrate the algorithm:
.
I know the velocity, force, acceleration are all vector. But how come "steering" in formular "steering = desired_velocity - current_velocity" becomes into a force rather than a velocity in this article? why does this make sense? Does it mean that we can mix them in one calculation? Does that mean that a velocity vector add or subtract another velocity vector can product a force vector? if not , why the result is called "force"? I know how the steering behaviors work in AI. The key point of achieving this is that we can sum up all the different steering forces together and get a result total force. This total force can be used in formular "a = F/m" to get the acceleration. After that , we can use this acceleration to calculate new position and velocity of object in game loop update.
Based on my view , the "F" should be steering force , but I'm stucking on understanding the way to calculate it.

Unity3D: TPS shooting without mouse aiming

I'm currently developing some TPS game. I have my player model and camera snapped to its shoulder, and some Empty game object in front of player at some distance for calculating vector for bullets (Yellow diamond at screenshot).
I'm developing for mobile platforms, so there is no mouse; just that Empty game object that points direction of the gun.
So when a fire event occurs I want to apply force to bullet and it will fly in right direction. Here is my code
b.transform.position = transform.position;
b.transform.position += transform.forward;
b.SetActive(true);
var rb = b.GetComponent<Rigidbody>();
print((Aim.position - transform.position).normalized);
rb.AddForce((Aim.position - transform.position).normalized * Thrust);
Aim is my EmptyGameObject that points direction, transform is GunEnd gameobject, and b is my bullet instance. So if I try shoot from default player position bulet flies correct from GunEnd to Aim object.
But if I rotate character for example more that 90 degree left, bullets start to fly in some weird trajectory
So, can anybody help me how to correct send bullets?
When you move it´s position with b.transform.position += transform.forward; you might be setting it in an odd place if the transform does not rotate when you aim (and by what I can see in the screenshot, it is not rotating as its components in the transform.rotate remain the same in y). Try moving it with the vector you find with the Aim, like this:
b.transform.position += (Aim.position - transform.position).normalized;

Source engine styled rope rendering

I am creating a 3D graphics engine and one of the requirements is ropes that behave like in Valve's source engine.
So in the source engine, a section of rope is a quad that rotates along it's direction axis to face the camera, so if the section of rope is in the +Z direction, it will rotate along the Z axis so it's face is facing the camera's centre position.
At the moment, I have the sections of ropes defined, so I can have a nice curved rope, but now I'm trying to construct the matrix that will rotate it along it's direction vector.
I already have a matrix for rendering billboard sprites based on this billboarding technique:
Constructing a Billboard Matrix
And at the moment I've been trying to retool it so that Right, Up, Forward vector match the rope segment's direction vector.
My rope is made up of multiple sections, each section is a rectangle made up of two triangles, as I said above, I can get the position and sections perfect, it's the rotating to face the camera that's causing me a lot of problems.
This is in OpenGL ES2 and written in C.
I have studied Doom 3's beam rendering code in Model_beam.cpp, the method used there is to calculate the offset based on normals rather than using matrices, so I have created a similar technique in my C code and it sort of works, at least it, works as much as I need it to right now.
So for those who are also trying to figure this one out, use the cross-product of the mid-point of the rope against the camera position, normalise that and then multiply it to how wide you want the rope to be, then when constructing the vertices, offset each vertex in either + or - direction of the resulting vector.
Further help would be great though as this is not perfect!
Thank you
Check out this related stackoverflow post on billboards in OpenGL It cites a lighthouse3d tutorial that is a pretty good read. Here are the salient points of the technique:
void billboardCylindricalBegin(
float camX, float camY, float camZ,
float objPosX, float objPosY, float objPosZ) {
float lookAt[3],objToCamProj[3],upAux[3];
float modelview[16],angleCosine;
glPushMatrix();
// objToCamProj is the vector in world coordinates from the
// local origin to the camera projected in the XZ plane
objToCamProj[0] = camX - objPosX ;
objToCamProj[1] = 0;
objToCamProj[2] = camZ - objPosZ ;
// This is the original lookAt vector for the object
// in world coordinates
lookAt[0] = 0;
lookAt[1] = 0;
lookAt[2] = 1;
// normalize both vectors to get the cosine directly afterwards
mathsNormalize(objToCamProj);
// easy fix to determine wether the angle is negative or positive
// for positive angles upAux will be a vector pointing in the
// positive y direction, otherwise upAux will point downwards
// effectively reversing the rotation.
mathsCrossProduct(upAux,lookAt,objToCamProj);
// compute the angle
angleCosine = mathsInnerProduct(lookAt,objToCamProj);
// perform the rotation. The if statement is used for stability reasons
// if the lookAt and objToCamProj vectors are too close together then
// |angleCosine| could be bigger than 1 due to lack of precision
if ((angleCosine < 0.99990) && (angleCosine > -0.9999))
glRotatef(acos(angleCosine)*180/3.14,upAux[0], upAux[1], upAux[2]);
}

Resources