Snap to grid problem!

Started by
13 comments, last by Cipher3D 20 years, 5 months ago
Cipher:

If all you want is to get the SnapToGrid function to work, just replace the main body of the function (after the IF check) with the two lines I originally posted.


Insanity:
Your code is essentially the same as mine, although I think yours is slower due to the overhead from calling RInt(). Also, RInt is inefficient because of the unnecessary if(). Try this:

int RInt(float u){  int i = int(u + 0.5);  return(i);}// or this, which saves stack space by not creating ''i''int RInt(float u){  return int(u + 0.5); // may give compiler warning}

Advertisement
Yes - I fixed it. Thanks to all of you who gave me insight into this!

Special thanx to dobbs - who was the catalyst for my solution (man this feels like da grammys).

And thanks to...

Anyway, yeah.
Insanity - your solution didn''t work, although it supposed to...maybe it doesn''t fit into my engine. thanx anyways

//snap to gridvoid CObliteration::SnapToGrid(POINT2D* p){	if (!editorSnapToGrid)		return;	int dx = p->x%16;	int dy = p->y%16;		if (dx < 0)	{		//inverse and take the absolute value at the same time		dx = abs(dx);		dx = 16-dx;	}	if (dy < 0)	{		dy = abs(dy);		dy = 16-dy;	}	p->x -= dx;	p->y -= dy;	if (dx >= 8)			p->x += 16;	if (dy >= 8)		p->y += 16;}


OH yeah, does anybody know whats the cylces of a % operation compared to a mul or div?
Thanx,

Ciph


Time to move on.
I eat heart attacks
Glad to see you've got it working, Cipher. BlackSheep, thanks for the optimization tip. I'm still in the writing stage, and haven't done much optimization yet. Probably wouldn't have caught that one anyway ...


Coding Stuff ->  [ iNsAn1tY Games | DarkVertex | How To Do CSG | Direct3D Vs. OpenGL | Google ]
Fun Stuff    ->  [ Evil T-Shirts | Stick-Based Comedy | You're Already Here | The Best Film Reviews ]

[edited by - iNsAn1tY on August 16, 2003 6:49:47 AM]
My opinion is a recombination and regurgitation of the opinions of those around me. I bring nothing new to the table, and as such, can be safely ignored.[ Useful things - Firefox | GLee | Boost | DevIL ]
gridstepping==gridspacing

void CKernel::SnapToGrid(int &x,int &y,CViewInfo cInfo){	float xdiv=(float)x/(float)cInfo.m_iGridStepping;	float ydiv=(float)y/(float)cInfo.m_iGridStepping;	float xmed=floor(xdiv);	float ymed=floor(ydiv);	if(xdiv<=(xmed+0.5))		x=(int)(cInfo.m_iGridStepping*xmed);	else		x=(int)(cInfo.m_iGridStepping*(xmed+1));	if(ydiv<=(ymed+0.5))		y=(int)(cInfo.m_iGridStepping*ymed);	else		y=(int)(cInfo.m_iGridStepping*(ymed+1));}
http://www.8ung.at/basiror/theironcross.html
check the date of the OP.
I eat heart attacks

This topic is closed to new replies.

Advertisement