Topic: Useful Miscellaneous Code Snippets  (Read 34951 times)

0 Members and 1 Guest are viewing this topic.

Chronojet ⚙ Dragon

  • The Oddity
  • 今コソ輝ケ、我ガ未来、ソノ可能性!!
  • LOOK AT ME
  • Nickname: Mewkyuu
  • Gender: Indeterminate
Re: Useful Miscellaneous Code Snippets
« Reply #90 on: December 07, 2011, 01:54:18 am »
CreateLaser01Copy

Just use like CreateLaser01. Modify as needed.

Code: [Select]
task CreateLaser01Copy(lX, lY, lSpeed, lAngle, lLength, lWidth, lGraphic, lDelay) {
let objT = Obj_Create(OBJ_SHOT);
Obj_SetPosition(objT, lX, lY);
ObjShot_SetGraphic(objT, lGraphic);
ObjShot_SetDelay(objT, lDelay);
Obj_SetAlpha(objT, 0);
loop(lDelay) { yield; }
let doCreate = Obj_BeDeleted(objT); Obj_Delete(objT); if(!doCreate) {
let obj = Obj_Create(OBJ_LASER);
Obj_SetPosition(obj, lX, lY);
Obj_SetAngle(obj, lAngle);
ObjShot_SetGraphic(obj, lGraphic);
ObjLaser_SetWidth(obj, lWidth);
ObjLaser_SetSource(obj, false);
ObjShot_SetBombResist(obj, false);
ObjLaser_SetLength(obj, 0);
while(!Obj_BeDeleted(obj)) {
if(ObjLaser_GetLength(obj)<lLength){
ObjLaser_SetLength(obj, ObjLaser_GetLength(obj)+lSpeed);
} else {
Obj_SetPosition(obj, Obj_GetX(obj)+lSpeed*cos(lAngle), Obj_GetY(obj)+lSpeed*sin(lAngle));
}
if(ObjLaser_GetLength(obj)>lLength){ ObjLaser_SetLength(obj, lLength); }
yield;
}
}
}

MMX

  • In Soviet Gensokyo...
  • ...bullets dodge you.
Re: Useful Miscellaneous Code Snippets
« Reply #91 on: December 07, 2011, 02:39:31 pm »
CreateSprite

Very usefull function that creates simple square sprite with given size and texture. It returns an effect object id wich may be processed to change it's properties as usual.

Code: [Select]
function CreateSprite(texture, width, height){
let obj=Obj_Create(OBJ_EFFECT);

ObjEffect_SetTexture(obj,texture);
ObjEffect_SetRenderState(obj,ALPHA); // by default. May be changed later

ObjEffect_SetPrimitiveType(obj,PRIMITIVE_TRIANGLESTRIP);
ObjEffect_CreateVertex(obj, 4);

ObjEffect_SetVertexXY(obj,0,-width/2,-height/2);
ObjEffect_SetVertexXY(obj,1,width/2,-height/2);
ObjEffect_SetVertexXY(obj,2,-width/2,height/2);
ObjEffect_SetVertexXY(obj,3,width/2,height/2);

ObjEffect_SetVertexUV(obj,0,0,0);
ObjEffect_SetVertexUV(obj,1,width,0);
ObjEffect_SetVertexUV(obj,2,0,height);
ObjEffect_SetVertexUV(obj,3,width,height);

return obj;
}
« Last Edit: February 18, 2012, 02:06:49 pm by MMX »
My danmakufu thread Most recent - "Kappa Mechanics" (Nitori fight)   My youtube channel Latest update - EoSD extra no bombs clear

  • LiveJournal blog

MMX

  • In Soviet Gensokyo...
  • ...bullets dodge you.
Re: Useful Miscellaneous Code Snippets
« Reply #92 on: December 08, 2011, 02:59:09 am »
Oops. I've read the topic's rules so this post doesnt fit here (also doublepost). Moderator, please delete this.
« Last Edit: December 08, 2011, 03:09:28 am by MMX »
My danmakufu thread Most recent - "Kappa Mechanics" (Nitori fight)   My youtube channel Latest update - EoSD extra no bombs clear

  • LiveJournal blog

ShedPH93

  • Neophyte
  • Nickname: SH93
Re: Useful Miscellaneous Code Snippets
« Reply #93 on: February 10, 2012, 04:35:47 pm »
ReflectionAngle
Returns the angle of reflection, given the angle of incidence and the angle of the "mirror".
Code: [Select]
function ReflectionAngle (angle, mirror)  {
   return 2*mirror-angle; }

MMX

  • In Soviet Gensokyo...
  • ...bullets dodge you.
