• entries
59
114
• views
22827

# Code without meaning.

59 views

#ifndef CLAMP#define CLAMP(v,c) if (abs(v) > abs(c)){if (v < 0){v = c;if (v > 0)v = 0 - v;}else{v = c;if (v < 0)v = 0 - v;}}#endif

Hmmm. This is hanging around, and is something I wrote over a year ago, possibly more. I have not real context, except that it had some purpose in controlling a velocity. it has no point at all...

Remember kids, don't code at 2am. Or look at old code at 2am, or smoke pot. There is a time and a place for everything, and it's called college.

Edit: Ok looking at it again. I see purpose, but poorly designed purpose. It seems to be designed to handle casues where v and c are signed different (or pivoted around 0). As such, iirc, if I am trying to bring a velocity to 0, the idea that the increment would clamp to zero (and a very very poorly calculated zero at that), instead of going over.

xvelocity = -3f;
CLAMP(xvelocity,0.004f * timeinfo.elapsedTime);

The funny thing is I have a generic form of that idea in the same header called MOVETOPIVOT providing an abitrary pivot point. So it can be used for stopping, and hitting a max velocity.

Conversly, why am I doing crazy calculations instead of something simple. like >

hmmm

Edit+1:not so much just poorly designed, but full of boogs :P

Edit+2:Ok, to give an idea of how the player the 2d player (Dev), again old code, and looking at it I see lots of room for improvemnet. Things like movement through space depend on the final state of user input at the start of a game loop. I *should* track time of keypress to give finer control of things like jumping, but since a game loop is "short" most users would never be able to press and release the jump key in a single frame unless intentionally doing so, conversly this sucks when you do it on accident. Hmm.

One more thing about the below code, getState and setState are primed at runtime with animations, timing and state, so a state change indicates a new sprite on screen. Dev had a few animations, walk, to run, flying, coasting, jumping etc. Swimming too iirc.
void DevController::run(TimeInfo &timeinfo){		int xdir=0;	int ydir=0;	bool sprint=false;		int jump=0;	eventRcv.resetSel();	while(eventRcv.existSel())	{		switch((DevEvents)*(eventRcv.getDataSel()))		{		case moveLeft:			xdir--;			break;		case moveRight:			xdir++;			break; 		case sprintOn:			sprint = true;			break;		case sprintOff:			sprint = false;			break;		case jumpOn:			jump++;			break;		case jumpOff:			jump--;			break;		}		eventRcv.moveSelForward();	}	eventRcv.removeAllNodes();	switch(xdir)	{	case -1:		if (sprint)      		{			myObject->velocity.setX(MOVETOPIVOT(myObject->velocity.getX(),JS_DEV_HORZ_RUN_ACCL * timeinfo.elapsedTime,0-JS_DEV_HORZ_SPRINT_VEL_MAX));		}		else		{			myObject->velocity.setX(MOVETOPIVOT(myObject->velocity.getX(),JS_DEV_HORZ_ACCL * timeinfo.elapsedTime,0-JS_DEV_HORZ_VEL_MAX));		}		break;	case 0:      		myObject->velocity.setX(MOVETOPIVOT(myObject->velocity.getX(),JS_DEV_HORZ_DECEL * timeinfo.elapsedTime,0));		break;	case 1:		if (sprint)      		{			myObject->velocity.setX(MOVETOPIVOT(myObject->velocity.getX(),JS_DEV_HORZ_RUN_ACCL * timeinfo.elapsedTime,JS_DEV_HORZ_SPRINT_VEL_MAX));		}		else		{			myObject->velocity.setX(MOVETOPIVOT(myObject->velocity.getX(),JS_DEV_HORZ_ACCL * timeinfo.elapsedTime,JS_DEV_HORZ_VEL_MAX));		}		break;	}	switch(myObject->getState())	{	case JS_DEV_STILL_LEFT:	case JS_DEV_STILL_RIGHT:		if (xdir > 0)			this->myObject->setState(JS_DEV_WALK_RIGHT);		else if (xdir < 0)			myObject->setState(JS_DEV_WALK_LEFT);		break;	case JS_DEV_WALK_LEFT:		if (myObject->velocity.getX() > 0)			myObject->setState(JS_DEV_WALK_RIGHT);      		else if (abs(myObject->velocity.getX()) > JS_DEV_HORZ_VEL_MAX)		{			if (abs(myObject->velocity.getX()) > JS_DEV_HORZ_RUN_VEL_MAX)			{				myObject->setState(JS_DEV_SPRINT_LEFT);			}			else			{				myObject->setState(JS_DEV_RUN_LEFT);			}		}		else if (myObject->velocity.getX() == 0)			myObject->setState(JS_DEV_STILL_LEFT);		break;	case JS_DEV_WALK_RIGHT:		if (myObject->velocity.getX() < 0)			myObject->setState(JS_DEV_WALK_LEFT);		else if (myObject->velocity.getX() > JS_DEV_HORZ_VEL_MAX)		{			if (myObject->velocity.getX() > JS_DEV_HORZ_RUN_VEL_MAX)			{				myObject->setState(JS_DEV_SPRINT_RIGHT);			}			else			{				myObject->setState(JS_DEV_RUN_RIGHT);			}		}   		else if (myObject->velocity.getX() == 0)			myObject->setState(JS_DEV_STILL_RIGHT);		break;	case JS_DEV_RUN_LEFT:		if (abs(myObject->velocity.getX()) < JS_DEV_HORZ_VEL_MAX)		{			myObject->setState(JS_DEV_WALK_LEFT);		}		break;	case JS_DEV_RUN_RIGHT:		if (abs(myObject->velocity.getX()) < JS_DEV_HORZ_VEL_MAX)		{			myObject->setState(JS_DEV_WALK_RIGHT);		}		break;	}	myObject->position += myObject->velocity.scale(timeinfo.elapsedTime);	lastUpdate = timeinfo.currentTime;}

There are no comments to display.

## Create an account

Register a new account