00:00
00:00
Newgrounds Background Image Theme

Someone gifted Ministerhomer supporter status!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

Ball Physics 2016-05-22 02:59:13


First off, understand that I am transitioning to AS3 soon. I made this in an hour because my curiosity got the better of me and I wanted to see if I could do it before migrating. That said, the logic is the same and this is what I have:

on(press){
	startDrag(this);
	dragging = true;
	xmove = 0;
	ymove = 0;
}

onClipEvent(load){
	var r:Number = _height/2;
	var shapeFlags:Array=[];
	var points:Array = [0, 30, 45, 60, 90, 120, 135, 150, 180, 210, 225, 240, 270, 300, 315, 330];
	var xmove:Number = 0;
	var ymove:Number = 0;
	var dragging:Boolean=false;
	var lastX:Number = _parent._xmouse;
	var lastY:Number = _parent._ymouse;
	var curX:Number = _parent._xmouse;
	var curY:Number = _parent._ymouse;
	
	function getPoint(angle:Number){
		var pX:Number = r * Math.cos(angle);
		var pY:Number = r * Math.sin(angle);
		return [pX, pY, angle];
	}
	
	for(i=0; i<points.length; i++){
		shapeFlags.push(getPoint(points[i]));
	}
	
}

onClipEvent(enterFrame){
	
	if(_x<0){	_x+=100;	xmove = 0;	}
	if(_y<0){	_y+=100;	ymove = 0;	}
	if(_x>1200){	_x-=100;	xmove = 0;	}
	if(_y>600){	_y-=100;	ymove=0;	}
	
	lastX -= (lastX - _parent._xmouse)/2;
	lastY -= (lastY - _parent._ymouse)/2;
	
	xmove*=0.99;
	ymove*=0.99;
	
	if(dragging == false){
		ymove+=1;
		_y+=ymove;
		_x+=xmove;
	
		for(i in shapeFlags){
			while(_parent.surface.hitTest(_x + shapeFlags[i][0], _y + shapeFlags[i][1], true)){
				switch(i){
					case 14: case 15: case 0: case 1: case 2: _x-=1; break;
					case 2: case 3: case 4: case 5: case 6: _y+=1; break;
					case 6: case 7: case 8: case 9: case 10: _x+=1; break;
					case 10: case 11: case 12: case 13: case 14: _y-=1; break;
				}
				_y-=1; //REMEMBER THIS
				_parent.dis=i;
				ymove-=Math.sin(shapeFlags[i][2]);
				if(i!=11){
					xmove-=Math.cos(shapeFlags[i][2]);
				}
			}
		}
	} else {
		if(!Key.isDown(1)){
			xmove = (_parent._xmouse - lastX)/2;
			ymove = (_parent._ymouse - lastY)/2;
			dragging = false;
			stopDrag();
		}
	}
	
}

The basic premise is that the ball has a bunch of shapeflags placed around its edge that determine how the ball reacts with stuff around it. The index of the shapeflag begins at the point directly to the right of the center of the ball and moves around it counter-clockwise. The annoying thing is that the _y-=1; part of the while script is an obvious interference with the code any time the ball hits a wall sideways or on the top, which prevents things like loop-de-loops and wall bouncing. The funny thing is that if I remove this code, the while statement jams. Despite the fact that I have 'pushing' code within the switch-case statement, none of these statements will fire, thus I have to rely on that stupid _y-=1; code to prevent an infinite loop.

I've tried remaking the switch-case statements using a bunch of if-else commands, and this seems to make my ball go ballistic and shoot off to the side of the screen.

if(i > 13 || i < 3){
					_x-=1;
				} else if(i > 2 && i < 7){
					_y+=1;
				}else if(i > 5 && i < 11){
					_x+=1;
				} else if(i > 9 && i < 15){
					_y-=1;
				}

Any tips on how I could get this to work? I feel as if it should be simple to prevent the ball from getting sucked into the walls and ceilings but for some reason my attempts at using the shapeflags are all failing. I also want to see if I can get this ball to bounce, but that's not my main concern right now.


If I offer to help you in a post, PM me to get it. I often forget to revisit threads.

Want 180+ free PSP games? Try these links! - Flash - Homebrew (OFW)