Author Topic: Useful Miscellaneous Code Snippets  (Read 61825 times)

Drake

  • *
Useful Miscellaneous Code Snippets
« on: February 22, 2010, 06:38:54 PM »
We need a thread like this.

  • This is not for posting patterns that look neat or do something cool, it's to post simple solutions that make everyday coding easier for the average person.
  • If any code you feel like posting is not in a function, please turn it into one for simple use.
  • If any code you see can be made simpler or more efficient, please speak up.
  • You may explain any parameters taken if needed.

Format:
Code: [Select]
Name
[i]Description of function[/i]
-Parameter1: Description of parameter
-Parameter2: Description of parameter
[code]code goes here[/code ] (remove space)



ToFormatString
Returns a number as a string without decimals and added commas for thousand, million marks etc.
Code: [Select]
function ToFormatString(n){
let dummystring = ToString(n);
let dummystring2 = "";
loop(7){
dummystring = erase(dummystring,(length(dummystring)-1));
}
ascent(i in 0..length(dummystring)){
if((length(dummystring)-i)%3==0 && i!=0){
dummystring2 = dummystring2 ~ ",";
}
if(i==0){
dummystring2 = ToString(dummystring[0]);
}else{
dummystring2 = dummystring2 ~ ToString(dummystring[i]);
}
}
return dummystring2;
}

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

Iryan

  • Ph?nglui mglw?nafh
  • Cat R?lyeh wgah?nagl fhtagn.
Re: Useful Miscellaneous Code Snippets
« Reply #1 on: February 22, 2010, 07:08:51 PM »
Great idea for a thread and a very useful piece of code.

*thumbsup*


So, I think I'll make an addition, a piece of code that I created on request for someone in the Q&A thread. Someone else provided another solution to the problem (actually I think it was you  :V), but this one works too, so anyways:

GetPointToLine(ox, oy, p1x, p1y, p2x, p2y);

This function returns the (minimum) distance between a specific point and a specific line. Note that it will return either a positive or a negative value, depending on which side of the line your point is located. I left it this way since this can be useful for several things, and if you want the absolute value you can simply use (| |) brackets, anyways.

Parameters:

ox, oy: Coordinates of the seperate point.
p1x, p1y: Coordinates of one point that lies on the line.
p2x, p2y: Coordinates of another point that lies on the line.

Interchanging the p1 and p2 coordinates switches the output from positive to negative (and vice versa).

Code: [Select]
function GetPointToLine(ox, oy, p1x, p1y, p2x, p2y){

let anorm=atan2(p1y-p2y, p1x-p2x)-90;
let n1=cos(anorm);
let n2=sin(anorm);

let dist= ( (ox-p1x)*cos(anorm)+(oy-p1y)*sin(anorm) )/(( (n1)^2 + (n2)^2 )^0.5) ;

return dist;
}


On popular demand, here a function that requires the function above to function:


GetPointInRectangle(ox, oy, pcx, pcy, recl, recw, align);

This function checks wether or not a given point lies within a given rectangle. Returns true if it is and false if it isn't.

Parameters:

ox, oy: Coordinates of the seperate point.
pcx, pcy: Coordinates of the center of the rectangle.
recl, recw: Length and width of the rectangle.
align: the alignment of the rectangle, i.e. the angle the longer side points at.

Code: [Select]
function GetPointInRectangle(ox, oy, pcx, pcy, recl, recw, align){

let dist1;
let dist2;
let isit=false;

dist1=(| GetPointToLine(ox, oy, pcx, pcy, pcx+cos(align), pcy+sin(align)) |);
dist2=(| GetPointToLine(ox, oy, pcx, pcy, pcx+cos(align+90), pcy+sin(align+90)) |);

if( (dist1<0.5*recw)&&(dist2<0.5*recl) ){ isit=true; }

return(isit);
}
« Last Edit: June 16, 2010, 12:51:50 PM by Iryan »
Old Danmakufu stuff can be found here!

"As the size of an explosion increases, the numbers of social situations it is incapable of solving approaches zero."