Re: Useful Miscellaneous Code Snippets
« Reply #94 on: February 18, 2012, 02:03:27 pm »
LinesIntersect
Code: [Select]
function LinesIntersect(x1,y1,x2,y2,x3,y3,x4,y4){
let intpoint=[0,0];
let ua=((x4-x3)*(y1-y3)-(y4-y3)*(x1-x3))/((y4-y3)*(x2-x1)-(x4-x3)*(y2-y1));
let ub=((x2-x1)*(y1-y3)-(y2-y1)*(x1-x3))/((y4-y3)*(x2-x1)-(x4-x3)*(y2-y1));
if(ua>0 && ua<1 && ub>0 && ub<1){
intpoint[0]=x1+ua*(x2-x1);
intpoint[1]=y1+ua*(y2-y1);
}else{
intpoint[0]=9999;
intpoint[1]=9999;
}
return intpoint;
}

Checks if two line segments defined by their endpoints intersect, and if they do returns coordinates of intersection point. First i made it to return false when there's no intersection, but returning the same array with some nonsense values is easier to process later.



Algorithm taken from this site http://paulbourke.net/geometry/lineline2d/
My danmakufu thread Most recent - "Kappa Mechanics" (Nitori fight)   My youtube channel Latest update - EoSD extra no bombs clear

  • LiveJournal blog

fondue

  • excuse me
  • LOOK AT ME
  • Nickname: mrfondue
  • Gender: Male
Re: Useful Miscellaneous Code Snippets
« Reply #95 on: October 28, 2012, 12:26:35 pm »
uh-oh necrobump

waitUntil
Code: [Select]
function waitUntil(condition){ while(!condition) { yield; } }
Halts the current task/function/sub until the requirements (k) are met.haven't tested it myself though yet orz

e: Thanks to kyuu for making it shorter~
« Last Edit: October 28, 2012, 04:48:24 pm by fondoomahsterr »

CK Crash

  • boozer
  • Nickname: Lucas
Re: Useful Miscellaneous Code Snippets
« Reply #96 on: October 30, 2012, 08:39:19 am »
uh-oh necrobump

waitUntil
Code: [Select]
function waitUntil(condition){ while(!condition) { yield; } }
Halts the current task/function/sub until the requirements (k) are met.haven't tested it myself though yet orz

e: Thanks to kyuu for making it shorter~
Pretty sure this won't work. The expression will be evaluated immediately as true or false, which will make the function yield infinitely or not at all. Condition is its own variable, it just starts with a different value depending on what arguments you give the function.
Code: [Select]
function Add(var2) {var2 += 4;}

let var1 = 4;
Add(var1);
//var1 still equals 4 after running the function, var2 copies the value of var1 without changing it

Not that you can't use a function to conditionally wait:
Code: [Select]
let waitSwitch = false;
function WaitForSwitch(){
     while(!waitSwitch){yield;}
}

@MainLoop{
     if(condition){waitSwitch = true;}
     else{waitSwitch = false;}
     yield;
}
« Last Edit: November 01, 2012, 05:43:04 am by Lucas »

Sparen

  • Danmakufu Artist
  • Git ready, git set, PUUSH!
  • Gender: Male
Re: Useful Miscellaneous Code Snippets
« Reply #97 on: April 15, 2013, 11:40:17 pm »
Simple revision for less typing and naming standards and cleanliness etc.

Code: [Select]
function Obj_GetAngleToPlayer(obj){
    return Obj_GetAngleToPoint(obj,GetPlayerX,GetPlayerY)
}

function Obj_GetAngleToPoint(obj,tarX,tarY){
    if (Obj_GetX(obj) > tarX){
        return 180 - atan2((|Obj_GetY(obj)-tarY|),(| Obj_GetX(obj)-tarX |));
    }
    return atan2((|Obj_GetY(obj)-tarY|),(| Obj_GetX(obj)-tarX |));
}

Code: [Select]
function Obj_GetAngleToPoint(obj,tarX,tarY){
  if(Obj_GetX(obj) > tarX && Obj_GetY(obj) < tarY){
    return 180 - atan2((|Obj_GetY(obj)-tarY|),(| Obj_GetX(obj)-tarX |));
  }
  if(Obj_GetX(obj) > tarX && Obj_GetY(obj) >= tarY){
    return 180 - atan2((tarY-Obj_GetY(obj)),(| Obj_GetX(obj)-tarX |));
  }
  if(Obj_GetX(obj) < tarX && Obj_GetY(obj) >= tarY){
    return atan2((tarY-Obj_GetY(obj)),(| Obj_GetX(obj)-tarX |));
  }
  return atan2((|Obj_GetY(obj)-tarY|),(| Obj_GetX(obj)-tarX |));
}

I felt the need to supply this version because the version that Drake posted does not work if you are located above the bullet (as far as my scripts went).

