How exactly is addforce impulse calculacted? - unity3d

I'm having a bit of a problem that I have been trying to figure out for the last ~5 days, basically, I want an object to move by a specific amount using AddForce, for example, lets say 1 unit, however, when I use this snippet of code
body.AddForce(new Vector3(1,0,0), ForceMode.Impulse);
instead of moving it by 1 unit, so that the position is
1,0,0
It moves it by
0.03291207,0,0
so I realized that there is a calculation here, so I thought maybe for every 1 velocity, it moves it by 0.0329...., so I figured if I were to multiply the velocity by around 30, however, if I put
body.AddForce(new Vector3(30,0,0), ForceMode.Impulse);
, but now it moves it by
37.92695,0,0
, so I was really confused. So after making a spreadsheet and then copying and pasting different velocities and how they work, I finally figured it out. so it works a something a bit like this :
basically, there are 3 things, the input value [which we will call X], the velocity, and the position. the Velocity starts outs as the X, and every frame its decreased by - 0.2355
the position starts out as whatever position it is, and then there is a speed value, lets call it Y, now Y starts out as X divided by 100 and multiplied by 2, so for example if X is 1, Y would start out as 0.02, then, ~0.0048 is removed from Y, so now it's 0.0152, and then every frame, the position is increased by the Y value, and also every frame, Y is being decreased by ~0.0048, ~0.0048 being the friction [I think].
and now that I knew how it worked, I could reverse engineer, and I did! so I figured out that to get the object to move by 1 unit, I needed to input around 5.02 velocity. So, you might be wondering if I figured it out, why am I posting it here? well, the problem arises when I try to move it on the Z-axis at the same time, like
body.AddForce(new Vector3(1,0,1), ForceMode.Impulse);
now it moves it by on both axis by
0.0500636, 0, 0.0500636
instead of
0.03291207,0,0.03291207
. So this really made me confused, so then I made a spreadsheet and then copied and pasted different velocities and how they work. And it turns out the formula is still the same, however, 0.2355 and 0.0048 are changed, for example when it's
body.AddForce(new Vector3(1,0,1), ForceMode.Impulse);
, 0.0048 is actually 0.00332966 so Y is being decreased by 0.00332966 every frame, but if there are 2 different numbers, then, they both have different 0.0048's, for example, if it's
body.AddForce(new Vector3(1,0,2), ForceMode.Impulse);
. for 1 the value is 0.00210584 meaning Y gets decreased by 0.00210584 every frame, but for 2 in the same vector, 0.00421168 is the magic number, meaning Y is decreased by 0.00421168 every frame.
And that's where I'm stuck, I can't figure out where these numbers are coming from. I tried dividing it, multiplying it, subtracting and adding and I just cant figure it out. So I would really appreciate help from the physics experts here. Keep in mind that I don't know that much about physics and only know a bit about algebra and stuff like that, so I have no idea what the correlations are. If anyone needs it, here is the table I made showcasing every "magic number" from 1 to 6, the colored squares are squares that are quite similar
Sorry if this post was a bit confusing and sorry if I'm not supposed to ask this here, but I'm not sure where else on stack could i ask this, this whole topic is a bit confusing so I'm not sure if I explained it well enough, and I'm a bit tired, so if you didn't understand anything just leave a comment and I will try to answer it, Thanks in advance for any help :)

Related

Forward facing vector is a bit offset

I am making an FPS game and I need for the forward facing vector to hit exactly where the crosshair is aiming.
This is my current blueprint.
It takes all references from the camera position where the players head should be. (as it usually is in FPS games).
When "shooting" the vector it is slightly offset though. (pink dot near crosshair)
Things I have tried:
Increasing distance of vector makes the problem go away but it then becomes inconsistent, which means it's a bad solution to the problem :(
Manually changing axis values, but that was also very inconsistent.
Changing between 3 different nodes of taking rotation from the camera, they all (didn't) work the same way :/
Maybe there is an issue of the values that I am taking, although the starting position of the camera seems to be correct.
Thank you for any insight you may have!
Suggested by user Ruzihm the issue was that the crosshair was off-center. My blueprints were actually okay.
So for anyone looking see if your crosshair is in the center.

JavaFX - Creating basic jumping mechanic

I've been looking for awhile now for someone who had created a good example of making good physics in JavaFX, or even just a 'basic jumping mechanic' as the title says. I can't really find any information on it and I'm not really sure how to implement the idea.
All I want is a basic example, or just an explanation, or even just a point in the direction of what element of JFX I'm going to use.
Any help is appreciated.
Thanks
I'm assuming you already have some sort of game loop that ticks 60 times a second such as the AnimationTimer. If you want the jump height to be something like 200 pixels, you need to set and objects y-velocity (velocity is added to the objects location every tick) to a large negative number (as the object is moving upwards) and add a smaller amount every tick to this velocity until it hits zero, (this will be the top of the jump) and then keep adding this value to the y-velocity until it reaches the ground or collides with something. (This value will be your gravity constant)
In essence, you need to set the y-velocity to a high value then take away small increments every tick to slow the jump until the y-velocity hits 0, then begin adding the gravity constant again until the object hits the ground, hope this helps :)