Re: Useful Miscellaneous Code Snippets
« Reply #2 on: February 23, 2010, 01:12:11 AM »
wait
Stalls for n number of frames
Only usable in tasks. Make sure to have "yield;" somewhere in your @MainLoop.
Code: [Select]
function wait(n){loop(n){yield;}}
:smug:
« Last Edit: February 25, 2010, 03:58:38 PM by Suikama »

AweStriker Nova

  • Star Sign "Thunder Constellation"
Re: Useful Miscellaneous Code Snippets
« Reply #3 on: February 23, 2010, 02:07:27 AM »
GetDistance(x1,y1,x2,y2)
Code: [Select]
function GetDistance(x1,y1,x2,y2){
return(((x2-x1)^2+(y2-y1)^2)^(1/2))
}
gets the distance between point (x1,y1) and point (x2,y2).

Blargel

  • RAWR!
  • I'M AN ANGRY LOLI!
Re: Useful Miscellaneous Code Snippets
« Reply #4 on: February 23, 2010, 07:04:35 AM »
A few angle related functions that might be helpful.

NormalizeAngle(angle)

Converts the passed in angle into an angle between 0 and 360, not including 360.
Examples: NormalizeAngle(540) = 180, NormalizeAngle(-270) = 90, NormalizeAngle(360) = 0.

Code: [Select]
function NormalizeAngle(angle){
  angle %= 360;
  if(angle<0){ angle += 360; }
  return angle;
}



IsSameAngle(angle1, angle2)

Checks if angle1 is the same angle as angle2. Returns true if they are, false if they are not.
Makes use of the NormalizeAngle function above.
Examples: IsSameAngle(0, 360) = true, IsSameAngle(-90, 270) = true, IsSameAngle(-30, 30) = false.

Code: [Select]
function IsSameAngle(angle1, angle2){
  angle1 = NormalizeAngle(angle1);
  angle2 = NormalizeAngle(angle2);
  return angle1==angle2;
}



AngularDistance(angle1, angle2)

Gets the smallest angle between angle1 and angle2. Will always return a number between -180 and 180, not including -180. A positive number means the shortest way from angle1 to angle2 goes clockwise, a negative number means the shortest way goes counterclockwise. Also uses the NormalizeAngle function from above.
Examples: AngularDistance(90, 180) = 90, AngularDistance(180, 90) = -90, AngularDistance(1, 359) = -2.

Code: [Select]
function AngularDistance(angle1, angle2){
  let distance = NormalizeAngle(angle2 - angle1);
  if(distance>180){ distance-=360; }
  return distance;
}



That last one may seem weird to you, but it's pretty handy for homing bullets that need to decide if they should turn left or right.
<WorkingKeine> when i get home i just go to the ps3 and beat people up in blazblue with a loli
<Azure> Keine: Danmakufu helper by day, violent loli by night.

Azure Lazuline

  • Looooove!!
  • PM me for free huggles and love!
    • Entanma Project - indie game development
Re: Useful Miscellaneous Code Snippets
« Reply #5 on: February 23, 2010, 07:42:29 AM »
ObjEffect_SetAlpha(obj,vertices,alpha)

Since Obj_SetAlpha doesn't work with effect objects, you need to loop through each vertex with ObjEffect_SetVertexColor instead. That's boring, so I made a function for it. obj is the object to apply it to, vertices is the number of vertices of the object, and alpha is the alpha value to set it to. You can also easily modify this to allow color setting in addition to alpha.

Code: [Select]
function ObjEffect_SetAlpha(obj,vertices,alpha){
let k=0;
loop(vertices){
ObjEffect_SetVertexColor(obj,k,alpha,255,255,255);
k++;
}
}

Blargel

  • RAWR!
  • I'M AN ANGRY LOLI!
Re: Useful Miscellaneous Code Snippets
« Reply #6 on: February 23, 2010, 09:16:17 AM »
Oh right object related stuff. I made a few creation functions that automatically set all the initial behaviors because typing out those Obj_SetPosition, Obj_SetSpeed, Obj_SetAngle, and all that gets annoying when you do it a lot.

