program HowToChangeTheMassOfSprites; uses SwinGame,sgTypes; procedure Main(); var earth : Sprite; asteroid : Sprite; begin OpenGraphicsWindow('Sprites With Different Mass' ,800 ,600 ); ClearScreen(ColorWhite ); LoadBitmapNamed('earth' ,'earth.png' ); LoadBitmapNamed('asteroid' ,'asteroid.png' ); earth := CreateSprite(BitmapNamed('earth' ) ); SpriteSetX(earth ,700 ); SpriteSetY(earth ,100 ); SpriteSetVelocity(earth ,VectorTo(-0.8 ,0.6 ) ); SpriteSetMass(earth ,10 ); asteroid := CreateSprite(BitmapNamed('asteroid' ) ); SpriteSetX(asteroid ,100 ); SpriteSetY(asteroid ,500 ); SpriteSetVelocity(asteroid ,VectorTo(1 ,-0.6 ) ); SpriteSetMass(asteroid ,1 ); repeat ProcessEvents(); ClearScreen(ColorWhite ); DrawSprite(earth ); UpdateSprite(earth ); DrawSprite(asteroid ); UpdateSprite(asteroid ); if SpriteCollision(earth ,asteroid ) then CollideCircles(asteroid ,earth ); RefreshScreen(); until WindowCloseRequested(); FreeSprite(earth ); FreeSprite(asteroid ); ReleaseAllResources(); end; begin Main(); end.
How To Change The Mass Of Sprites
This article will show you how to give different mass for different sprites and also the change in their behaviour accordingly.
Higher number for mass means the object has higher inertia. If two objects collide with one is higher in mass, the direction and speed for the lower mass object will change significantly where the higher mass would continue its path with less difference.
To set the mass for an object you need to do the following steps:
- Get two images and make them sprites with initial position (Sprite Set X and Sprite Set Y)
- Set a velocity (Sprite Set Velocity) for those sprites
- Set the mass (Sprite Set Mass) for the sprites
- In the game loop collide both sprites with each other to observe the change in their behavior.
The code below shows how the mass will effect on speed and direction of an asteroid if it collides with the earth which is ten times heavier than the asteroid. You can try with the earth being only twice as heavy as the asteroid and see the difference.
In reality if an asteroid hits the earth which is 10 times heavier than that asteroid, there will be explosion and they both will be destroyed (Unless a superhero comes and saves the earth). If you want to make it look real, you can do the animation (read animation).
Suggested projects will appear here.