Archive: major code help plz


3rd June 2006 17:47 UTC

major code help plz
okay. so i have this preset that i'm making. an extension of wireframe which is essentially a texer that moves around the window but only along one axis (x, -x, y, -y) at a time.
one night i decided to add a little mouse input, that when your mouse is over the avs screen the texer will follow your mouse around, again only moving along one axis at a time. sounded easy to me at the time, but it's been almost a week now and i'm starting to loose it. i almsot have it but this one little bit is stopping me. i'll paste what i have here and see if anyone can help me out.

init

dc=0; n=1; xs=0.03; ys=0.04; xl=0.9; yl=0.9; x1=-0.03; y1=0; x2=0; y2=0

frame
(original movement code)
x1=x1+if(equal(dc,0),xs,if(equal(dc,1),-xs,x1a));
x1a=if(above(x1,xl),assign(dc,1),if(below(x1,-xl),assign(dc,0),0));
y1=y1+if(equal(dc,2),ys,if(equal(dc,3),-ys,0));
y1a=if(above(y1,yl),assign(dc,3),if(below(y1,-yl),assign(dc,2),0));
nb=if(below(getspec(0,0,0.5),0.1),assign(dc,4),0);

(resets x1 and y1 if the particle escapes the screen - bug fix)]
xr=if(above(abs(x1),1),assign(x1,0),0);
yr=if(above(abs(x1),1),assign(y1,0),0);

(checks if mouse is on screen)
mx=getkbmouse(1); my=getkbmouse(2);
osx=if(below(abs(mx),1),1,0);
osy=if(below(abs(my),1),1,0);
os=band(osx,osy);

(finds the distance between the mouse and the particle and rounds to a whole number)
difx=(mx-x2)*10|1;
dify=(my-y2)*10|1;

(pretty much speaks for itself)
xay=if(above(difx,dify),1,0);
yax=if(above(dify,difx),1,0);

(new movement code in place of x1/y1)
x2=x2+if(___,if(above(difx,0),xs,if(below(difx,0),if(difx,-xs,0),0)),0);
y2=y2+if(___,if(above(dify,0),ys,if(below(dify,0),if(dify,-ys,0),0)),0);



beat


dc=rand(5)-1;

point


x=if(os,x2,x1);
y=if(os,y2,y1);
(switches between x1/y1 and x2/y2 when mouse is on screen)


at the moment this works.. kinda. when the mouse is on screen the particle follows the mouse, but doesn't move along one axis at a time. what i need is something to put in place of the ___ in x2/y2 (xay and yax kinda work). something that determines if difx is bigger or smaller than dify (i think i've got this with xay/yax) and then causes the largest value to enable its corresponding x2/y2 movement. for example if difx was greater than dify, ___ would turn on, and stay on until the x2 movement causes difx to equal 0 (moves the particle along the x-axis until it's in line with the mouse), then switch and turn on the y2 movement and shift the particle along the y axis until dify equaled 0 (the particle aligns itself with the mouse in the y axis and in this case would end up right under the cursor).
but this also needs to work the other way if dify is greater than difx. so really i need 2 things, one for x and one for y, but both with the ability to override the other if it is greater than it, until it reaches 0 and then lets the other take over. if that makes any sense.

any help would be very much appreciated.


3rd June 2006 17:52 UTC

-> and here's the preset if anyone wants to see it kinda working


3rd June 2006 23:49 UTC

Re: major code help plz
Using "|1" to round to a whole number looks really strange to me. Why not use floor(n+.5) instead? In fact, why do you even need to round this to a whole number anyway? I would probably prefer an approach similar to the following:

(finds the ABSOLUTE distance between the mouse and the particle and DOESN'T round to a whole number)
difx=abs(mx-x2);
dify=abs(my-y2);

(make xay true if difx is above dify AND difx is above a certain threshold)
xay=if(above(difx,dify), above(difx,.05) ,0);
yax=if(above(dify,difx), above(dify,.05) ,0);

(new movement code in place of x1/y1)
x2=x2+if(xay,if(above(mx,x2),xs,-xs),0);
y2=y2+if(yax,if(above(my,y2),ys,-ys),0);


Now, this should make x2 and y2 home in on the cursor, although probably not in the manner you want. Once the object is diagonally aligned with the mouse, it'll rapidly alternate between horizontal and vertical movement, and that'll be ugly. It's possible to fix that, but it'll be a much more "involved" modification. I'll need a few minutes to experiment with it.

By the way, where you have:
osx=if(below(abs(mx),1),1,0);
osy=if(below(abs(my),1),1,0);

Surrounding below() with an if(x,1,0) is completely redundant, since the below() function will return a 1 or a 0 in the first place. You'll get the exact same result with this version:
osx=below(abs(mx),1);
osy=below(abs(my),1);


4th June 2006 10:12 UTC

thanks for that last bit.. but difx and dify have to be rounded because they never actually reach 0 (only on very very rare occasions), so x2 and y2 keep switching from positive to negative movement once it gets within 0.03 and 0.04 - the shifting speed assigned in init. rounding makes sure they reach 0 so x2 and y2 work properly.


4th June 2006 17:14 UTC

http://dump.jheriko.kicks-ass.net/nudgey.avs


6th June 2006 08:31 UTC

That's why I added the "above(difx,.05)" and "above(dify,.05)" to the red statements shown above. They account for that.

Not that it matters anyway. I like jheriko's approach to this better.


6th June 2006 10:19 UTC

i got it.. well as close as i can. lemme know what you think (keeping in mind i already know how completely stupid this preset is)


6th June 2006 11:42 UTC

Didn't we fix this together on #avs?

http://dump.jheriko.kicks-ass.net/nudgey_13.avs

What is still wrong with it?