ObjShot_Create(x, y, speed, angle, graphic, delay)

Creates a shot object while setting up all the initial values. It then returns the object ID so you can do manipulations with it later.
Code: [Select]
function ObjShot_Create(x, y, speed, angle, graphic, delay){
  let obj = Obj_Create(OBJ_SHOT);
  Obj_SetPosition(obj, x, y);
  Obj_SetSpeed(obj, speed);
  Obj_SetAngle(obj, angle);
  ObjShot_SetGraphic(obj, graphic);
  ObjShot_SetDelay(obj, delay);
  return obj;
}



ObjLaser_Create(x, y, angle, length, width, graphic, delay)

Creates a laser object while setting up all the initial values. It then returns the object ID so you can do manipulations with it later.
Code: [Select]
function ObjLaser_Create(x, y, angle, length, width, graphic, delay){
  let obj = Obj_Create(OBJ_LASER);
  Obj_SetPosition(obj, x, y);
  //Obj_SetSpeed is skipped because it is ignored by laser objects.
  Obj_SetAngle(obj, angle);
  ObjLaser_SetLength(obj, length);
  ObjLaser_SetWidth(obj, width);
  ObjShot_SetGraphic(obj, graphic);
  ObjShot_SetDelay(obj, delay);
  return obj;
}



ObjSinuateLaser_Create(x, y, speed, angle, length, width, graphic, delay)

Creates a curvy laser object while setting up all the initial values. It then returns the object ID so you can do manipulations with it later.
Code: [Select]
function ObjSinuateLaser_Create(x, y, speed, angle, length, width, graphic, delay){
  let obj = Obj_Create(OBJ_SINUATE_LASER);
  Obj_SetPosition(obj, x, y);
  Obj_SetSpeed(obj, speed);
  Obj_SetAngle(obj, angle);
  ObjSinuateLaser_SetLength(obj, length);
  ObjSinuateLaser_SetWidth(obj, width);
  ObjShot_SetGraphic(obj, graphic);
  ObjShot_SetDelay(obj, delay);
  return obj;
}

EDIT: Fixed a few mistakes I just noticed. Was typing these from memory rather than copy-pasting.
« Last Edit: February 25, 2010, 06:12:33 AM by Blargel »
<WorkingKeine> when i get home i just go to the ps3 and beat people up in blazblue with a loli
<Azure> Keine: Danmakufu helper by day, violent loli by night.

AweStriker Nova

  • Star Sign "Thunder Constellation"
Re: Useful Miscellaneous Code Snippets
« Reply #7 on: February 25, 2010, 04:22:38 AM »
Object Sinuates have their own SetLength and SetWidth functions, though. They work fine:

Code: [Select]
ObjSinuateLaser_SetWidth(obj,width);
ObjSinuateLaser_SetLength(obj,length);

Blargel

  • RAWR!
  • I'M AN ANGRY LOLI!
Re: Useful Miscellaneous Code Snippets
« Reply #8 on: February 25, 2010, 06:12:01 AM »
Yeah I noticed that, but didn't want to edit it again to annoy people to death with NEW markers next to the thread when there really isn't anything new. Editing it now, lol.
<WorkingKeine> when i get home i just go to the ps3 and beat people up in blazblue with a loli
<Azure> Keine: Danmakufu helper by day, violent loli by night.

Helepolis

  • Charisma!
  • *
  • O-ojousama!?
Re: Useful Miscellaneous Code Snippets
« Reply #9 on: February 25, 2010, 10:07:20 AM »
Suikama =.= I send you a pm but either you missed it or ignored it :V

' Wait '  is an official function in Dnh that halts the dialogue for x-amount of frames. Either type lower capital w or change the name. FAST BEFORE I GUGNIR YOU.

Re: Useful Miscellaneous Code Snippets
« Reply #10 on: February 25, 2010, 03:58:56 PM »
Just don't use it during dialogues then :V

