c# - (XNA) Velocity doesnt reach 0 -
I recently started working with XNA and there is a little problem. I have a phantom that is completely fine and everything runs, but velocity does not reach 0. It's always 2.5 or -2.5 or something like that.
Protected Override Zero Update (GameTime GameTime) {// Control (if Keyboard GateState). ICaiddown (Keys.) {If (well.x maxVel * (- 1)) vel.X - = acc.X * 3 * (float) game time.Apple group gametime TotalSeconds; Player. SpritePos.X + = vel.X * (float) game time. AspenGametime Kolshakand; } And if (vel.X! = 0) {// Velocity should be set to 0, if no button is pressed. It does not work // it just stops at 2.5 or -2.5, so that no button is pressed, the speed of the connĂrent is slowed. If (vel.X> 0) {player.spritePos.X + = vel.x * (float) game time.appalgroup timeshundred; Vel.x - = acc.X * (float) game time. AspenGametime Kolshakand; } If (vel.X <0) {player.spritePos.X + = vel.x * (float) game time. ElapsedGameTime TotalSeconds; Vel.x + = acc.X * (float) game time. Applegate Scheduling Clause; }} // Collision with Screen Boundary (Player.pritPos.X + player.spriteTextxture.Width> GraphicsDisvision.Visport.Width || Player.pritPos.X As I said, I'm new to coding with C # / XNA, so this code is very impure.
The reason this part is:
if (vel.x & Gt; 0) {player.pritPos.X + = vel.x * (float) game time. ElapsedGameTimeCollus; Vel.x - = acc.X * (float) game time. AspenGametime Kolshakand; } If (vel.X <0) {player.spritePos.X + = vel.x * (float) game time. ElapsedGameTime TotalSeconds; Vel.x + = acc.X * (float) game time. Applegate Scheduling Clause; } If I rewrite it using the value of acc.X provided by you and I'm guessing gametime. Time has expiredThere is a second running on the total turnover of 1/30 and XANA 30 updates:
if (vel.X> gt; 0) {vel.x - = 150 * (float) 1/30; } If (vel.X <0) {vel.x + = 150 * (float) 1/30; } Which is:
if (vel.X> 0) {vel.x - = 5; } If (vel.X <0) {vel.X + = 5; } So if vel.X is 2.5, then it will reduce it to -2.5. In the next update it is -2.5, so it grows to 2.5, and it keeps switching between the two but it never gets 0 to do this.
To fix this, you can try:
if (vel.X> 0) {player.spritePos.X + = vel.x * ( Name) gameTime.ElapsedGameTime TotalSeconds; Vel.x - = acc.X * (float) game time. AspenGametime Kolshakand; If (vel.X <0) vel.x = 0; } If (vel.X <0) {player.spritePos.X + = vel.x * (float) game time. ElapsedGameTime TotalSeconds; Vel.x + = acc.X * (float) game time. Applegate Scheduling Clause; If (vel.X> 0) vel.x = 0; }
Comments
Post a Comment