Recent posts by Sentient351 on Kongregate

Flag Post

Topic: Game Programming / Best Resources for Learning Java for Android Development

Hey everyone!

I’ve decided this week to start learning Java so I can make stuff for Android and iOS. Android is no longer supporting flash as of version 4.1. I’m fairly comfortable with AS3, and I’d like to learn Java quickly, at least enough to start translating stuff I’ve written in Actionscript to Java for Android. What are the best resources you’ve found for this? I have nothing but free time at the moment, so I’d appreciate any advice!

 
Flag Post

Topic: Game Programming / Finding intersections with angled vectors

I’m sorry, but what is an object field? Do you mean just making them available as a stable variable I can access from the rectangle model once it’s created, or using an object as an array to store them? Forgive my ignorance, but I’ve never seen that term before, and I’m very curious.

I do realize there are a lot of unnecessary things in my code right now. I was going to work on it to make it more efficient, but I didn’t seem to notice any slowdown even on my 5 year old laptop, so I figured I’d just work on other parts for now. There are obviously a lot of Math.sqrt calls I could eliminate simply by calculating them in advance, or setting them up to only run them when something changes (like velocity). It seems like this might be pretty easy to implement, after all. I’ll go work on it a bit and get back to you if I need anymore help.

Thanks!

 
Flag Post

Topic: Game Programming / Finding intersections with angled vectors

Sorry about that. Let me edit them out. The reason I didn’t the first time is because there seems to be a problem realigning things when I post them, but let me make it more legible real quick. Also, would it be more useful if I made a basic psuedocode translation? Also, for some reason, the edit button has been kind of quirky. It has occasionally not been letting me edit, for some reason.

 
Flag Post

Topic: Game Programming / Finding intersections with angled vectors

The only thing that would probably be relevant is the circleVsRectangle function, which finds blocks close enough to bother checking, finds the closest collision point, and then checks that for a collision. VectorModels are just a class that allow me to access data from vectors such as magnitude, normalized vectors, vector normals, etc. I’m going to consolodate some of the redundant functions later on, but if you have any suggestions about the easiest way to do what I want, I’d be happy to hear them.

V0 the ball’s motion vector, between it’s position and it’s next position with its velocity.
V1 is the vector I’m checking for a collision.
V2 is a helper vector between the the ball’s position and the beginning of the target vector
v3 is the final vector between the ball’s collision point and the intersection point
v4 and v5 check to make sure the intersection is actually on the vector