But fine >_>

Re: Useful Miscellaneous Code Snippets
« Reply #11 on: February 25, 2010, 08:55:50 PM »
GetPointToLine(ox, oy, p1x, p1y, p2x, p2y);

hngh



Green area indicates affected region. Obviously the upper diagram is the intended result, however your function will yield the lower diagram. Any way to adjust your code so that I can just get the first result, Iryan?

also lol I mispelled the function





Iryan

  • Ph?nglui mglw?nafh
  • Cat R?lyeh wgah?nagl fhtagn.
Re: Useful Miscellaneous Code Snippets
« Reply #12 on: February 25, 2010, 09:03:41 PM »
This function returns the (minimum) distance between a specific point and a specific line.

p1x, p1y: Coordinates of one point that lies on the line.
p2x, p2y: Coordinates of another point that lies on the line.
http://en.wikipedia.org/wiki/Line_%28geometry%29

A line stretches into infinity. Obviously the lower diagram was the intended result. What you are looking for is the distance to a line segment.

I could try to get the math of that, too, but that won't be easy, and over here I have to go to sleep in a bout an hour, so my concentration might not be the best... and when I'm scripting while tired, I do stupid stuff like confusing x and y axis with one another. This has been established by experiment. :V

Bah, I'm going to try it anyways.

Also, what your text implied you want to create is not what is shown in your diagram. The green area would look more like this:

  _______
(________)

Ie., a rectangle with half a circle attached on each end.
« Last Edit: February 25, 2010, 09:10:14 PM by Iryan »
Old Danmakufu stuff can be found here!

"As the size of an explosion increases, the numbers of social situations it is incapable of solving approaches zero."

Re: Useful Miscellaneous Code Snippets
« Reply #13 on: February 25, 2010, 09:43:14 PM »
A line stretches into infinity. Obviously the lower diagram was the intended result. What you are looking for is the distance to a line segment.

I meant my intended result.

Also, what your text implied you want to create is not what is shown in your diagram. The green area would look more like this:

  _______
(________)

Ie., a rectangle with half a circle attached on each end.

I know, I'm lazy. I found a workaround anyway, so I'm still abusing your code.
« Last Edit: February 25, 2010, 09:45:33 PM by X-Naut »

Iryan

  • Ph?nglui mglw?nafh
  • Cat R?lyeh wgah?nagl fhtagn.
Re: Useful Miscellaneous Code Snippets
« Reply #14 on: February 25, 2010, 10:04:03 PM »
Reformated the post.

GetPointToLineSegment(ox, oy, p1x, p1y, p2x, p2y);

Returns the distance between a given point and a given line segment. For reference, look at the picture Naut posted.

Parameters:

ox, oy: Coordinates of the seperate point.
p1x, p1y: Coordinates of one point that lies on the line.
p2x, p2y: Coordinates of another point that lies on the line.


Code: [Select]
function GetPointToLineSegment(ox, oy, p1x, p1y, p2x, p2y){

let distance;

let va=atan2(p1y-p2y, p1x-p2x);
let vx=cos(va);
let vy=sin(va);

let anorm=va-90;
let n1=cos(anorm);
let n2=sin(anorm);

// ->n = [cos(anorm), sin(anorm)] = [n1, n2]

// g:  [ox-p1x, oy-p1y]*[n1, n2]=0;

// a*->v + b*->n = o->p1/2

let b= ( (oy-p1y-ox*vy/vx+p1x*vy/vx)/(n2-n1*vy/vx) );

let a= ( (ox-p1x-n1*b)/vx  );

if( a<0){
let b= ( (oy-p2y-ox*vy/vx+p2x*vy/vx)/(n2-n1*vy/vx) );
let a= ( (ox-p2x-n1*b)/vx  );

if(a>0){ distance=( (ox-p1x)*cos(anorm)+(oy-p1y)*sin(anorm) )/(( (n1)^2 + (n2)^2 )^0.5);
} else{
distance=( (ox-p2x)^2 + (oy-p2y)^2 )^0.5;
}
} else{
distance=( (ox-p1x)^2 + (oy-p1y)^2 )^0.5;

}
distance=(|distance|);

return distance;
}
« Last Edit: February 26, 2010, 01:53:19 PM by Iryan »
Old Danmakufu stuff can be found here!

