skynes

I understand it'd be pretty difficult to help me with a problem with me just describing it, without seeing code so...

http://skynes.tripod.com/TheWizard.zip

There's a link to a zip file containing my XNA project and all images.

The Project so far is a mix of a couple of tutorials I found online (2d wizard and a tile engine) along with sprites I found online. I haven't made any of my own sprites yet cause I don't see the point until I have everything working, but I will be using all my own original sprites when it works.

Anyway.

So far it's a wizard who can jump and shoot fireballs. An enemy (movement currently commented out) walks across the screen, firballs dont do anything yet, but touching the enemy costs you a life.

My problem is with the platforms. I can jump onto the platforms ok, but if you jump while on the platforms you end up hanging in mid-air. I've tried incorporating a gravity effect, so when you move off the platforms you fall, but I haven't had any luck with it. In all honesty I really don't have a clue how to make gravity function using what I've got.



Re: XNA Game Studio Express 2d Platforms

George Clingerman

The download link wasn't working for me. Can't wait to how far you've built off my original code! I love looking at how far people can take the initial concept and go with it in their own directions.

[Edit: Link works if I right click and Save As in Firefox. If I click it, it doesn't work so all is well]

[Edit: Nix that. Link still doesn't work. File downloaded has no data. So the link still doesn't work for me.]





Re: XNA Game Studio Express 2d Platforms

Jim Perry

Link is working for me now. Haven't had a chance to look at it yet though.

edit: looks like there's a problem with jumping. Once I get on the platform I can't get down.  I also managed to jump and stay near the top of the window.

George - I emailed the zip to you.






Re: XNA Game Studio Express 2d Platforms

skynes

Durrrr... me skynes... me do daft fings... durrrr...

Tripod doesnt allow remote access... I shoulda remembered that.

try http://skynes.tripod.com on its own. Link should be there and since it's from inside the site, it should allow downloads also.





Re: XNA Game Studio Express 2d Platforms

GroZZleR

In order to simulate gravity, you need to more accurately represent motion. You can do this with 2 Vector2Ds: Position and Velocity. It may sound complex, but it's really quite simple. Velocity represents speed in a direction and Position is the objects current position.

Whenever you need to move the object, give it a velocity in the direction you want to move in. For example:
if(IsKeyDown(Keys.Right)
Wizard.Velocity.X = 5;
if(IsKeyDown(Keys.Left)
Wizard.Velocity.X = -5;
// And the same for Up and Down using Y instead of X.

When it comes time to update the position of the Wizard, just do:
Wizard.Position += Wizard.Velocity;

That'll move the Wizards position by it's velocity. So if you have -5 X and +3 Y, he'll move 5 to the left and 3 up.

Now if you want to add gravity, just add to his Y velocity every frame. This will constantly give him movement downwards.

Wizard.Velocity.Y += 2;
if(Wizard.Velocity.Y > 20)
Wizard.Velocity.Y = 20; // Don't want him rocketing at the speed of light if he's been falling for a long time.

That should help you get him off of the platform and it'll more accurately represent jumps (jump up fast, then slow down a bit, then begin to fall).

Hope this helps!




Re: XNA Game Studio Express 2d Platforms

Rafael Mores

I'm on a Plataform project as well, and I've decided to add some simple elements of physics to the game, being the goal to actually write a small physics engine because I have had trouble trying to use FarSeer's Physics Engine and another one I found out there. I still have A LOT of work to do, but I'll give you a grasp of the concepts of which might be interesting to you:

Force = mass * acceleration

mass is a scalar value, every sprite should have one.

both force and acceleration are vectors, so that is easy to implement.

Your player may have 80kg and gravity may be 9.8m/s^2 so each call to update in your game you get to calculate gravity for him.

As far as movement, you can add to his final position a velocity vector like Grozzler put it.

player input + gravity = player position

then comes the floor problem, since floors have to stop you from going down, they need to be pushing you up in the same ammount gravity pulls you down, so for that you will need normals. but I guess if you dont want to mess around there, and have specific Y values for your plataforms, you can just do

if( player.position.Y == value ) {stop gravity}

Anyway, Id recommend checking codeplex for some XNA components, if you know how to use them into your game. Good Luck!





Re: XNA Game Studio Express 2d Platforms

Zedox

Hey look at these two links. I posted some of my code for my game.
1: http://xna.multigan.com/pastebin/ page=view&id=1172197702
2: http://xna.multigan.com/pastebin/ page=view&id=1172197909

The first link is a class for a fighter (in your case a wizard). In your constructor, you want a Position, Velocity, Mass, and Gravity. This is basic physics. You have two methods really. You have your move method which updates the position:
Final Position = Initial Position + (Velocity*Change in Time)

Update the velocity, which in this case, you only want to worry about gravity and not the horizontal velocity:
Velocity = Velocity - (Mass*Gravity)
This is not the way you would learn it in physics class...kinda. It is really Distance/Time.

Then you have the Jump method. You want to send an "impulse" into your velocity to "spring" you upwards.
Velocity = -450 (I just chose 450 as an example...)
The reason why I have a negative number is because the way XNA has things oriented and *** or something like that.

In the other class, which is basically your game class (in my case, it is the fighting screen) you have a method which I called checkBorders(). When you are on the ground you want to have 0 Velocity (so you don't move) and you don't want to go through the ground or the roof, so that's why I put those there.

If there are any questions just ask in this thread, I'll check it over the next couple of days. After that, eh...





Re: XNA Game Studio Express 2d Platforms

skynes

Everything so far has been a great help and given me a good idea of what to do.

I'm going to go and re-write how movement works to use velocity and gravity rather than what it was.

I'll get back to you on what works and what doesn't.

thanks.

 

[Edit]

As odd as it is with playing with physics. Adding gravity, which forced me to change how all the movement worked actually made it all simpler and easier for me to understand!

I have the next version as www.skynes.tripod.com

my newest problem is that I won't jump anymore. My movement left and right is fine, works as intended. My jump looks like hes jumping, as in, the sprite changes through the jump pattern. But his feet never leave the ground.

I set the jump speed to -40, but it never jumps.





Re: XNA Game Studio Express 2d Platforms

Zedox

Show me your code. What is the mass of the character...The Force (which you apply to the Vertical Velocity) must be greater than the Mass * Gravity in order for the character to move. Just show the code.




Re: XNA Game Studio Express 2d Platforms

skynes

 

Mass is 80.

Gravity is 9.

I divide the total by 120 to get a more reasonable value. So in this case my gravity is 6. I will be playing with my values in the future, so divide by 120 is only to get a smaller number, otherwise I'm forced to have a jump value of a couple of thousand.

The jump force is -40

private void ApplyGravity()

{

mWizard.Velocity = new Vector2(mWizard.Velocity.X, mWizard.Velocity.Y + grav.GetVelocity(mWizard));

 

mWizard.Position = new Vector2(mWizard.Position.X + mWizard.Velocity.X, mWizard.Position.Y + mWizard.Velocity.Y);

}

//Within jump code

mWizard.Velocity = new Vector2(0, -40);

 Sorry, I don't know what this forums procedure on posting code extracts is.

 





Re: XNA Game Studio Express 2d Platforms

Rafael Mores

I'd try debbuging the values right when you press the jump button, my guess is that you may have your jump applied one time but at each update call the gravity gets called in and the result is a devastating push down which you dont actually see. But then again, it may be something else. nothing wrong with that code, but if you can post how you apply values to your velocity vector and how you update those in the update method would be helful



Re: XNA Game Studio Express 2d Platforms

skynes

The program spans multiple methods, I have the full thing at www.skynes.tripod.com Second on the list, the Wizard but with gravity.

I actually don't think it's the gravity method. I've commented out the application of gravity and he still didn't jump.

Moving left and right works, so in the Keyboard.right/left bit I tried adjusting the Y velocity by -40, with no gravity enforced. Technically that should send him flying to the top of the screen, but it didn't. He still didn't move.

This is what is odd. Moving left and right works grand using mwizard.velocity but when I try to make an up or down motion, it does nothing.





Re: XNA Game Studio Express 2d Platforms

waheyluggage

Hi
 
I couldn't grab the code to take a good look but I think it might be worthwhile to not keep creating new Vector2's so I'd probably do something like...
 
(this is rough code, not sure if it will work and you'll have to fill in bits yourself).  You will want to display your position and velocity on screen so you can see the numbers change.
 
(that's annoying, in my edit view the code and text font sizes are the other way arround)
 
* in the wizard class definition have...
 
     Vector2 velocity = Vector2.Zero();
     Vector2 position = WhereIWantToStartPosition;
     BOOL canJump = FALSE;
 
* Have methods in the wizard's class like...
 
    void UpdateWizard()
    {
          UpdateKeys();
          UpdatePosition();
          UpdateCollision();
          UpdateVelocity();
    }
 
    void UpdateKeys()
    {
         if (pressedLeft)    velocity.X = velocity.X - 40;
         if (pressedRight)  velocity.X = velocity.X + 40;
         if ((pressedJump) && (canJump == TRUE))
         {
               velocity.Y = 220;
               canJump = FALSE;
         }
    }
 
    void UpdatePosition()
    {
        position = position + velocity;    // move the player by his velociy
    }
 
    void UpdateVelocity()
    {
           // make our Y velocity adjust for gravity
          velocity.Y = velocity.Y - 40.0f;    
 
          // make sure we don't fall too fast
          if (velocity.Y <  -200.0f)   velocity.Y = -200.0f;         
 
          // allow for a little bit of inertia in the X,
          // 0.0f = no inertia, 1.0f = you never stop sliding, tweak this to get a nice
          // feel for if your character goes on ice make it a different one.
          velocity.X = velocity.X * 0.95f;    
    }
 
    void UpdateCollision()
    {
           // need to put your own stuff here.
 
           // banged head
           if (blockedUp) velocity.Y = 0.0f;      
 
           // run into wall
           if (blockedLeftOrRight) velocity.X = 0.0f;      
 
           // landed on something
           if (blockedDown)              
           {
                  velocity.Y = 0.0f;  
 
                  // we're on some floor so we can jump   
                  canJump = TRUE;   
           }
     }
 




Re: XNA Game Studio Express 2d Platforms

Rafael Mores

I couldnt download your sourcecode, I might take a look at it if you can provide a reliable link. :)

how do you make him jump

is the gravity taking over the jump impulse

are you constantly adding up vectors for gravity or jump at each update (giving acceleration - change in velocity over time)





Re: XNA Game Studio Express 2d Platforms

skynes

Got a suggestion for where I can put the project

I make him jump by adding a negative value to his Y velocity. Then doing wizard.position += wizard.velocity

I have commented out the gravity, so the gravity will not affect his jump, yet his jump still doesn't jump.