2d Platformer physics [closed]
Asked Answered
A

9

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;
Argument answered 1/12, 2008 at 23:22 Comment(1)
The video is off!Vole
R
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!

Revelation answered 1/12, 2008 at 23:55 Comment(0)
V
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!

Variable answered 2/12, 2008 at 1:40 Comment(2)
Technically, I think you should update position first (so it uses the velocity of the previous timestep), then update velocity.Hudak
@Hudak Even more technically, check out verletBoeke
S
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.

Sustain answered 2/12, 2008 at 2:30 Comment(1)
Thanks for that link. I was going to recommend it as well but it has since been lost over the years in my bookmarks :)Crow
M
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...)

Manslayer answered 9/5, 2009 at 21:26 Comment(1)
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.Manslayer
E
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

Enjoyment answered 1/12, 2008 at 23:43 Comment(0)
H
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.

Hudak answered 25/3, 2009 at 17:24 Comment(0)
I
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)

Invite answered 26/7, 2009 at 14:39 Comment(0)
E
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();
Ectoblast answered 9/8, 2012 at 5:10 Comment(0)
H
-6

Ever heard of GameMaker?

Hawse answered 2/12, 2008 at 2:25 Comment(2)
That takes all the fun out of it.Argument
you want to spend your life doing things the hard way, be my guestKamilahkamillah

© 2022 - 2024 — McMap. All rights reserved.