"As the size of an explosion increases, the numbers of social situations it is incapable of solving approaches zero."

Re: Useful Miscellaneous Code Snippets
« Reply #15 on: February 25, 2010, 10:14:49 PM »
ty ilu

Blargel

  • RAWR!
  • I'M AN ANGRY LOLI!
Re: Useful Miscellaneous Code Snippets
« Reply #16 on: February 26, 2010, 11:55:08 AM »
GetPlayerMoveAngle

Returns the angle the player is moving at. Based off of button inputs so if the player is at the edge of the screen, it may or may not suit your purposes. Good for player scripts with shots that need to be directable based off of which way the player is moving. Returns NULL if the player is not moving.
Code: [Select]
function GetPlayerMoveAngle {
  let angle = NULL;
  if(GetKeyState(VK_DOWN)==KEY_HOLD){
    if(GetKeyState(VK_RIGHT)==KEY_HOLD){
      angle = 45;
    }
    else if(GetKeyState(VK_LEFT)==KEY_HOLD){
      angle = 135;
    }
    else {
      angle = 90;
    }
  }
  else if(GetKeyState(VK_UP)==KEY_HOLD){
    if(GetKeyState(VK_RIGHT)==KEY_HOLD){
      angle = 315;
    }
    else if(GetKeyState(VK_LEFT)==KEY_HOLD){
      angle = 225;
    }
    else {
      angle = 270;
    }
  }
  else if(GetKeyState(VK_RIGHT)==KEY_HOLD){
    angle = 0;
  }
  else if(GetKeyState(VK_LEFT)==KEY_HOLD){
    angle = 180;
  }
  return angle;
}
« Last Edit: June 17, 2010, 11:43:23 AM by Blargel »
<WorkingKeine> when i get home i just go to the ps3 and beat people up in blazblue with a loli
<Azure> Keine: Danmakufu helper by day, violent loli by night.

Re: Useful Miscellaneous Code Snippets
« Reply #17 on: February 27, 2010, 08:33:27 AM »
GetPlayerMoveAngle

Does Danmakufu support joystick/analogue control? Because that code of yours sure doesn't.  :V
The SoEW patch has had its second release, come and get it!

Blargel

  • RAWR!
  • I'M AN ANGRY LOLI!
Re: Useful Miscellaneous Code Snippets
« Reply #18 on: February 27, 2010, 08:44:52 AM »
Does Danmakufu support joystick/analogue control? Because that code of yours sure doesn't.  :V

Danmakufu only detects 8 directions so... I'm pretty sure this will work fine.
<WorkingKeine> when i get home i just go to the ps3 and beat people up in blazblue with a loli
<Azure> Keine: Danmakufu helper by day, violent loli by night.

Re: Useful Miscellaneous Code Snippets
« Reply #19 on: February 27, 2010, 06:32:40 PM »
Danmakufu only detects 8 directions so... I'm pretty sure this will work fine.
Does Touhou only support 8 directions as well?

Azure Lazuline

  • Looooove!!
  • PM me for free huggles and love!
    • Entanma Project - indie game development
Re: Useful Miscellaneous Code Snippets
« Reply #20 on: February 27, 2010, 07:23:14 PM »
Yes, I know that all official games (and Danmakufu) only support 8 directions.

Re: Useful Miscellaneous Code Snippets
« Reply #21 on: February 27, 2010, 08:38:48 PM »
Yes, I know that all official games (and Danmakufu) only support 8 directions.

Really? I thought PCB and on had omnidirectional support when using a joystick.
The SoEW patch has had its second release, come and get it!

Blargel

  • RAWR!
  • I'M AN ANGRY LOLI!
