javascript

String to integer using javascript

Swiftlikeninja's picture

Okay I ran into an interesting scenario trying to sort through an xml feed. The Structure I have has a number of completes field and Im trying to get a leaderboard set up with the top completes displayed but the feed has the completes as a string and as such the structure sort sorts them alphabetically. Meaning that 10 would actually come before 2 which can cause some major issues in sorting. I found that if I use a number splitter off the complete index and rebuild the structure w/ the kineme structure maker then sorting works but the structure Im using can range to over 400 members so manually doing this is out of the question and i don't think iterator is the answer. Can anyone assist me in a javascript answer on how to change a substructure in an entire structure list from a string to an integer?

Or even simpler, tell me if Im missing something in the structure sort?

Snow.qtz Javascript Fix and Particle Count Mod

gtoledo3's picture

When looking at a recent post by yanomano about array size, I realized that the Snow.qtz example from the Apple Developer Examples "OpenCL" download suffered from a very similar code setup to set the particle positions.

I have changed that so that when the particle count is reduced, that the structure reduces correctly.

Also, I have noted that when the qtz is initialized, that it runs a bit "oddly" until particle count is reset. This amounts to the distribution of particles not being as even looking, and performance being a bit more uneven. I'm not sure if this has always been the case with this composition, but I've added a small portion to the setup to create a pulse at startup that quickly adds and subtracts a particle on startup, and any time that particle count is changed thereafter. This smooths performance on my system.

Mouse Interaction 'The Hard Way'

toneburst's picture

Little demo of mouse-interaction without using the new QC4 Interaction patch. I need a few extra options the Interaction patch didn't allow, so ended-up attempting to do the whole thing in JS, with a simple Iterator to render the billboards.

Here's the JS code, if anyone's interested:

/*
   toneburst 2010
*/
 
///////////////
// Functions //
///////////////
 
// Init points array
// In: number of points, scale on x, scale on y, offset x, offset y
// Out: array of point objects
function initPoints(numpnts, scalex, scaley, offsetx, offsety) {
   // Init vars
   pnts = new Array(numpnts);
   var spacingx = scalex / (numpnts - 1);
   var spacingy = scaley / (numpnts - 1);
   // Loop through creating points objects and adding to array
   for(i = 0; i < numpnts; i++) {
      var p = new Object();
      p.x = spacingx * i + offsetx;
      p.y = spacingy * i + offsety;
      p.focus = false;
      pnts[i] = p;
   }
   return pnts;
}
 
// Distance between two 2D points
// In: points objects in form p.x, p.y
// Out: number
// For mouse-click hit-testing
function distance(p0, p1) {
   xd = p1.x - p0.x; // x-distance
   yd = p1.y - p0.y; // y-distance
   return Math.sqrt(xd*xd + yd*yd);
}
 
// Function to clamp input value to cmin > cmax range
function clamp(val, cmin, cmax) {
   return Math.max(cmin, Math.min(cmax, val));
}
 
// Function to handle dragging of points, with constraints
// In: points array (point x y z focus), mouse-position, point index, clamp-limit
// Out: point object
function dragPoint(pnts, mspos, pindex, clmplimit, dminx, dmaxx, dminy, dmaxy) {
   // Get working point
   p = pnts[pindex];
   // Get previous point x-position
   if(pindex == 0) {
   // If first point
      prevx = dminx - clmplimit;
   } else {
      prevx = pnts[pindex - 1].x;
   }
   // Get next point x-value
   if (pindex == pnts.length - 1) {
   // If last point
      nxtx = dmaxx + clmplimit;
   } else {
      nxtx = pnts[pindex + 1].x
   }
   // Clamp x with minimum distance
   p.x = clamp(mspos.x, prevx + clmplimit, nxtx - clmplimit);
   // Clamp y
   p.y = clamp(mspos.y, dminy, dmaxy);
   // Return point object
   return p;
}
 
///////////////
// Main Loop //
///////////////
 
// Vars for main program loop
var clickCoord = new Array(0, 0);
var oldMouseButton = false;
var points = new Array();
var dragIndex = -1;
 