public static function circleVsRectangle(gameModel:Object, c1:CircleModel, b:Number = 1, f:Number = 1):Object
{
	var model:Object = gameModel;
	var data:Object = new Object();
	var rad:Number = c1.radius * MOD;
	var cx:Number = c1.pxpos;
        var cy:Number = c1.pypos;
			
	var v0:VectorModel = new VectorModel();
        var v1:VectorModel = new VectorModel();
	var v2:VectorModel = new VectorModel();
	var v3:VectorModel = new VectorModel(0, 0, 500, 500);
	var v4:VectorModel = new VectorModel();
	var v5:VectorModel = new VectorModel();
	var cornerVector:VectorModel = new VectorModel();
	var corner:Point;
			
	var v0t:VectorModel = new VectorModel();
        var v1t:VectorModel = new VectorModel();
	var v2t:VectorModel = new VectorModel();
	var v3t:VectorModel = new VectorModel();
	var hitIndex:uint = 0;
	var sideIndex:uint = 0;
			
	var cornerData:Object = new Object();
			
	for(var i:uint = 0; i < model.blockModels.length; i++)
	{
		var r1:RectangleModel = model.blockModels[i];
		var inRange:Boolean = false;
		var cv0:VectorModel = new VectorModel(cx, cy, cx + c1.vx, cy + c1.vy);
		var cv1:VectorModel = new VectorModel(cx, cy, r1.xpos, r1.ypos);
				
		if(cv1.m < cv0.m + r1.width)
		{
			inRange = true;
		}
		if(model.blockModels[i].active && !model.blockModels[i].hit && inRange)
		{	
			var hw:Number = r1.width * 0.5;
			var hh:Number = r1.height * 0.5;
			var perp1:Number;
			var perp2:Number;
			var t:Number;
			var ix:Number = 0;
			var iy:Number = 0;
			var dp1:Number;
			var dp2:Number;
			var p1:Point = new Point(r1.xpos - hw, r1.ypos + hh);
			var p2:Point = new Point(r1.xpos - hw, r1.ypos - hh);
			var p3:Point = new Point(r1.xpos + hw, r1.ypos - hh);
			var p4:Point = new Point(r1.xpos + hw, r1.ypos + hh);
			var s1:VectorModel = new VectorModel(p1.x, p1.y, p2.x, p2.y);//left side
			var s2:VectorModel = new VectorModel(p2.x, p2.y, p3.x, p3.y);//top side
			var s3:VectorModel = new VectorModel(p3.x, p3.y, p4.x, p4.y);//right side
			var s4:VectorModel = new VectorModel(p4.x, p4.y, p1.x, p1.y);//bottom side
			var sides:Array = new Array(s1, s2, s3, s4);
					
			for(var j:uint = 0; j < sides.length; j++)
			{
				switch(j)
			        {
			                case 0:
					v0t.update(cx + rad, cy, cx + rad + c1.vx, cy + c1.vy);
					break;
							
					case 1:
					v0t.update(cx, cy + rad, cx + c1.vx, cy + rad + c1.vy);
					break;
							
					case 2:
					v0t.update(cx - rad, cy, cx - rad + c1.vx, cy + c1.vy);
					break;
							
					case 3:
					v0t.update(cx, cy - rad, cx + c1.vx, cy - rad + c1.vy);
					break;	
				}
				v1t.update(sides[j].a.x, sides[j].a.y, sides[j].b.x, sides[j].b.y);
				v2t.update(v0t.a.x, v0t.a.y, v1t.a.x, v1t.a.y);
				perp1 = VectorMath.perpProduct(v2t, v1t);
				perp2 = VectorMath.perpProduct(v0t, v1t);
				t = perp1 / perp2;
				ix = v0t.a.x + v0t.vx * t;
				iy = v0t.a.y + v0t.vy * t;
				v3t.update(v0t.a.x, v0t.a.y, ix, iy);
				v4.update(v1t.a.x, v1t.a.y, ix, iy);
				v5.update(v1t.b.x, v1t.b.y, ix, iy);
				dp1 = VectorMath.dotProduct(v2t, v1t.ln);
				dp2 = VectorMath.dotProduct(v0t, v3t);
						
				if(dp1 == 0)
				{
					dp2 *= -1;
				}
						
				if(v3t.m != 0.001 && v3t.m < v3.m && v4.m <= v1t.m && v5.m <= v1t.m && dp1 <= 0                               && dp2 > 0)
				{
					v0.update(v0t.a.x, v0t.a.y, v0t.b.x, v0t.b.y);
					v1.update(v1t.a.x, v1t.a.y, v1t.b.x, v1t.b.y);
					v2.update(v2t.a.x, v2t.a.y, v2t.b.x, v2t.b.y);
					v3.update(v3t.a.x, v3t.a.y, v3t.b.x, v3t.b.y);
					hitIndex = i;
					sideIndex = j;
				}
			}
		}
	}
	cornerData = checkCornerCollision(model, c1);
	cornerVector = cornerData.vector;
	corner = cornerData.corner;
			
	if(cornerVector.m != 0.001 && cornerVector.m < v3.m)
	{
		v3.update(cornerVector.a.x, cornerVector.a.y, corner.x, corner.y);
		ix = cornerData.ix;
		iy = cornerData.iy;
		data.collision = circleVsCorner(c1, cornerVector.a.x, cornerVector.a.y, corner.x, corner.y, ix, iy);
		data.index = cornerData.index;
		return data;
	}
	ix = v3.b.x;
	iy = v3.b.y;
				
	if(v3.m < v0.m)
	{
		switch(sideIndex)
	        {
			case 0:
		        c1.xpos = ix - rad;
			c1.ypos = iy;
			c1.vx *= -1;
			break;
						
			case 1:
			c1.ypos = iy - rad;
			c1.xpos = ix;
			c1.vy *= -1;
			break;
						
			case 2:
			c1.xpos = ix + rad;
			c1.ypos = iy;
			c1.vx *= -1;
			break;
					
			case 3:
			c1.ypos = iy + rad;
			c1.xpos = ix;
			c1.vy *= -1;
			break;		
		}
		data.collision = true;
		data.index = hitIndex;
		return data;
	}
	data.collision = false;
	return data;	
}
public static function checkCornerCollision(gameModel:Object, c1:CircleModel, b:Number = 1):Object
{
	var model:Object = gameModel;
	var data:Object = new Object();
	var rad:Number = c1.radius * MOD;
	var cx:Number = c1.pxpos;
	var cy:Number = c1.pypos;
	
	var v1:VectorModel = new VectorModel();
	var v2:VectorModel = new VectorModel();
	var v3:VectorModel = new VectorModel(0, 0, 500, 500);
	var rad0:VectorModel = new VectorModel();
	var cornerVector:VectorModel = new VectorModel(0, 0, 500, 500);
	var corner:Point = new Point(1000, 1000);
			
	var hitIndex:uint = 0;
			
	for(var i:uint = 0; i < model.blockModels.length; i++)
	{
		var r1:RectangleModel = model.blockModels[i];
		var inRange:Boolean = false;
		var cv0:VectorModel = new VectorModel(cx, cy, cx + c1.vx, cy + c1.vy);
		var cv1:VectorModel = new VectorModel(cx, cy, r1.xpos, r1.ypos);
		
		if(cv1.m < cv0.m + r1.width)
		{
			inRange = true;
		}
		if(model.blockModels[i].active && !model.blockModels[i].hit && inRange)
		{
			var intersectionData:Object = new Object();
			var hw:Number = r1.width * 0.5;
			var hh:Number = r1.height * 0.5;
			var cix:Number;
			var ciy:Number;
			var ixt:Number;
			var iyt:Number;
			var ix:Number;
			var iy:Number;
			var px:Number;
			var py:Number;
			var p1:Point = new Point(r1.xpos - hw, r1.ypos + hh);
			var p2:Point = new Point(r1.xpos - hw, r1.ypos - hh);
			var p3:Point = new Point(r1.xpos + hw, r1.ypos - hh);
			var p4:Point = new Point(r1.xpos + hw, r1.ypos + hh);
			var points:Array = new Array(p1, p2, p3, p4);
			
			var v4:VectorModel = new VectorModel();
			
			for(var j:uint = 0; j < points.length; j++)
			{
				intersectionData = lineIntersectingCircle(c1, points[j].x, points[j].y);
				if(intersectionData.intersects)
			        {
				        px = points[j].x;
				        py = points[j].y;
				        cix = intersectionData.intersection.x;
				        ciy = intersectionData.intersection.y;
				        rad0.update(cix, ciy, c1.pxpos, c1.pypos);
				        ixt = px + rad0.vx;
				        iyt = py + rad0.vy;
				        v1.update(ixt, iyt, ixt + c1.vx, iyt + c1.vy);
				        v2.update(ixt, iyt, px, py);
				        v3.update(cix, ciy, px, py);
			        }
			        if(v3.m < cornerVector.m)
				{
					hitIndex = i;
					ix = ixt;
					iy = iyt;
					cornerVector.update(v3.a.x, v3.a.y, v3.b.x, v3.b.y);
					corner = points[j];
				}
			}
		}
	}
	data.index = hitIndex;
	data.vector = cornerVector;
	data.corner = corner;
	data.ix = ix;
	data.iy = iy;
	return data;
}
private static function lineIntersectingCircle(c1:CircleModel, px:Number, py:Number):Object
{	
	var data:Object = new Object ();
	data.intersects = false;
	
	var cx:Number = c1.xpos;
	var cy:Number = c1.ypos;
	var vx:Number = c1.vx * 100;
	var vy:Number = c1.vy * 100;
	var p0:Point = new Point(px, py);
	var p1:Point = new Point(px - vx, py - vy);
	var p2:Point = new Point(c1.pxpos, c1.pypos);
	var rad:Number = c1.radius * MOD;
		
	var a:Number = (p1.x - p0.x) * (p1.x - p0.x) + (p1.y - p0.y) * (p1.y - p0.y);
	var b:Number = 2 * ((p1.x - p0.x) * (p0.x - p2.x) +(p1.y - p0.y) * (p0.y - p2.y));
	var cc:Number = p2.x * p2.x + p2.y * p2.y + p0.x * p0.x + p0.y * p0.y - 2 * (p2.x * p0.x + p2.y * p0.y) - rad * rad;
	var determinant:Number = b * b - 4 * a * cc;
			
	if(determinant > 0)
	{
		var e:Number = Math.sqrt (determinant);
		var u1:Number = (-b + e) / (2 * a);
		var u2:Number = (-b - e) / (2 * a);
		
		if ((u1 < 0 || u1 > 1) && (u2 < 0 || u2 > 1)) 
		{
			if ((u1 < 0 && u2 < 0) || (u1 > 1 && u2 > 1)) 
			{
				data.intersects = false;
				return data;
			} 
			else 
			{
				data.intersects = false;
				return data;
			}
		} 
		else 
		{
			if (0 <= u2 && u2 <= 1) 
			{
				data.intersects = true;
				data.intersection = Point.interpolate (p0, p1, 1 - u2);
				return data;
			}
		}
	}
	return data;
}
private static function circleVsCorner(c1:CircleModel, cx:Number, cy:Number, px:Number, py:Number, ix:Number, iy:Number, ba:Number = 1):Boolean
{
	var v0:VectorModel = new VectorModel(cx, cy, px, py);
	var v1:VectorModel = new VectorModel(cx, cy, cx + c1.vx, cy + c1.vy);
        var rad:Number = c1.radius * MOD;
			
	if(v0.m <= v1.m)
	{
		c1.xpos = ix;
		c1.ypos = iy;
		
		v0.update(c1.xpos, c1.ypos, px, py);
		v1.update(c1.xpos, c1.ypos, c1.xpos + c1.vx, c1.ypos + c1.vy);
				
		var bounce:VectorModel = VectorMath.bounce(v1, v0.ln);
				
		c1.vx = bounce.vx * ba;
		c1.vy = bounce.vy * ba;
				
		return true;
	}
	else
	{
		return false;
	}
}

 
Flag Post