Re: Useful Miscellaneous Code Snippets
« Reply #22 on: February 28, 2010, 04:03:17 PM »
Hmmmm, I have reason to believe that the GetPointToLineSegment code isn't working correctly. I dunno how to test it, but it wasn't working right when I used it. I have some code that I Googled out and converted to Danmakufu code and it seems to work better:

Code: [Select]
function GetPointToLineSegment(cx, cy, ax, ay, bx, by){

  let distanceLine;
  let distanceSegment;

  let r_numerator = (cx-ax)*(bx-ax) + (cy-ay)*(by-ay);
  let r_denomenator = (bx-ax)*(bx-ax) + (by-ay)*(by-ay);
  let r = r_numerator / r_denomenator;
 
  let px = ax + r*(bx-ax);
  let py = ay + r*(by-ay);
 
  let s =  ((ay-cy)*(bx-ax)-(ax-cx)*(by-ay) ) / r_denomenator;
 
  let distanceLine = absolute(s)*(r_denomenator^0.5);
 
  let xx = px;
  let yy = py;
  if (r>=0 && r<=1){
    distanceSegment = distanceLine;
  }
  else {
    let dist1 = (cx-ax)*(cx-ax) + (cy-ay)*(cy-ay);
    let dist2 = (cx-bx)*(cx-bx) + (cy-by)*(cy-by);
    if(dist1 < dist2){
      xx = ax;
      yy = ay;
      distanceSegment = dist1^0.5;
    }
    else {
      xx = bx;
      yy = by;
      distanceSegment = dist2^0.5;
    }
  }
  return distanceSegment;

}
<WorkingKeine> when i get home i just go to the ps3 and beat people up in blazblue with a loli
<Azure> Keine: Danmakufu helper by day, violent loli by night.

Iryan

  • Ph?nglui mglw?nafh
  • Cat R?lyeh wgah?nagl fhtagn.
Re: Useful Miscellaneous Code Snippets
« Reply #23 on: February 28, 2010, 04:24:29 PM »
Hmmmm, I have reason to believe that the GetPointToLineSegment code isn't working correctly. I dunno how to test it, but it wasn't working right when I used it. I have some code that I Googled out and converted to Danmakufu code and it seems to work better:
  :o

Could you please show me the (relevant part of the) script where the error occured?

Through testing around, I figured out that, probably due to dividing by approximately zero, the script ceases to work for the case p1x=p2x . Was that the case with your script, maybe?


Edit: Fixed version of my code is here.
The error I noticed won't turn up anymore. Though I have to say that the values will have inaccuracies of less than 0.01 pixels if the angle of the line lies between 89.999 and 90.001 (or 269.999 and 270.001).

Code: [Select]
function GetPointToLineSegment(ox, oy, p1x, p1y, p2x, p2y){

let distance;

let va=atan2(p1y-p2y, p1x-p2x);
let vx=cos(va);
let vy=sin(va);

let anorm=va-90;
let n1=cos(anorm);
let n2=sin(anorm);

    if(floor(10000*(|vx|))!=0 ){

let b= ( (oy-p1y-ox*vy/vx+p1x*vy/vx)/(n2-n1*vy/vx) );

let a= ( (ox-p1x-n1*b)/vx  );

if( a<0){
let b= ( (oy-p2y-ox*vy/vx+p2x*vy/vx)/(n2-n1*vy/vx) );
let a= ( (ox-p2x-n1*b)/vx  );

if(a>0){ distance=( (ox-p1x)*cos(anorm)+(oy-p1y)*sin(anorm) )/(( (n1)^2 + (n2)^2 )^0.5);
} else{
distance=( (ox-p2x)^2 + (oy-p2y)^2 )^0.5;
}
} else{
distance=( (ox-p1x)^2 + (oy-p1y)^2 )^0.5;

}
distance=(|distance|);

    } else{

let uy=(p1y+p2y-(|p1y-p2y|))/2;
let ly=(p1y+p2y+(|p1y-p2y|))/2;

if(oy<=uy){
distance=( (p1x-ox)^2 + (uy-oy)^2 )^0.5;
}
if(oy>=ly){
distance=( (p1x-ox)^2 + (ly-oy)^2 )^0.5;
}
if(oy>uy&&oy<ly){
distance=(| p1x-ox |);
}

    }

    return distance;
}
« Last Edit: February 28, 2010, 04:56:21 PM by Iryan »
Old Danmakufu stuff can be found here!