// The Loop
function (__structure PointsOut)
         main
      (__number MouseX, __number MouseY, __boolean MouseButton,
       __index NumPoints, __boolean InitPoints, __number InitScaleX, __number InitOffsetX, __number InitScaleY, __number InitOffsetY,
       __number DragMinX, __number DragMaxX, __number DragMinY, __number DragMaxY, __number DragMinDistanceX,
       __boolean PinStart, __boolean PinEnd,
       __structure StartColor, __structure EndColor,
       __boolean LoadPoints, __structure PointsIn)
{
   if(!_testMode) {
 
      mousePos = new Object();
      mousePos.x = MouseX; mousePos.y = MouseY;
 
      // Set initial X and Y positions
      if(!points[0] || InitPoints) {
         points = initPoints(NumPoints, InitScaleX, InitScaleY, InitOffsetX, InitOffsetY);
      }
 
      // Load points
      if(LoadPoints) {
         if(PointsIn[0]) {
            for(h = 0; h < PointsIn.length; h++) {
               p = new Object();
               p.x = PointsIn[h].x; p.y = PointsIn[h].y; p.focus = false;
               points[h] = p;
            }
         }
      }
 
      // Start drag on mousedown
      if(MouseButton > oldMouseButton) {
         clickCoord = Array(MouseX, MouseY);
         for(i = 0; i < points.length; i++) {
            if(distance(mousePos, points[i]) < 0.01) {
               dragIndex = i;
               if(dragIndex == 0 && PinStart) { dragIndex = -1; break; }
               else if(dragIndex == points.length - 1 && PinEnd) { dragIndex = -1; break; }
               else { points[dragIndex].focus = true; break; }
            }
         }
 
      // End drag on mouseup
      } else if(MouseButton < oldMouseButton && dragIndex >= 0) {
         points[dragIndex].focus = false;
         dragIndex = -1;
      }
 
      // Do drag
      if(dragIndex >= 0) {
         // Call function to set point position with constraints
         points[dragIndex] = dragPoint(points, mousePos, dragIndex, DragMinDistanceX, DragMinX, DragMaxX, DragMinY, DragMaxY);
      }
 
      // Update mousebutton state for next frame
      oldMouseButton = MouseButton;
 
      // Init output object
      var result = new Object();
 
      // Set output values
      result.PointsOut = points;
 
      // Return result object
      return result;
   }
}

Dunno if this is going to be of any direct use to anyone.

a|x

Inertia Javascript

dust's picture

does anyone know an easy way to do inertia in javascript ? i know qc has inertia built in which is fine but i'm making an iPad app that is is a remote for qc so the controls need to be the same on both. i'm using inertia in qc. i know the iphone has inertia scrolling so maybe someone knows some stock sdk code for inertia or how to do in javascript in qc.

im going to experiment.

would this work. ?

d = (p2-p1);

s = 8;

inertia = ((inertia += d) / s));

s being the speed variable. i really want to incorporate friction as well.

any help would be awesome.

i've been poking around paul bourkes site thinking he may have a function.

im going to poke around some BBCode and see if i can find a inertia function.

maybe look for a framework or something ?

it doesn't matter what language. i said java script because i can convert javascript to what ever language i want.

help please. i know someone out there has a function built.

Sorting a structure

Swiftlikeninja's picture

Okay, I have been trying to wrap my head around javascript to no avail so i call upon the assistance of the masses here at kineme. I dont know if just the structure sort patch will do what i need but in essence, i'm building a scoreboard display that will display individuals and team scores. What I would like assistance in is figuring out a way to sort a structure of based upon a substructure. If you look at the attached png as an example, i would like to be able to sort the entire structure by team name preserving the keys and structure layout.

once i figure this out my next task will be separating each team into its own structure.

If anyone could point me in the right direction I would be eternally grateful. I have tried to dig through all the javascript references on the kineme site and cyberos guide and my mind just explodes.

If I could just learn javascript i would be in heaven! :P