Phun forum repost

About advanced scenes, and the Thyme scripting language used in Algodoo.

Phun forum repost

Postby Kilinich » Sat Nov 07, 2009 3:41 pm

Repost from phun forum to algodoo.
Some of my "knowhow" thyme functions:

1) For dynamic textures (rotation/scaling ):
Code: Select all
//T - original textureMatrix, T(2) and T(5) - horizontal and vertical shift
//a - rotation angle
//s - scale
scene.my.txTransform := (T, a, s) => {[math.cos(a)*s, math.sin(a)*s, T(2), - math.sin(a)*s, math.cos(a)*s, T(5), T(6), T(7), T(8)]};

//Type it in console to make test:
scene.addbox{color := [1,1,1,1]; texture := "algoryx.png"; textureMatrix := {scene.my.txTransform([1,0,1,0,1,1,0,0,1], app.mousepos(0), 2^app.mousepos(1))}}

2) Vectors and dynamic light:
Code: Select all
// Make a vector from two (3-dimensional) points like [x,y,z]
// for example [app.mousepos(0), app.mousepos(1), 0], [0,0,0]
Scene.my.vec := (dot1, dot2)=>{[dot2(0) - dot1(0), dot2(1) - dot1(1), dot2(2) - dot1(2)]};

// Calculates square of vector's lenght (add ^0.5 for length)
// argument is 3-dimensional vector [x,y,z]
Scene.my.vmod2 := (v)=>{v(0) ^ 2 + v(1) ^ 2 + v(2) ^ 2};

// Сross product of two vectors
Scene.my.vmul := (v1, v2)=>{[v1(1) * v2(2) - v1(2) * v2(1), v1(2) * v2(0) - v1(0) * v2(2), v1(0) * v2(1) - v1(1) * v2(0)]};

// Surface normal (length =1) - do a normal vector from two angles of spherical coordinate system
Scene.my.vnorm := (anorm, axy)=>{[math.sin(anorm) * math.cos(axy), math.sin(axy) * math.sin(anorm), math.cos(anorm)]};

// Illuminance of surface, where vn - surface normal, vr - vector from surface to light source, I - intensity of light source
Scene.my.E := (vn, vr, I)=>{I / Scene.my.vmod2(vr) - I * Scene.my.vmod2(Scene.my.vmul(vr, vn)) / Scene.my.vmod2(vr) ^ 2};

3) Round and type conversion from float to integer:
Code: Select all
scene.my.round = (x) => {(math.mod (x,1) < .5) ? (x - math.mod (x,1)) : (x - math.mod (x,1) + 1)}
scene.my.int10 = (x) => {X==0 ? 0 : X==1 ? 1 : X==2 ? 2 : X==3 ? 3 : X==4 ? 4 : X==5 ? 5 : X==6 ? 6 : X==7 ? 7 : X==8 ? 8 : X==9 ? 9 : 9}
Scene.my.float2int := (f)=>{f < 9.5 ? scene.my.int10(scene.my.round(f)) : {scene.my.float2int(f / 10) * 10 + scene.my.int10(math.mod(scene.my.round(f), 10))}}
infix 1 right: #_ => scene.my.float2int;

//now try:

12.444 / 5
// gives 2.4888

#12.444 / 5
// gives 2

4) Simple functions for mouse control
Code: Select all
// Angle between vector (p1, p2) and horizont
scene.my.alpha := (p1, p2) => {((p2(0) - p1(0)) == 0) ? ((p2(1) - p1(1)) > 0 ? math.pi / 2 :  - math.pi / 2) : (math.pi * ((p2(0) - p1(0)) > 0 ? 0 : 1.0) * ((p2(1) - p1(1)) > 0 ? 1.0 :  - 1.0) + math.atan((p2(1) - p1(1)) / (p2(0) - p1(0))))}

// Pointer to mouse, replace x0, y0 with Pointer phunlet coordinates
scene.my.pointer = {scene.my.alpha([x0, y0], app.mousepos)}
Last edited by Kilinich on Sat Nov 07, 2009 11:22 pm, edited 2 times in total.
Dream of Algodoo as game development engine...
User avatar
Kilinich
[Best bug reporter 2010]
 
Posts: 2098
Joined: Mon Aug 31, 2009 8:27 pm
Location: South Russia

Re: Phun forum repost

Postby niffirg1 » Sat Nov 07, 2009 9:49 pm

The Rounding can be very useful and the others are cool thanks kilinich
User avatar
niffirg1
 
Posts: 376
Joined: Mon Aug 31, 2009 10:31 pm
Location: The Great American South!


Return to Thyme scripting

Who is online

Users browsing this forum: No registered users and 7 guests

cron