[XNA] beschleunigte Bewegung

Du must erst using System.Diagnostics; einbinden, damit der Debug findet.
Außerdem das Fenster unter Ansicht->Ausgabe öffnen oder Strg+W,O
 
immerhin die Ausgabe funktioniert nun.

ich lasse mir zuerst die halbe Ballbeschleunigung ausgeben, dann die Ballgeschwindigkeit*t und dann t².
Komischerweise wechslen sich nur immer 2 zaheln ab, obwohl ich ja 3 Variablen ausgeben lasse. Die Zaheln sind auf jeden Fall imerm gleich.

8333350,000277778900,8333350,000277778900,8333350,000277778900,.....

ich bin immer mehr verwirrt...

-----------edit---------------
ich hab jetzt nen ansatz. ich hab mir einfach nru mal meine variable t ausgeben lassen, die die Zeit mitzählt. aber t ist konstant....
PHP:
float t = (float)gameTime.ElapsedGameTime.TotalSeconds;
 
Zuletzt bearbeitet:
Poste doch noch mal deinen neuen Code hier, vielleicht kann jemand den Fehler finden.

Ob t konstant ist oder nicht ist eigentlich egal, bzw. muss egal sein, denn t wird ja übergeben, damit unabhänging von der Geschwindigkeit des PCs das Spiel ausgeführt werden kann. Also jetzt grad wie in deinem Fall, dass der Ball auf einem Pentium 2, 3, 4 ... PC die gleiche Geschwindigkeit hat. Denn die Methoden Update() bzw. Draw() werden nicht immer in selben Zeitabständen aufgerufen (auch wenn es jetzt bei dir so aussah), sondern sofort nach dem alle Update()-Methoden bzw. Draw()-Methoden durchlaufen sind. (also: alle Update() -> alle Draw() -> alle Update() -> alle Draw()...). Auch kann die Ausführungsgeschwindigkeit von der Anzahl von Prozessen abhängen, die nebenher auf dem PC laufen.
 
Zuletzt bearbeitet:
es ist klar, das alle prozessoren unterscheidlich lange brauchen, um einen befehl zu verarbeiten.
aber t müsste doch trotzdem jede sekunde verändert werden....


hier mal der ganze quellcode, wenn der fehler nicht gefunden wird, werde ich es aufgeben mit visual studio bzw c# noch etwas zu machen...
aber trotzdem danke für eure hilfe, ich hab jetzt dadurch schon einiges gelernt.

PHP:
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using System.Diagnostics;

namespace WindowsGame2
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        Texture2D strand;
        Texture2D ball;
        Vector2 ballposition = Vector2.Zero;
        Vector2 ballgeschw = new Vector2(50.0f, 250.0f);
        float ballbeschl = 20f;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";  
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            strand = Content.Load<Texture2D>("strands");
            ball = Content.Load<Texture2D>("ball");
            spriteBatch = new SpriteBatch(GraphicsDevice);
            // TODO: use this.Content to load your game content here
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();
            
            // TODO: Add your update logic here 
            UpdateSprite(gameTime);
            base.Update(gameTime);
        }
        

        void UpdateSprite(GameTime gameTime)
        {
            
            //Ball bewegen
              
            float t = (float)gameTime.ElapsedGameTime.TotalSeconds;
            Debug.Write(t);
            float ballbeschlhalb = 0.5f * ballbeschl;
            Debug.Write(ballbeschlhalb);
            float ballgeschwmalt = ballgeschw.X * t;
            Debug.Write(ballgeschwmalt);
            float tquadr = t * t;
            Debug.Write(tquadr);
            ballposition.X +=  ballbeschlhalb * tquadr + ballgeschwmalt ;

            

            int MaxX = graphics.GraphicsDevice.Viewport.Width - ball.Width;
            int MinX = 0;
            int MaxY = graphics.GraphicsDevice.Viewport.Height - ball.Height;
            int MinY = 0;

            //Kollisionsabfrage

            if (ballposition.X > MaxX)
            {
                ballgeschw.X *= -1;
                ballposition.X = MaxX;
            }

            else if (ballposition.X < MinX)
            {
                ballgeschw.X *= -1;
                ballposition.X = MinX;
            }

            if (ballposition.Y > MaxY)
            {
                ballgeschw.Y *= -1;
                ballposition.Y = MaxY;
            }

            else if (ballposition.Y < MinY)
            {
                ballgeschw.Y *= -1;
                ballposition.Y = MinY;
            }
        }
        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            graphics.GraphicsDevice.Clear(Color.White);

            // TODO: Add your drawing code here
            spriteBatch.Begin();
            spriteBatch.Draw(strand, Vector2.Zero, Color.White);
            spriteBatch.Draw(ball, ballposition, Color.White);
            spriteBatch.End();
            base.Draw(gameTime);
        }
    }
}
 
Setzt mal das
Code:
            float t = (float)gameTime.ElapsedGameTime.TotalSeconds;
            ballposition.X += 0.5f * ballbeschl * t * t + ballgeschw.X * t;
            ballgeschw.X += ballbeschl * t;
für deine Geschwindigkeitsberechnung ein, also statt:
Code:
            float t = (float)gameTime.ElapsedGameTime.TotalSeconds; 
            Debug.Write(t); 
            float ballbeschlhalb = 0.5f * ballbeschl; 
            Debug.Write(ballbeschlhalb); 
            float ballgeschwmalt = ballgeschw.X * t; 
            Debug.Write(ballgeschwmalt); 
            float tquadr = t * t; 
            Debug.Write(tquadr); 
            ballposition.X +=  ballbeschlhalb * tquadr + ballgeschwmalt ;

Bei mir hat's so einwanfrei funktioniert.