Topic: Game Programming / Finding intersections with angled vectors

Oops, accidentally hit submit before posting code. Give me a second.

 
Flag Post

Topic: Game Programming / Finding intersections with angled vectors

I’ve finally finished my collision code for my ball and block game, but I’ve decided My process for now is projecting the ball’s motion vector from its top, bottom, left, and right points and checking for the closest collision with four vectors drawn representing the four sides of rectangles. I’m checking for corner collisions separately, and everything is working so far.

I want to have some of the blocks rotating and moving in patterns to prevent monotony taking over, though, and I was wondering if you had suggestions for finding the best way to do this. I was thinking of creating a separate invisible layer to orient the ball to the block’s rotation angle, running a normal collision, and then reversing it to the proper orientation after the collision. I’m sure I can figure this out for myself, but rather than screwing around, I figured it might be wise to post here to see if anyone else had some concrete ideas on how to better implement this. The problem with just running my current collision code, even if I adjust and angle vectors is that it’s configured to test only those four points against the vectors of the blocks, which are all grid aligned, so I only need to deal with 90 degree angles aligned to the stage.

My code for corner collisions finds points other than those four as the closest, collision, but it’s only set up to find the point of contact based on a vector between the ball and a single point projected the inversion of the ball’s velocity toward the ball.

