Add gravity & collision detection to any instance easilly!

Learn to use DS Game Maker by Example!

Add gravity & collision detection to any instance easilly!

Postby YoshiInAVoid » September 5th, 2011, 4:59 pm

I was bored today so I started work on The gravity template.

All of the previous gravity examples had loads of lines of code for each object that you wanted to apply gravity to, so if you had more than 10, your game would take ages to compile and save and stuff, so I thought: "Wouldn't it be good if you could add gravity to any instance you wanted with one line in the object?"

And here it is!
GravityTemplate.dsgm
(7.09 KiB) Downloaded 266 times

(This also includes the code writen below and a sample background... Everything you need to compile it into the nds at the bottom of this post.)

I put all of the complex stuff in scripts, so the only code I had to use in the object was:

Code: Select all
Run Script,Fall;[Me],this


And it adds gravity and collision detection to your object without cluttering up the whole object code.

Then I added this for jumping:

Code: Select all
If,Pad.Newpress.A && Touching_Floor([Me]),this
Start Block,,this
Run Script,Set_Velocity;[Me]<com> -1700,this
End Block,,this


And this for moving left and right:

Code: Select all
Set Variable,[X];[X] + ((Pad.Held.Right && [X] < 224) - (Pad.Held.Left && [X] > 0)),this


And then put a background on the same screen as the object.

Which is all you need if you have the gravity template in your project to build something like this! :P
GravityTemplate.nds
(335.06 KiB) Downloaded 109 times


The collision detection isn't the best, but I'll update it so it looks better later. Until then though, what do you think?
YoshiInAVoid
 
Posts: 1960
Joined: December 30th, 2010, 11:53 am
Location: England

Re: Add gravity & collision detection to any instance easill

Postby Bobbyloujo » September 5th, 2011, 10:30 pm

This will only work if the sprite is 32x32 pixels. You should use [Width] and [Height] to make it work no matter the sprite size.
Seriously never posting again, lawlz.
jk
Bobbyloujo
 
Posts: 1295
Joined: December 30th, 2010, 8:40 pm
Location: Uh-merica

Re: Add gravity & collision detection to any instance easill

Postby Sonic The Hedgehog » September 6th, 2011, 12:56 am

Bobbyloujo wrote:This will only work if the sprite is 32x32 pixels. You should use [Width] and [Height] to make it work no matter the sprite size.


Yeh that would be cool

but I think you should add double jumping :grin:

and it would be cool if you could add that you can jump through the white lighn :grin: :grin:
"He collected the money he got on the battlefeild then he built a ark and got every kind of animal male and female, and then he beat the c*** out of every single one of them"

I've been playing way too much Garrys Mod
User avatar
Sonic The Hedgehog
 
Posts: 824
Joined: June 22nd, 2011, 12:26 pm
Location: My happy place

Re: Add gravity & collision detection to any instance easill

Postby Sonic The Hedgehog » September 8th, 2011, 6:58 pm

So no update :?: 
"He collected the money he got on the battlefeild then he built a ark and got every kind of animal male and female, and then he beat the c*** out of every single one of them"

I've been playing way too much Garrys Mod
User avatar
Sonic The Hedgehog
 
Posts: 824
Joined: June 22nd, 2011, 12:26 pm
Location: My happy place

Re: Add gravity & collision detection to any instance easill

Postby YoshiInAVoid » September 8th, 2011, 8:01 pm

Nope. It was ment as a small example of what scripts can achive.

You can add in your own collision detection code ensted of my crappy one I wrote in a few minutes.
YoshiInAVoid
 
Posts: 1960
Joined: December 30th, 2010, 11:53 am
Location: England

Re: Add gravity & collision detection to any instance easill

Postby bart2465 » May 20th, 2012, 9:08 am

can somebody fix it for 16x16 sprites?

that would be very nice
Bgames
special games

see Evilbirds ds (angry birds) and Gravityduck ds (gravity duck)
bart2465
 
Posts: 14
Joined: May 5th, 2012, 4:51 pm
Location: the netherlands

Re: Add gravity & collision detection to any instance easill

Postby YoshiInAVoid » June 2nd, 2012, 11:31 am

Rewrite the collision detection code to have 16 rather than 32. I would also just recommend you to to rewrite the whole thing since it's very old and glitchy.
YoshiInAVoid
 
Posts: 1960
Joined: December 30th, 2010, 11:53 am
Location: England


Return to Tutorials & Examples

Who is online

Users browsing this forum: Google [Bot] and 1 guest