16

It was a long holiday weekend, so I got the coding bug again and started playing around:

Mario http://gfilter.net/junk/tileengine.jpg

I wrote a basic tile engine, but having never attempted this before, I am really struggling with handling sprite collision detection and implementing realistic physics for gravity.

For any other game hobby writers, can you point me towards some walkthroughs on the best way to approach this?

Update:

I thought I'd share a progress report:

http://www.youtube.com/watch?v=-RKNQ2UiiLY <-- Game in Action

Its still really buggy, but collision detection is mostly working, I've started working on some other features (such as bumping the blocks (notice the bug) and interacting with the enemies).

Mario still walks like he is on the moon, I'm using these constants, any advice for tweaking them for more realism?

    const float AirDrag = 1.00f;
    const float GroundFriction = .97f;
    const float Gravity = 0.8f;
dmckee --- ex-moderator kitten
  • 98,632
  • 24
  • 142
  • 234
FlySwat
  • 172,459
  • 74
  • 246
  • 311

9 Answers9

5

Download the FarseerPhysics engine, have a look at how it works http://www.codeplex.com/FarseerPhysics I think it's the best thing available for XNA/Silverlight!

asleep
  • 4,054
  • 10
  • 34
  • 51
3

Gravity is easy:

const gravity = ... ; // pixels per timestep (eg. video frame) squared
// while in freefall, each timestep:
y_velocity += gravity;
y_pos += y_velocity;

Mind you, most 2d platform games I've played don't have realistic gravity. Just do whatever makes the game fun!

Hugh Allen
  • 6,509
  • 1
  • 34
  • 44
  • 4
    Technically, I think you should update position first (so it uses the velocity of the previous timestep), then update velocity. – gnovice Mar 25 '09 at 17:06
  • @gnovice Even more technically, check out verlet –  Jan 10 '16 at 14:56
2

jnrdev might be of some assistance. It covers tile collision/response and slopes. It's not the best code I have ever seen, but it gets the job done.

Zack The Human
  • 8,373
  • 7
  • 39
  • 60
  • Thanks for that link. I was going to recommend it as well but it has since been lost over the years in my bookmarks :) – grepsedawk Dec 02 '08 at 02:55
2

There are a couple of really useful 2-d platformer tutorials at http://www.metanetsoftware.com/technique/tutorialA.html and http://www.metanetsoftware.com/technique/tutorialB.html. I think they've been referenced by others elsewhere on SO. They cover collision detection and response, raycasting, various optimisation techniques etc. and have a good explanation of the theory behind it all for those (like me) who are less mathematically inclined. It doesn't go as far as stuff like rigid body dynamics, but I don't think you'd need that for the type of game you are writing (though it would of course be cool if you added this sort of stuff...)

bm212
  • 1,429
  • 11
  • 12
  • by the look of it, the tutorials I suggested cover similar stuff to jnrdev mentioned by Zack Mulgrew. I haven't read jnrdev yet (beyond an initial glance) so can't really compare them though. – bm212 May 09 '09 at 23:41
1

That may be a detour, but try the Platformer starter kit from XNA 3.0, that contains stuff like Physics and basic Collision detection. You will need to change stuff to make it work outside of XNA, but it's not rocket science.

XNAGS 3.0 download

Michael Stum
  • 177,530
  • 117
  • 400
  • 535
0

Your bug with the multiple blocks being bumped, you could fix that by only bumping the block that is most aligned with the playersprite, or has the least offset. Be sure not to limit it to just one direction. Blocks can actually be bumped from any direction in Mario. (Above by doing a ground pound in same games, or the drill-spin-thing) (Sides by using a shell)

Sneakyness
  • 5,305
  • 4
  • 33
  • 39
0
    UIGraphicsBeginImageContext(images.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    UIColor *color = [UIColor whiteColor];
    [color setFill];

    CGContextTranslateCTM(context, 1, images.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);
    CGContextSetBlendMode(context, kCGBlendModeDestinationOver);
    CGRect rect = CGRectMake(0.0, 0.0, images.size.width, images.size.height);
    CGContextDrawImage(context, rect, images.CGImage);
    CGContextClipToMask(context, rect, images.CGImage);
    CGContextAddRect(context, rect);
    CGContextDrawPath(context, kCGPathFill);
    images = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
Nikunj Patel
  • 304
  • 2
  • 7
0

I don't know what you're using for a physics model, but physics models that use fluid drag were recently addressed in another SO question. I won't repeat everything that I gave in my answer, I'll just link to it.

To summarize, the OP for the question wanted to accelerate an object from rest to a maximum velocity. I went through a few derivations for modeling velocity as a function of time for two different types of drag. Your situation may be slightly different, so the integrals used may have different forms or need to be solved with different initial conditions, but hopefully my answer will point you in some informative directions.

Community
  • 1
  • 1
gnovice
  • 125,304
  • 15
  • 256
  • 359
-6

Ever heard of GameMaker?

GameFreak
  • 2,881
  • 7
  • 34
  • 38