Does anyone know of any simpler ways to do this, or should I just set it up to temporarily orient the ball to the rotation of the block so I’m essentially using the same code? I started this whole project to teach myself more about working with vectors, especially vector collisions, so any advice from more experienced programmers would be greatly appreciated.

Here’s the relevant code. I still have to go through and reduce redundant code, but it should be understandable.

 
Flag Post

Topic: Game Programming / Collisions...

Are you using an overlap based collision system (in the case of balls, for example, checking if the distance between balls is less than their combined radii)? If so, then once the ball starts moving faster than that distance, it’s possible to overshoot a collision or pass through too far and have the collision reposition the ball I the wrong place. Also, sometimes, if you’re using a single loop and returning after it detects a single collision, it’s possible sometimes for it to find a collision within the balls trajectory, but for that collision not to be the closest actual collision, just the first one you encounter in your loop for that frame.

You should post the relevant code so people can try to figure out exactly what’s going on, though, since the problem might be something completely different depending on how you’ve set it up.

 
Flag Post

Topic: Game Programming / [AS3] Weird bug with arrays

Oops! That’s good to know. I guess it was my fault after all. Thanks for the information!

 
Flag Post

Topic: Game Programming / [AS3] Weird bug with arrays

I tried creating a new array with a single variable like this:

