Gamestudio Links
Zorro Links
Newest Posts
Zorro 2.70
by jcl. 09/29/25 09:24
optimize global parameters SOLVED
by dBc. 09/27/25 17:07
ZorroGPT
by TipmyPip. 09/27/25 10:05
assetHistory one candle shift
by jcl. 09/21/25 11:36
Plugins update
by Grant. 09/17/25 16:28
AUM Magazine
Latest Screens
Rocker`s Revenge
Stug 3 Stormartillery
Iljuschin 2
Galactic Strike X
Who's Online Now
0 registered members (), 16,232 guests, and 5 spiders.
Key: Admin, Global Mod, Mod
Newest Members
krishna, DrissB, James168, Ed_Love, xtns
19168 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Snap #321990
05/04/10 07:42
05/04/10 07:42
Joined: Jan 2002
Posts: 300
Usa
Rich Offline OP
Senior Member
Rich  Offline OP
Senior Member

Joined: Jan 2002
Posts: 300
Usa
Hi, here is a small function that I wrote to snap values. I was looking for a code or an engine function to do this, but I did not find one, so I attempted to write it myself.

I am sure it could be optimized, as I am not the best programmer.

It could be used to snap a camera to every 45 degrees, or a grid position to every 8 quants, ect.

63 factored by 45 returns 45, -487 factored by 45 returns -495 ect..

Hope someone finds it useful, and that it doesn't suck smirk

Code:
function snap(value,snapv)
	{
	var temp;
	var temp2;

	if(snapv <= 0.2 && snapv >= -0.2)
	   {return(0);} //exit on zero, no dividing by zero!
	
	temp = integer(abs(value)/snapv); 
	temp = temp*snapv; 
	temp2 = temp; //set
	temp = integer(abs(value)-temp); 
	
	if (temp < abs(snapv)*.51)
		{ 
		return(integer(temp2*sign(value)));
		}
	temp2 = temp2+abs(snapv);
	return(integer(temp2*sign(value))); 
}




A8 com / A7 free
Re: Snap [Re: Rich] #321991
05/04/10 07:59
05/04/10 07:59
Joined: Nov 2007
Posts: 1,143
United Kingdom
DJBMASTER Offline
Serious User
DJBMASTER  Offline
Serious User

Joined: Nov 2007
Posts: 1,143
United Kingdom
It's great that you are helping the community with contributions, but i must be honest that this particular example does seem to be a little long-winded...

function(double value, int snap)
{
return integer(value / snap) * snap;
}

I use this in a little mouse-snapping contribution I added...

http://www.opserver.de/ubb7/ubbthreads.php?ubb=showflat&Number=285586#Post285586

... But it's good to see you are at least trying to work things out for yourself, keep it up.

Thanks, DJB.

Re: Snap [Re: DJBMASTER] #321995
05/04/10 08:31
05/04/10 08:31
Joined: Jan 2002
Posts: 300
Usa
Rich Offline OP
Senior Member
Rich  Offline OP
Senior Member

Joined: Jan 2002
Posts: 300
Usa
Wow, that is much shorter! Thanks a lot for the example code and feedback.

And OT: Really looking forward to your 3DGS Easy Scripter project!

Take care.


A8 com / A7 free

Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

Gamestudio download | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1