"As the size of an explosion increases, the numbers of social situations it is incapable of solving approaches zero."

Blargel

  • RAWR!
  • I'M AN ANGRY LOLI!
Re: Useful Miscellaneous Code Snippets
« Reply #24 on: February 28, 2010, 06:17:17 PM »
I'm not entirely sure what the problem was. I was trying to find the distance from the enemy to a line drawn from the player to a point 80 pixels away from the player. I noticed being strange while the line was drawn at a 270 degree angle when I did if(GetPointToLineSegment(enemyx, enemyy, GetPlayerX, GetPlayerX, endx, endy) < 16). That condition never became true for some odd reason, even if I purposely made the line intersect the enemy. If I increased 16 to 32, it would become true sometimes, but noticeably smaller than it should be.
<WorkingKeine> when i get home i just go to the ps3 and beat people up in blazblue with a loli
<Azure> Keine: Danmakufu helper by day, violent loli by night.

Iryan

  • Ph?nglui mglw?nafh
  • Cat R?lyeh wgah?nagl fhtagn.
Re: Useful Miscellaneous Code Snippets
« Reply #25 on: February 28, 2010, 06:26:15 PM »
Ah, so it was this peculiar problem. But now that I have fixed it, my code takes up more space then yours. Daaaymn.

Though I'd really like to understand the math behind your code. Either it uses some formulas and theorems which I am not aware of, or it only uses the respectie equations it in a solved form in which I can not perceive them anymore...
Old Danmakufu stuff can be found here!

"As the size of an explosion increases, the numbers of social situations it is incapable of solving approaches zero."

Blargel

  • RAWR!
  • I'M AN ANGRY LOLI!
Re: Useful Miscellaneous Code Snippets
« Reply #26 on: March 01, 2010, 06:42:32 AM »
There was a giant explanation that I skipped through while converting it to Danmakufu code. It wasn't hard to convert anyway. :V

You can read it here.

I just read a little and I think it uses linear algebra lololol.
<WorkingKeine> when i get home i just go to the ps3 and beat people up in blazblue with a loli
<Azure> Keine: Danmakufu helper by day, violent loli by night.

Iryan

  • Ph?nglui mglw?nafh
  • Cat R?lyeh wgah?nagl fhtagn.
Re: Useful Miscellaneous Code Snippets
« Reply #27 on: March 01, 2010, 03:09:29 PM »
There was a giant explanation that I skipped through while converting it to Danmakufu code. It wasn't hard to convert anyway. :V

You can read it here.

I just read a little and I think it uses linear algebra lololol.
Yay! Thanks for the-

Waitaminute... I just realized I don't really understand english math vocabulary.

Blah, thanks anyways.  :V
Old Danmakufu stuff can be found here!

"As the size of an explosion increases, the numbers of social situations it is incapable of solving approaches zero."

AweStriker Nova

  • Star Sign "Thunder Constellation"
Re: Useful Miscellaneous Code Snippets
« Reply #28 on: March 05, 2010, 03:31:01 AM »
Somehow this topic ended up on the second page.

Since programmers can be lazy (and we are), here's the standard code to bounce an object bullet:
Code: [Select]
if(Obj_GetX(obj)<=GetClipMinX||Obj_GetX(obj)>=GetClipMaxX){
Obj_SetAngle(obj,180-Obj_GetAngle(obj));
}
if(Obj_GetY(obj)<=GetClipMinY||Obj_GetY(obj)>=GetClipMaxY){
Obj_SetAngle(obj,360-Obj_GetAngle(obj));
}
« Last Edit: March 05, 2010, 03:33:06 AM by AweStriker Nova »