var array:Array = new Array(10);

But when I traced it, it came back completely blank. It worked fine if I added more items, pushed the item into the array, or created it with

var array:Array = 10;

I tried this on flash pro CS6 and Flash Builder 4.6 with the same result. I wonder if this is a new bug. I don’t seem to recall having this problem before.

 
Flag Post

Topic: Game Programming / [AS3] Odd problem embedding sounds

Yeah, I figured it would turn out to be just one of those odd untraceable bugs. It’s probably just a random Flash Builder problem.

NineFiveThree: It wasn’t giving me any error. It just pointed to the line it didn’t like when it terminated, but didn’t give me any information about what it thought was wrong with that line.

The sounds are just piano samples of individual notes, which I need to be able to play without interrupting other notes. I’m rather new to dealing with sound in Flash, actually. Do I not need individual SoundChannel variables and channel objects to play each note? If there is a way to simplify this, import many sounds at once, or use fewer channels, I’d be very grateful if you could advise me, as I will have to put in several more octaves of individual notes later on.

 
Flag Post

Topic: Game Programming / [AS3] Odd problem embedding sounds

Ok, well I seem to have fixed the problem for now. The class must have become corrupted somehow, but I have no idea why, or why what I did fixed it. I restarted my computer, which didn’t help. After that, I tried commenting out blocks of code including the embed command as well as its associated variables, and each time I saved and ran it, the error would move to the next block, even after I restored the previous code. I did that to each block, one by one (commenting out larger sections didn’t work and would just give me the same error I had before). On the last one, it jumped to the first line of the constructor. I commented that out, ran it, then restored it, saved, and ran it again. Now it seems to be working fine, but I’m very curious why that happened in the first place, if someone could shed some light on it.

 
Flag Post

Topic: Game Programming / [AS3] Odd problem embedding sounds

Hi everyone,

I’m about to start implementing sound into my game, and I’ve set up a test class. I ran it exactly as it is now, and at first it worked fine, but now it’s terminating as soon as I call for a new sound. This code was working fine, and the only thing I did after testing it was write a new twelve tone matrix generating class, but that doesn’t directly interact with any other class, and I’ve commented out any references to it. I’m using Flash Builder 4.6, and I’ve already tried simply restarting the program, to no avail. The class is really basic, just made as a preliminary test, but I have no idea why it’s bugging out now. Here’s the code:

 