Wiso sollte t jeder Sekunde verändert werden? Wenn das auszuführende immer gleich bleibt wie in deimem Fall muss das nicht unbedingt sein.
 
Zuletzt bearbeitet:
ich habe gemacht, was du gesagt hast.
genau diese 3 zeilen hatte ich ja am anfang auch....

es ist wieder da selbe problem. der ball bewegt sich extrem langsam und mit konstanter geschwindigkeit.
wenn t sich nicht ändert, heißt das die zeit ist konstant (was auf der erde nicht vorkommt) und das würde auch bedeuten, dass die beschleunigung nicht vorhanden ist, da beschleunigung heißt, dass sich die geschwindigkeit mit der zeit ändert.

c# oder visual studio oder XNA bekommts anscheinend nicht gebacken. bzw. ich weiß nicht wie es sonst gehen soll.

ich gebe auf.
 
falsche Einstellung.

wenn du es mit diesem Quellcode versucht funkioniert es auf jedenfall (es ist deiner, ich hab ihn nur an einigen Stellen einwenig (nicht komplett) dem Standard angepasst):
PHP:
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using System.Diagnostics;

namespace WindowsGame2
{
    /// <summary> 
    /// This is the main type for your game 
    /// </summary> 
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        public ContentManager content;
        Texture2D strand;
        Texture2D ball;
        Vector2 ballposition = Vector2.Zero;
        Vector2 ballgeschw = new Vector2(50.0f, 250.0f);
        float ballbeschl = 200f;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            content = new ContentManager(Services);
        }

        /// <summary> 
        /// Allows the game to perform any initialization it needs to before starting to run. 
        /// This is where it can query for any required services and load any non-graphic 
        /// related content.  Calling base.Initialize will enumerate through any components 
        /// and initialize them as well. 
        /// </summary> 
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here 

            base.Initialize();
        }

        /// <summary> 
        /// LoadContent will be called once per game and is the place to load 
        /// all of your content. 
        /// </summary> 
        protected override void LoadGraphicsContent(bool loadAllContent)
        {
            if (loadAllContent)
            {
                // Create a new SpriteBatch, which can be used to draw textures. 
                ball = content.Load<Texture2D>("Content\\ball");
                spriteBatch = new SpriteBatch(graphics.GraphicsDevice);
            }
            // TODO: use this.Content to load your game content here 
        }

        /// <summary> 
        /// UnloadContent will be called once per game and is the place to unload 
        /// all content. 
        /// </summary> 
        protected override void UnloadGraphicsContent(bool unloadAllContent)
        {
            if (unloadAllContent)
            {
                // TODO: Unload any ResourceManagementMode.Automatic content
                //content.Unload();
            }
        }

        /// <summary> 
        /// Allows the game to run logic such as updating the world, 
        /// checking for collisions, gathering input, and playing audio. 
        /// </summary> 
        /// <param name="gameTime">Provides a snapshot of timing values.</param> 
        protected override void Update(GameTime gameTime)
        {

            // Allows the game to exit 
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            // TODO: Add your update logic here  
            UpdateSprite(gameTime);
            base.Update(gameTime);
        }


        void UpdateSprite(GameTime gameTime)
        {

            //Ball bewegen
            float t = (float)gameTime.ElapsedGameTime.TotalSeconds;
            ballposition.X += 0.5f * ballbeschl * t * t + ballgeschw.X * t;
            ballgeschw.X += ballbeschl * t;

            int MaxX = graphics.GraphicsDevice.Viewport.Width - ball.Width;
            int MinX = 0;
            int MaxY = graphics.GraphicsDevice.Viewport.Height - ball.Height;
            int MinY = 0;

            //Kollisionsabfrage 

            if (ballposition.X > MaxX)
            {
                ballgeschw.X *= -1;
                ballposition.X = MaxX;
            }

            else if (ballposition.X < MinX)
            {
                ballgeschw.X *= -1;
                ballposition.X = MinX;
            }

            if (ballposition.Y > MaxY)
            {
                ballgeschw.Y *= -1;
                ballposition.Y = MaxY;
            }

            else if (ballposition.Y < MinY)
            {
                ballgeschw.Y *= -1;
                ballposition.Y = MinY;
            }
        }
        /// <summary> 
        /// This is called when the game should draw itself. 
        /// </summary> 
        /// <param name="gameTime">Provides a snapshot of timing values.</param> 
        protected override void Draw(GameTime gameTime)
        {
            graphics.GraphicsDevice.Clear(Color.White);

            // TODO: Add your drawing code here 
            spriteBatch.Begin();
            spriteBatch.Draw(ball, ballposition, Color.White);
            spriteBatch.End();
            base.Draw(gameTime);
        }
    }
}

Damit muss es tun, bei mir geht's doch auch. Sonst machst du was mutwillig falsch:)

t muss sich nicht ändern um einige Geschwindigkeitsänderung zu erreichen, denn t ist ja lediglich die Zeit vom letzten Update()-Aufruf bis zum jetztigen und du berechnest ja nur, in wie fern sich der Ball innerhalb der letzen Zeitabstandes beschleunigt hat und addierst diese zu eigentlichen Geschwindigkeit hinzu (->beschleunigt). Was du anscheinend von t erwartest, ist, dass es von dem Start des Spiels bis zum jetztigen Update()-Aufruf die Zeit angibt. Das musst dann erst selber in einer Variable mitspeichern, denn, wie gesagt, gibt "gameTime.ElapsedGameTime.TotalSeconds;" die Zeit von letzten Update bis zum Jetzigen in Sekunden an.
 
Zuletzt bearbeitet:
tausend Dank.

Mit deinem Quellcode klappts.

Ich werde nun auf die Fehlersuche gehen, vielleicht verstehe ich es ja und habe noch Spaß mit XNA und c#.
 

Neue Beiträge

Zurück