Blargel

  • RAWR!
  • I'M AN ANGRY LOLI!
Re: Useful Miscellaneous Code Snippets
« Reply #29 on: March 05, 2010, 05:39:05 PM »
Dunno if this belongs here or not but...

ShootShape

Shoots a geometric shape from the specified point. (x, y) is the point to spawn the shape, speed is the speed the corners move at, angle is the angle one of the corners will shoot at, graphic and delay are pretty self explanatory, corners is the number of corners, and bullets is the number of bullets per side.

As an example with the corners and bullets parameters, if you put 4 for corners and 10 for bullets, you will get a square that has 10 bullets per side.

Code: [Select]
task ShootShape(x, y, speed, angle, graphic, delay, corners, bullets){
  let Corners = [];
  let SideBullets = [];
  ascent(i in 0..corners){
    let obj = Obj_Create(OBJ_SHOT);
    Corners = Corners ~ [obj];
   
    Obj_SetPosition(obj, x, y);
    Obj_SetSpeed(obj, speed);
    Obj_SetAngle(obj, angle+i*(360/corners));
    ObjShot_SetGraphic(obj, graphic);
    ObjShot_SetDelay(obj, delay);
   
    Obj_SetAlpha(obj, 0);
    Obj_SetCollisionToPlayer(obj, false);
    Obj_SetAutoDelete(obj, false);
  }
 
  ascent(i in 0..corners){
    if(i==corners-1){
      let Side = BulletLine(Corners[i], Corners[0]);
      SideBullets = SideBullets ~ [Side];
    }
    else {
      let Side = BulletLine(Corners[i], Corners[i+1]);
      SideBullets = SideBullets ~ [Side];
    }
  }
 
  while(length(SideBullets) > 0){
    descent(i in 0..length(SideBullets)){
      if(SideBullets[i] == [0]){
        SideBullets = erase(SideBullets, i);
      }
      else {
        let tempArray = SideBullets[i];
        descent(j in 1..length(tempArray)){
          if(Obj_BeDeleted(tempArray[j])){
            tempArray = erase(tempArray, j);
          }
        }
        SideBullets[i] = tempArray;
      }
    }
    yield;
  }
 
  ascent(i in 0..corners){
    Obj_Delete(Corners[i]);
  }
 
  function BulletLine(obj1, obj2){
    let SideBullets = [0];
    let DummyArray = [0];
    ascent(i in 0..bullets){
      let obj = Obj_Create(OBJ_SHOT);
      SideBullets = SideBullets ~ [obj];
      DummyArray = DummyArray ~ [0];
     
      ObjShot_SetGraphic(obj, graphic);
      ObjShot_SetDelay(obj, delay);
    }
    RealTask;
    return SideBullets;
   
    task RealTask {
      while(SideBullets != DummyArray){
        let x1 = Obj_GetX(obj1);
        let y1 = Obj_GetY(obj1);
        let x2 = Obj_GetX(obj2);
        let y2 = Obj_GetY(obj2);
        let distance = ((x1-x2)^2 + (y1-y2)^2)^0.5;
        let interval = distance/bullets;
        let angle = atan2(y2-y1, x2-x1);
        descent(i in 1..length(SideBullets)){
          if(Obj_BeDeleted(SideBullets[i])){
            SideBullets[i] = 0;
          }
          else {
            let xo = Obj_GetX(SideBullets[i]);
            let yo = Obj_GetY(SideBullets[i]);
           
            Obj_SetPosition(SideBullets[i], x1+cos(angle)*interval*i, y1+sin(angle)*interval*i);
           
            let xn = Obj_GetX(SideBullets[i]);
            let yn = Obj_GetY(SideBullets[i]);
            let newangle = atan2(yn-yo, xn-xo);
           
            Obj_SetAngle(SideBullets[i], newangle);
          }
        }
        yield;
      }
    }
  }
}
<WorkingKeine> when i get home i just go to the ps3 and beat people up in blazblue with a loli
<Azure> Keine: Danmakufu helper by day, violent loli by night.