package
{
	import flash.media.Sound;
	import flash.media.SoundChannel;
	
	public class TwelveTone
	{	
		[Embed(source="notes/C4.mp3")]
		private var C4:Class;
		private var c4:Sound = new C4();
		private var c4Channel:SoundChannel = new SoundChannel();
		
		[Embed(source="notes/C#4.mp3")]
		private var Cs4:Class;
		private var cs4:Sound = new Cs4();
		private var cs4Channel:SoundChannel = new SoundChannel();
		
		[Embed(source="notes/D4.mp3")]
		private var D4:Class;
		private var d4:Sound = new D4();
		private var d4Channel:SoundChannel = new SoundChannel();
		
		[Embed(source="notes/D#4.mp3")]
		private var Ds4:Class;
		private var ds4:Sound = new Ds4();
		private var ds4Channel:SoundChannel = new SoundChannel();
		
		[Embed(source="notes/E4.mp3")]
		private var E4:Class;
		private var e4:Sound = new E4();
		private var e4Channel:SoundChannel = new SoundChannel();
		
		[Embed(source="notes/F4.mp3")]
		private var F4:Class;
		private var f4:Sound = new F4();
		private var f4Channel:SoundChannel = new SoundChannel();
		
		[Embed(source="notes/F#4.mp3")]
		private var Fs4:Class;
		private var fs4:Sound = new Fs4();
		private var fs4Channel:SoundChannel = new SoundChannel();
		
		[Embed(source="notes/G4.mp3")]
		private var G4:Class;
		private var g4:Sound = new G4();
		private var g4Channel:SoundChannel = new SoundChannel();
		
		[Embed(source="notes/G#4.mp3")]
		private var Gs4:Class;
		private var gs4:Sound = new Gs4();
		private var gs4Channel:SoundChannel = new SoundChannel();
		
		[Embed(source="notes/A4.mp3")]
		private var A4:Class;
		private var a4:Sound = new A4();
		private var a4Channel:SoundChannel = new SoundChannel();
		
		[Embed(source="notes/A#4.mp3")]
		private var As4:Class;
		private var as4:Sound = new As4();
		private var as4Channel:SoundChannel = new SoundChannel();
		
		[Embed(source="notes/B4.mp3")]
		private var B4:Class;
		private var b4:Sound = new B4();
		private var b4Channel:SoundChannel = new SoundChannel();
	
		[Embed(source="notes/C5.mp3")]
		private var C5:Class;
		private var c5:Sound = new C5();
		private var c5Channel:SoundChannel = new SoundChannel();
		
		public function TwelveTone():void
		{
			c4Channel = c4.play();
		}
	}
}

And it’s pointing to line 15 when it quits:

private var cs4:Sound = new Cs4();

The really odd thing is that at first it was giving me an error at line 9:

private var C4:Class;

After it bugged out, I changed it to play C#4 and commented out the code embedding C4, and it bugged out on line 15, so I changed it back to the way it is now, calling for C4, and instead of bugging out on line 9, it started pointing to line 15 when it terminated.

Does anyone have any ideas? I haven’t moved any files or changed anything else, and it doesn’t give me any more information about the error than just pointing to the line. Is there a better way to embed sounds in Flash Builder? Maybe it created class files for the notes and corrupted them? I really have no idea why it’s not working now.

Anyway, thanks for any help you can offer.

 
Flag Post

Topic: Game Programming / [AS3] Finding intersection coordinates of point and circle

Ok, I figured it out after waking up at 7:30 thinking about it. I got so set on finding it the way I described that I didn’t think of the obvious solution. (I guess that happens a lot when sleep deprived.) I set it up now to project a vector from the corner with the ball’s velocity and find the intersection point with the ball (inverted, so it’s going in the opposite direction, but parallel to the circle’s motion vector). Then, just take another vector from that intersection point to the ball’s center, and create another vector from the corner using the vx and vy of the new vector, and there is the exact position that the ball will be at when it hits the corner.

I’ll post the code tomorrow, if anyone is interested.

 
Flag Post

Topic: Game Programming / Corner Deflection

Oh, sorry. I should have been more specific when posting the code. v0.ln is the left normal of the v0 vector, which is the vector between the center of the circle and the corner in the collision frame.

 
Flag Post

Topic: Game Programming / Corner Deflection

Yes. I found dx and dy by dividing vx and vy by the vector’s magnitude. The sum of the square of dx and dy do equal 1.

 
Flag Post

Topic: Game Programming / Corner Deflection

Oops! I meant v1.vy * v2.dx PLUS v1.vy * v2.dy as the formula for the dot product in my comment. It’s not letting me edit right now, for some reason.

 
Flag Post

Topic: Game Programming / Corner Deflection