@Mods: I know this thread is dead. But I don't want someone else to wonder why their code doesn't work. Sorry if it's not supposed to be here.
@Drake: If my code's wrong, then PM me or something and I'll fix it. I've tested it though (in all four quadrants), so it should work fine.

Drake

  • *
  • LOOK AT ME
Re: Useful Miscellaneous Code Snippets
« Reply #98 on: April 16, 2013, 03:26:01 am »
I'm actually not even sure why I bothered OKing that function, never mind trying to clean it up.
Obj_GetAngleToPoint() and Obj_GetAngleToPlayer()
Code: [Select]
function Obj_GetAngleToPlayer(obj){
return Obj_GetAngleToPoint(obj, GetPlayerX, GetPlayerY);
}

function Obj_GetAngleToPoint(obj, tX, tY){
return (atan2(tY - Obj_GetY(obj), tX - Obj_GetX(obj)) + 360) % 360;
}
is enough.



Also, if anyone finds the debug window awkward to use:
DebugText() and SetDebugText()
Display arbitrary values at the bottom of the screen.
The line numbers are essentially self-sorting and it doesn't display empty lines so writing on lines like 4, 0, 1, 80 is fine even though you should never actually do that.
Only limit to the number of lines is the maximum array size, but you'll hit the top of the screen before that, obviously.

Run DebugText() in the @DrawLoop and call SetDebugText() to write some value on some line.
Code: [Select]
function DebugText(){
SetFontColor(255, 255, 255, 255, 255, 255);
let temp = GetCommonDataDefault("DEBUG_TEXT", []);
let j = 0;
descent(i in 0..length(temp)){
if(temp[i] != ""){
DrawText(temp[i], 36, 450 - j*14, 12, 255);
j++;
}
}
}

function SetDebugText(line, string){
let temp = GetCommonDataDefault("DEBUG_TEXT", []);
if(line >= length(temp)){
while(line > length(temp)){
temp = temp ~ [""];
}
SetCommonData("DEBUG_TEXT", temp ~ [ToString(string)]);
}else{
temp[line] = ToString(string);
SetCommonData("DEBUG_TEXT", temp);
}
}

For example,
SetDebugText(0, "Angle:" ~ ToString(Obj_GetAngleToPlayer(obj)));
SetDebugText(1, [GetPlayerX, GetPlayerY]);
SetDebugText(2, [Obj_GetX(obj), Obj_GetY(obj)]);

outputs:
« Last Edit: April 16, 2013, 03:28:22 am by Drake »

A Colorful Calculating Creative and Cuddly Crafty Callipygous Clever Commander
- original art by Aiけん | ウサホリ -

Fujiwara no Mokou

  • Hourai Incarnate
  • Oh, so this trial of guts is for ME?
Re: Useful Miscellaneous Code Snippets
« Reply #99 on: October 08, 2017, 08:27:39 am »
Just throwing this out here:
There's a lot of SetAngleX/SetAngleY/SetAngleZ functions in ph3, but that's a bit.... restrictive.
How about the ability to rotate about any arbitrary axis instead?

Unfortunately, no such function exists natively, but luckily we can convert arbitrary axis-angle rotation to Euler angles, then feed that output to the SetAngle functions to achieve the same desired effect.
Here's the math for it (note that <x,y,z> is the axis vector with <0,0,0> as the source):
Code: [Select]
function toEuler(let x, let y, let z, let angle)
{
let s=sin(angle);
let c=cos(angle);
let t=1-c;
        let yaw;
        let pitch;
        let roll;

//  normalize the axis
let magnitude = power(x*x + y*y + z*z, 0.5);
if (magnitude==0)
        {
            // error
            return 0;
        }
x /= magnitude;
y /= magnitude;
z /= magnitude;

if ((x*y*t + z*s) > 0.998)
        {
                // north pole singularity detected
yaw = 2*atan2(x*sin(angle/2), cos(angle/2));
roll = 90;
pitch = 0;
}
else if ((x*y*t + z*s) < -0.998)
        {
                // south pole singularity detected
yaw = -2*atan2(x*sin(angle/2), cos(angle/2));
roll = -90;
pitch = 0;
}
        else
        {
    yaw = atan2(y * s- x * z * t , 1 - (y*y+ z*z ) * t);
    roll = asin(x * y * t + z * s) ;
    pitch = atan2(x * s - y * z * t , 1 - (x*x + z*z) * t);
        }
        return [ yaw, pitch, roll ];
}
« Last Edit: October 08, 2017, 08:31:20 am by Fujiwara no Mokou »
  • Profile
 

SMF 2.0.15 | SMF © 2017, Simple Machines
Theme based on ModernDark64 design by BlocWeb
Page created in 0.052 seconds with 40 queries.