How to smooth movement in Unity 4.3 2D with AddForce?

at the moment i'm working on a 2D plattformer and the work is going very well. But there is one Problem i can't get rid of.
The player can use a dash, which should move the player very fast in the direction he is looking. My problem is, that the player game object instant appears at the target location... like a teleport.
I'm using the AddForce Function to move the game object. On jumping i'm using AddForce as well and there it works really nice, i get a smooth jump movement.
The only different between dash and jump is that on jump i apply force to the y axis and on dash to the x axis. Changing the amount of force doesn't affect the movement only the distance.
Does anyone have an idea what i'm doing wrong?
// Dash
rigidbody2D.AddForce (new Vector2((dashSpeed * direction), 0));
// Jump
rigidbody2D.AddForce (new Vector2(0, jumpForce));
Best,
Verdemis :)
EDIT: I can not access my project at the moment, but i will try to show you what i have done.
Note: dashSpeed is a float value, at the moment something like 3500
and direction contains 1 or -1, depending on the direction the player is looking. The Dash code is part of the Update method.
// Dash
if(Input.GetKeyDown(dashKey))
rigidbody2D.AddForce (new Vector2((dashSpeed * direction), 0));
What is your direction vector, is it normalized? Since multiplying non-normalized vectors can be rather hazardous. Do you have more of the script to show?
EDIT: You should always do physics things in the FixedUpdate loop(link).
Ok i could solved the problem. My problem was that i only did this AddForce only once. The AddForce code was only executed in a single frame. I added a time which i count down to define how long the dash movement gonna be!
The problem may be that you are using a very big force, I was messing around with some physics today and realized that even a force of 100 almost looks instant. Try making a smaller force number to see if that helps you. I just tested making a smaller number and that does not work.

How to get a ball/sphere to stop

I got a field in unity3d that has some depressions in it (like small holes). The field's slope always leads towards the nearest depression.
A sphere is dropped at random somewhere in the field, rolls around a bit until it stops in one of the depressions.
The problem is, this is taking too long. It could roll around for 5-10 seconds until it stops. I'd like to stop faster.
Any ideas how I can achieve this?
Edit: The main issue is when the ball is next to the depression, but it has speed that is 90 degrees from the hole, then it starts going in circles and takes a while to stop.
Ok, after getting some advice in the comments, and experimenting, this is the way I solved it:
Apply a small measure of strength towards the depression
If the current velocity is more than 30 degrees away from the center of the depression, slow the ball (apply strength in the opposite direction of the velocity)
IF the ball gets very near the center of the depression, stop it and place it in the center
Thanks for all the tips. If anyone comes up with a better way, I'm still open to suggestions.

Work out speed from the UIAccelerometer

I just wanted to know how I could get the speed that the UIAccelerometer is moving in so that I know how fast to move a UIView.
Oh boy this is a bit tricky.
The accelerometer does not give you Velocity, but as we all know this is acceleration. But you can figure it out (but its not that easy).
We need to use some simple physics calculations. Using
V(now) = V(previous) + acceleration(from accelerometer)*time(seconds since you last made a reading);
In code you would need to do a few things.
You must figure out when you are not moving. Eg leave device still for a second.
When the device moves you know the device started at an initial Velocity of 0m/s
Ok so lets pretend to make this real simple and we only check the accelerometer every 1 second (later on maybe do 10x a second.
Ok, we get the first reading of 1 second and the acceleration was 2.
So our velocity after 1 second from resting would be = 0 + 2*1 = 2 m/s
GREAT, now another second has passed (total time passed is 2 seconds), acceleration 2.5 velocity now would be
V(now) = Vpervious (2) + T(passed since previous reading) 1 * 2.5 = 2 + 1*2.5 = 4.5 m/s
Remember here that T here is time passed since last reading not total time.
If I am right (someone can correct me, the accelerometer is sending back the actual value of a in our equations). So you should be able to plug that in.
Also the accelerometer will be giving you 3 different values (x,y,z). So maybe first start with one direction then maybe y for another.
Would love to know if you got this working. John.