So I’ve decided that my intersection based collision code for my Arkanoid game is too bloated and that I’ll just go back to SAT collision detection and increase the framerate and decrease the velocity of the ball to compensate for problems I was having when the ball starts moving too fast, but I’m still not satisfied with my corner collisions with bricks. They often bounce off at too sharp an angle so the ball is traveling with too small a vx or vy velocity.

Before I go and try to rework this myself, I was wondering if anyone else had any tried and true methods for deflecting the ball. My repositioning of the ball and reversal of overlap is acceptable with SAT collision code, but I just need a better way to bounce it. I wish I could just see the collision code from the original arkanoid games, but if anyone has anything that has worked well for them, I’d really appreciate some guidance. If you guys have something that works, feel free to ignore what I’ve posted below, but here’s how I’m doing it now:


private static function checkCornerCollision(c1:CircleModel, cx:Number, cy:Number, ba:Number = 1):Boolean
{
	var v0:VectorModel = new VectorModel(c1.xpos, c1.ypos, cx, cy);
	//this just creates a vector from which I can access properties such      as magnitude, normals, normalized vectors, etc.

        if(v0.m < c1.radius)
	{
	     var o:Number = c1.radius - v0.m;
	     c1.xpos -= o * v0.dx;
	     c1.ypos -= o * v0.dy;
				
	     var v1:VectorModel = new VectorModel(c1.xpos, c1.ypos, c1.xpos +           c1.vx, c1.ypos + c1.vy);
	     var bounce:VectorModel = VectorMath.bounce(v1, v0.ln);
				
	    c1.vx = bounce.vx * ba;
	    c1.vy = bounce.vy * ba;
				
	    trace("corner " + c1.vx, c1.vy)
	    return true;
	}
	else
	{
		return false;
	}
}

And here is the relevant code from the VectorMath class that actually calculates the bounce vector:


public static function bounce(v1:VectorModel, v2:VectorModel):VectorModel
{
	var p1:VectorModel = project(v1, v2);
	var p2:VectorModel = project(v1, v2.ln);
	var bvx:Number = p1.vx + (p2.vx * -1);
	var bvy:Number = p1.vy + (p2.vy * -1);
			
	var bounceVector = new VectorModel(0, 0, 0, 0, bvx, bvy);
			
	return bounceVector;
}
public static function project(v1:VectorModel, v2:VectorModel):VectorModel
{
	var dp1:Number = dotProduct(v1, v2);
// dotProduct = v1.vy * v2.dx - v1.vy * v2.dy 
//dx and dy are the normalized vx and vy of the vector

	var vx:Number = dp1 * v2.dx;
	var vy:Number = dp1 * v2.dy;
			
	var sx:Number = 0;
	var sy:Number = 0;
	var ex:Number = 0;
	var ey:Number = 0;
			
	var projectionVector:VectorModel = new VectorModel();
			
	if(v2.a.x != 0 && v2.a.y != 0)
	{
		sx = v2.a.x;
		sy = v2.a.y;
		ex = v2.a.x + vx;
		ey = v2.a.y + vy;
				
		projectionVector.update(sx, sy, ex, ey);
	}
	else
	{
		projectionVector.update(0, 0, 0, 0, vx, vy);
	}
	return projectionVector;
}

 
Flag Post

Topic: Game Programming / [AS3] Finding intersection coordinates of point and circle

Thanks for the replies. I’ll try the adapting the ellipse vs. line code later. Honestly, at this point, I think the code may run faster if I just change back to a SAT based collision system and double the frame rate. I wrote the intersection based collision system to compensate for situations where the ball’s velocity was too high and it would miss collisions or be repositioned on the wrong axis of the block, but maybe I should just go back to a simpler collision system and lower the velocity per frame while increasing framerate.

 
Flag Post

Topic: Game Programming / [AS3] Finding intersection coordinates of point and circle

I know I probably have overcomplicated my collision class, but at this point, I do need to be able to accurately calculate the collision position. I wrote the rest of the code to base collisions on the intersection of the collision plane and the ball’s motion vector projected from it’s left, right, top, and bottom points, and if I don’t calculate the correct position for the corner collision when the collision point is not one of those four sides, I end up getting screwy results.

 
Flag Post

