ppIsaac Tutorial 03 - Texture and Materials

Tutorial 03 - Texture and Material

This tutorial shows how to load a texture on a geometry and how to use materials.

SuperStrict

Import pyroplay.ppisaaclite

Graphics 800,600

ppIsaac.Start()
ppIsaac.gravity.y = 50000


' create objects

Local ground:ppBox = ppBox.Create(300,400,600,50,0)
ground.SetAngle(10)

Local box01:ppBox = ppBox.Create(350,100,96,96,150)
box01.LoadTexture("media/box_texture.png")
box01.SetAngle(15)

Local box02:ppBox = ppBox.Create(400,0,96,96,150)
box02.LoadTexture("media/box_texture.png")
box02.SetAngle(15)


' create materials

Local matWood:ppMaterial = ppMaterial.Create()
Local matFlubber:ppMaterial = ppMaterial.Create()

ppMaterial.SetElasticity(defaultMaterial, matFlubber, 0.85)
ppMaterial.SetFriction(defaultMaterial, matFlubber, 0.2, 0.1)
ppMaterial.SetSoftness(defaultMaterial, matFlubber, 0.01)

ppMaterial.SetElasticity(defaultMaterial, matWood, 0.2)
ppMaterial.SetFriction(defaultMaterial, matWood, 0.8, 0.7)
ppMaterial.SetSoftness(defaultMaterial, matWood, 0.5)


' set material to objects

box01.SetMaterial(matWood)
box02.SetMaterial(matFlubber)


While Not KeyHit(KEY_ESCAPE) And Not AppTerminate()
Cls
ppIsaac.Update()
ppIsaac.Draw()

Flip
Wend

ppIsaac.Stop()
End

Listing 3.1 
 

Every geometry has got a LoadTexture function to load an image to the geometry. The parameters are the same as BlitzMax' LoadImage function:

box02.LoadTexture("media/box_texture.png")

Per default the texture is scaled to the size of the geometry. However, you can automatically resize the geometry to fit the textures dimension. If you want to do so call the following command after loading a texture:

box02.FitGeometryToTexture()


Now lets examine the material part. To create materials you will have to use the ppMaterial type. Just call a ppMaterial.Create() to create a new material.

When two objects collide, we call the two materials of the objects a material pair regardless whether the two materials are the same or not. Each material pair does have it's own settings for friction, elasticity and softness. Which means when i.e. matWood collides with defaultMaterial the friction value can be different from a collision between matWood and matFlubber. When you create a material, the values for the three material atributes does have a default value.

In Listing 3.1 we can see how to change these three atributes for the two material pairs matWood vs. defaultMaterial and matFlubber vs. defaultMaterial:

ppMaterial.SetElasticity(defaultMaterial, matFlubber, 0.85)
ppMaterial.SetFriction(defaultMaterial, matFlubber, 0.2, 0.1)
ppMaterial.SetSoftness(defaultMaterial, matFlubber, 0.01)

ppMaterial.SetElasticity(defaultMaterial, matWood, 0.2)
ppMaterial.SetFriction(defaultMaterial, matWood, 0.8, 0.7)
ppMaterial.SetSoftness(defaultMaterial, matWood, 0.5)

Every object in ppIsaac uses the defaultMaterial unless you set a different material to the object. The following line of code sets the materials to our two boxes:

box01.SetMaterial(matWood)
box02.SetMaterial(matFlubber)

The ground object uses the defaultMaterial. Run the example and see how different  the two boxes response to the impact with the ground object.

Links:
ppIsaac Product Page
Tutorial 01 - Hello Isaac!
Tutorial 02 - Static and Dynamic Objects
Tutorial 04 - Joints
Tutorial 05 - Collision Detection and Material Collision