Topic: Game Programming / [AS3] Finding intersection coordinates of point and circle

It’s actually not a problem with deflection. I have separate code to handle bouncing. The problem is that with the code I’ve set up for now, when the ball hit’s a brick’s corner, the corrected point is calculated based on how much the brick’s corner is inside the ball at the beginning of the collision frame, so when I initially reposition it, it doesn’t position it where it was when it actually collided.

 
Flag Post

Topic: Game Programming / [AS3] Finding intersection coordinates of point and circle

I’m finishing up my collision code for an Arkanoid clone, and I’m having trouble figuring out how to find the exact position of the ball when it touches a corner. I think I know the easiest way to do this, but I’m not quite sure how to do it. Here’s my question.

How do I find the point where a vector extended from the corner with a length equal to the ball’s radius intersects the ball’s motion vector, or

How do I find the angle of the vector between the corner and the ball’s motion vector given that the corner to motion vector length is the ball’s radius?

I’m open to other suggestions for finding this point. Right now I’m just multiplying the normalized vector with the x and y overlap after the corner is inside the ball and reversing it, but it’s inadequate, since it doesn’t position the ball where it was when it first collided.

I’d greatly appreciate any assistance, as this is the last part of the collision detection that I need to finish.

 
Flag Post

Topic: Game Programming / [AS3] Underscores for private variables...again

Thanks for the suggestions. I played around with the namespace idea, and I also tried just making a dedicated accessor namespace to use for all of my getters and setters so I could leave the private variables themselves alone, but that seemed like it would potentially cause unnecessary confusion, having to import the namespace into every class that was using accessors. When I tried to make a separate namespace for private variables to use as the default within the class (using public accessors), AS3 still insisted that I had ambiguous variable references, even when I added a classwide use namespace directive.

Oh well, I guess I’ll just settle on prefixing things as necessary or seeing how leading caps looks. Thanks for your help!

 
Flag Post

Topic: Game Programming / [AS3] Underscores for private variables...again

Hi everyone,

I’m sorry for bringing up this topic again, but I find myself having a problem. I’ve decided to start naming my accessor methods using actionscript’s built in getters and setters as opposed to naming them things like getVx, etc. The “problem” is that there is something I find so visually disruptive about having underscores anywhere in my code, and I was wondering if there were any other conventions that were common in differentiating private variables from their publicly accessible methods?

I realize that this probably seems a bit ridiculous and that it really doesn’t matter what I name them, but I’m also coming to find that I have a bit of OCD when it comes to how my code is visually laid out and organized. I’ve been just adding m_ before my private variables for now so I can just replace all later when I figure out how I want to name them, but are there any alternative conventions that offer a more elegant solution that are fairly standardized?

I hope this doesn’t seem like a waste of forum space. It’s just been something that’s really bugging me, as I like to have things a certain way but also want to make my code easily understandable to other people.

Thanks!

 
Flag Post

Topic: Game Programming / [AS3] Euler vs. Verlet Integration

I didn’t see these replies before responding. Thanks! I’ll go and read that article.

 
Flag Post

Topic: Game Programming / [AS3] Euler vs. Verlet Integration

Hi. Sorry about that. Euler is basically where you calculate the position of an object in the next frame simply by adding its velocity to its current position, for example:

object.x += vx;

Verlet Integration is where you use the previous position of the object to calculate its velocity, so upon instructing the object to move and assigning a velocity, you do so by first subtracting the assigned value for velocity from its current position and assigning that value to a variable for its previous position, then adding the difference between its current position and its previous position to its current position to find the new position. So, if we said it had a vx of 5, it would basically look something like this (this isn’t the actual code, but just a simple example).

previousX = xpos – vx;
xpos += (xpos – previousX);

Then if you wanted to add acceleration or friction, for example, you could simply add the value to its current position along with its current velocity based on its xpos – previousX, which would calculate the new velocity.

Perhaps I just need to see it in a different context interacting with more variables, collisions with other objects, etc.?