157 Forum Posts by "Etherblood"
At 9/22/14 04:24 AM, uglybetty wrote: Is it efficient enough to use a while loop for this, or is there other ways of improving the code?
You will not have any problems with this code, it should work fine.
An other way to do this would be to create an array which contains all 'valid' elements and create a random index to choose one, this approach should be better if your valid elements are sparse.
(if only 5 out of 10000 elements are valid a loop might be a bad idea)
At 9/22/14 09:27 AM, 512Pixel wrote: Thank for the reply Now i have a starting place. My tiles are thesame size as the player or twice the size of the player .
Ive never completed a 4 corner collision . do i begin by taking the vector postion dividing the player width and height by half .By combining the half widths of the player and the half withs of the tiles . As an example if the player is 5 pixes and the tiles are 5 pixels the combined half widths are 5 pixels. Then i see if the haf widths are less than the vectorx and vectoy postion. ist his the method you are reffering to ether blood .I am a begginer and fjust rrecently understood the tile method. Sorry if seems a silly question ....
or is placeing smaller graphics inthe player and test ifthey are touching the tiles postion .
If your player is a MovieClip/Sprite, you can get the bounding rectangle by calling player.getBounds(null) .
Use the 4 corners of this rectangle to calculate 4 two-dimensional tile indices, for example like this:
(this is pseudo code)
var rectangle:Rectangle = player.getBounds(null);
var leftIndex:Number = Math.floor(rectangle.left / tileWidth);
var topIndex:Number = Math.floor(rectangle.top / tileHeight);
var rightIndex:Number = Math.floor(rectangle.right / tileWidth);
var bottomIndex:Number = Math.floor(rectangle.bottom / tileHeight);
//you can now compose the 2d tile-indices for each of the rectangles corners
//upperLeft index = (leftIndex, topIndex)
//upperRight index = (rightIndex, topIndex)
//lowerLeft index = (leftIndex, bottomIndex)
//lowerRight index = (rightIndex, bottomIndex)
//use indices for collision checks
At 9/21/14 07:37 PM, Barzona wrote: I do like that example, but I also want the pieces to move in the same pattern as the head. I don't want them to just drag behind. Now, possibly if the dragon stops curving when it moves, like moves in a straight line, the segments will also stop moving in curves.
You can keep an array where you add the heads new position in front and remove the last position each time the head moves.
You then use each i's position (0, 5, 10, 15... for example) in this array as position for the segments.
At 9/21/14 04:02 PM, 512Pixel wrote: posted below
My grid collision detection calculates the indices for upper left corner and the lower right corner.
Afterwards I loop over all the tiles that are in the rectangle spanned by those 2 indices for collision detection.
(I also put an object in all tiles it overlaps when adding it)
I prefer this over a 4 corner check because it also works with hitboxes which are bigger than a tile.
If all your hitboxes are smaller than a tile a 4 corner check should work fine too.
Make sure to avoid duplicate collision tests. (if all 4 corners are in the same tile you still need to test only once against each object in it)
At 9/20/14 09:35 AM, 512Pixel wrote: Dividing the player postion by the tile width and then rounding the number up( the math.round method).
I don't know exactly what this post is intended to do? (Is this a question? There is no question.)
But normally you would use math.floor (round down), not math.round to calculate the array index.
At 9/16/14 12:14 AM, CanadaSquare wrote: it may seem like a lot, but i would really appreciate any help.
I didn't find the bug, but a lot potential to clean up&shorten your code, it might be easier finding with shortened code.
At the end you have 3 times the same if where the only difference is 1 number. (the percentage)
Set a variable to the percentage depending on the funds and use it in a single if.
You should also look up how to use 'else if' and 'else' in combination with ifs.
Lines like
aDays = aDays - aDays;
also make little sense, why not just write
aDays = 0;
?
I also recommend to use classes.
It should be relatively easy to track the bug if you put traces in the relevant code which help you identify what exactly is executed. Just check which code is executed how often when the bug happens.
At 9/13/14 09:54 AM, MintPaw wrote: Generally you can get away with just pretending both objects are circles.
Just for clarity:
What MintPaw most likely meant is that you can get away with giving most objects circles instead of rectangles as hitboxes.
Not that it is fine to pretend a rectangle (which can have different sidelengths) is a circle.
If a hitbox is a rectangle instead of a circle, it has a good reason for that and does not want to be treated as circle.
At 9/13/14 07:25 AM, Etherblood wrote: intersection if (circle.radius² < (x - circle.x)² + (y - circle.y)²) //pythagoras
Should be:
circle.radius² > (x - circle.x)² + (y - circle.y)²
At 9/13/14 04:36 AM, Hero101 wrote: I'm trying to figure out how to check collision between a circle (player) and a box/rectangle.
If you extend the rectangles border lines you will have 9 regions, depending on in which region the circles center is you'll have to do a different check:
the center is inside the rectangle => circle&rect intersect
the center is above the rectangle => intersection if rect.Top - circle.x < circle.radius
analog for below, right and left
(distance from the circle to the closest line of the rectangle is smaller than radius)
the center is to the upper left => intersection if distance between upper left corner to circle.center < circle.radius
analog for upper right, lower left & lower right
(distance from the circle to the closest corner of the rectangle is smaller than radius)
If you write it out like this it will become a lot of code.
(if, else if, else 3 times inside of another if, else if, else, just to determine the regions)
If we use the regions to calculate the closest point of the rectangle to the circle instead and do a point circle check afterwards it will become shorter:
if (circleX < rect.Left) x = rect.Left //circle is to the left, the closest point must lie on the left rectangle border
else if(circleX > rect.Right) x = rect.Right
else x = circleX
analog for y
now you check if the point lies inside the circle:
intersection if (circle.radius² < (x - circle.x)² + (y - circle.y)²) //pythagoras
At 9/13/14 04:55 AM, Xombiehacker wrote: If you want, I could probably teach you how to find your glitches with little to no hassle, beleive me, a lot of my projects have taken up a lot of room too. but it's not based on the amount of code there is, it's simply based on what code is actually relevant to the glitch it's self
The bug I'm having trouble with is most likely a float rounding error in my polygon clipping code, I don't know where exactly it happens because my tests also have roundoff errors, it only happens with very complex polygons (eg. minkowski addition with a 700 corner and a 8 corner poly, my basic math operations are called millions of times)
If you have advice how to find bugs like this I'll gladly take it.
General advice on how to perfectly account for float errors would be nice too.
PM me though, because this has nothing to do with this topic.
At 9/13/14 01:30 AM, Xombiehacker wrote: I don't read the code, I test the flash, and based on what happens I can tell what code needs to be altered simply by induction and deduction.
Like I said, any decent coder should not need the compiler to tell you where the glitch lies
This might be true for small and simple solo projects, but if the project grows a bit you'll need any help finding bugs you can get.
I am currently working on a project with a friend, we've got ~600 classes so far, I believe I am a decent coder and there are still glitches I can't pinpoint. I spend more time reading than writing code.
Also your code is not 'wrong', but it isn't the best of examples because it doesn't scale very well.
At 9/12/14 09:33 AM, Barzona wrote: I've yet to get into this sort of thing. If you have any ideas, I'd love to hear them.
An easy way to do this would be to just calculate the position for B if it moved on a fixed orbit around A.
Afterwards just move B towards this position you could give B a speed with which it moves or just always move it 1/10 (0 < value < 1) of the distance each timestep.
At 9/12/14 04:35 AM, Hero101 wrote: Can you expand on this notion? It probably is overkill for this game as it is so simple and short, but I would like to know for future games. What do you mean by "buff" system?
What I mean with buff system is a way to attach 'buffs' to your units.
The most simple kind of buff is something that just does something every frame.
As example if your character is on fire you would attach an 'onFire' buff to your character.
This buff would then deal damage over time to your character until it expires or is otherwise removed.
It becomes more complex if the buffs modify the player behaviour, because the character should not check
'if i have buff reverseControl do x instead of y'
(implementing new buffs would require changing the character class each time a new buff is coded),
instead the buff should be like
'when my character does x do y instead'.
This way you don't have to edit your character each time new buffs are introduced and creating new buffs will be relatively easy.
An easy way to implement buffs that just do something to your characters would be to have an array containing buffs for each of your characters and updating them each time your character is updated.
At 9/12/14 03:39 AM, Hero101 wrote: My current idea is to use booleans inside the key handlers that will check which level it is.
Imagine what your code would look like if you had 100 different levels and you put all possibilities inside the key handler...
Stuff that differs between levels (like gravity, friction etc) should be defined in the levels.
Include the environment variables when deciding what exactly your character does.
If you have multiple entities which should behave differently in your level (eg reversed controls for your character but not for monsters) you might want to consider to introduce a 'buff' system, implementing a nice&clean buff system can be complex though and might be overkill, so I think it's okay to use booleans like 'controlsReverted' in your character as long as you don't use too many behaviour modifiers.
http://docs.unity3d.com/Manual/CreatingAndUsingScripts.html
Ok, it's UnityScript, not JavaScript.
At 9/10/14 05:46 PM, Barzona wrote: It has it's own coding language, right?
I know very little about Unity, apart from that it supports C#, JavaScript and Boo.
At 9/10/14 01:53 PM, MintPaw wrote: Could you do something like case "y" || "yes" || "yup": ?
I don't think so.
At 9/10/14 11:42 AM, MintPaw wrote: In AS3 we have the access modifier "internal" to do this.
Good to know, thanks.
Just wanted to mention that switch case can also do stuff like this:
switch(answer)
{
case "y":
case "yes":
case "ok":
trace("agreed!");
break;
case "n":
case "no":
case "cancel":
trace("disagreed!");
break;
default:
trace("???");
break;
}
This is analog to:
if(answer == "y" || answer == "yes" || answer == "ok")
{
trace("agreed!");
}
else if(answer == "n" || answer == "no" || answer == "cancel")
{
trace("disagreed!");
}
else trace("???");
At 9/10/14 03:44 AM, Xombiehacker wrote: this.g1 = Math.sin((rightCalf.rotation+90)*(Math.PI/180))*89.25+rightCalf.y;
this.g2 = Math.sin((leftCalf.rotation+90)*(Math.PI/180))*89.25+leftCalf.y;
I have no idea what you're calculating here, but if the character is stuck in the ground it's most likely not what you expected or your characters y coordinate is wrong.
Trace the results and check if they are what you expected.
At 9/10/14 03:44 AM, Xombiehacker wrote: if(this.g1 > this.g2){
this.ground = this.g1;
}
if(this.g1 < this.g2){
this.ground = this.g2;
}
You might want to replace this with:
ground = Math.max(g1, g2);
As it is shorter and handles the case 'g1 == g2'.
Math.max returns the maximum value of it's arguments.
Also do you know that you can chain 'else if' and 'else' after ifs?
At 9/10/14 03:17 AM, Hero101 wrote: What is the benefit of giving your packages names?
Packages are often used like folders to organize your files.
Also, if you used 2 libraries, lets say a graphics and physics lib, which both used a Point class, you would need the package name to uniquely identify which point you want to use.
You could than just write
graphicsPackage.Point
or
physicsPackage.Point
to differentiate between them.
I don't know about as3 packages, but in java variables/functions without access modifiers (private, protected, public) are only visible inside the package they are declared in.
At 9/6/14 07:23 PM, AnteHero wrote: My first thought was to check only the tiles which the character is overlapping with and then reverse the last movement if there is a collision. Unless you can think of a better way?
Checking only the tiles your character overlaps is correct.
You would be glued to walls if you jumped against them because you'd also undo the y part of your characters movement.
If you split it into 2 steps and move your character in x/y direction first, undo if it collides and repeat for the other axis.
This approach may have some unwanted artifacts though.
Another way would be, like MintPaw sugessted, to move your character and push it out of the tiles afterwards.
This generally works if your character moves small distances per frame and I recommend this.
If you also have fast moving objects you will have to calculate when exactly your object intersects a tile and only move the object up to this point.
If you need help on how to calculate tany of this, just ask.
At 9/5/14 09:34 PM, Hero101 wrote: Why are the objects bounds null in the code above? Is that due to what was "Return"-ed in checking for a collision?
The getBounds function returns the bounding rectangle int the coordinate space of the object you pass as argument.
Because I'm lazy I just passed null as argument and assume that it returns in relation to their parent.
You could pass them their parent instead or any other displayObject, its just important that the bounding boxes are in the same coordinate space.
At 9/5/14 09:19 PM, Hero101 wrote: What I don't understand is where exactly it is returning those values (true/false) in the rest of the code and what to do with it. Is it returning it to some other function or variable else where in the code?
Sorry to sound so amateur-ish...
After defining this function you can do your coolision tests by calling it.
Like this for example:
if (aabbIntersection(objectA.getBounds(null), objectB.getBounds(null)))
{
//handle object intersection here
}
This code would be placed in your collision loop
At 9/5/14 08:42 PM, Hero101 wrote: I looked into getBounds. I will have to play around with it and give it a go. Probably won't see the whole picture of how it all works until I try it out myself. If you're bored you could always type up some pseudo code as a visual aid for me... Appreciate your help :)
I made 3 versions, they all do the same, hope I didn't include typos, ask if you have questions.
function aabbIntersection(a:Rectangle, b:Rectangle)
{
//start with x-axis
if (a.right <= b.left) return false;
//rightmost point of rectangle a is still to the
//left of the leftmost point from rectangle b
//the rectangles cannot intersect, return false
if (b.right <= a.left) return false;
//same as above, just with the rectangles switched
//analog for y
if (a.bottom <= b.top) return false;
if (b.bottom <= a.top) return false;
//the rectangles overlap on the x axis and
//on the y axis => they overlap
return true;
}
function aabbIntersection_version2(a:Rectangle, b:Rectangle)
{
if (a.right > b.left && b.right > a.left)
{
//overlap on x axis
if (a.bottom > b.top && b.bottom > a.top)
{
//overlap on y axis
return true;
}
}
return false;
}
function aabbIntersection_version3(a:Rectangle, b:Rectangle)
{
return a.right > b.left && b.right > a.left && a.bottom > b.top && b.bottom > a.top;
}
At 9/5/14 08:04 PM, Hero101 wrote: That had to be the most simple explanation out there. How do you define the corners of an object though through code (like in the video Ax, Ay, AX, AY)? Are they just Points? If so, is there another way than me physically defining the points myself?
You should define this points relative to your objects coordinates for better quality collision, but for starters you can just use the movieclips/sprites bounding boxes.
You can use the getBounds(coords) method of your objects to get their bounding boxes, use the same object as coordinate parameter.
You now have 2 rectangles, theese already have getter for the upper left and the lower right point, use them.
At 9/5/14 07:20 PM, Hero101 wrote: Isn't that what aabb collision is? Every source I find on rectangle collision seems, well, not so simple so I have a hard time understanding it. Maybe I should try to find a youtube video that could break it down for me...
Yup, thats aabb collision, it's really simple once you understood how it works, fast, and already very useful.
My platformer uses only aabb and I didn't have to bother to use quadtrees or similar because the levels are relatively small.
I can try to explain how to detect and solve intersections using aabb's if you want.
At 9/5/14 06:47 PM, Hero101 wrote: For example, lets say all items collected just increased your score and there are 5 different types of items. Now say for the first couple of levels there is only one type of item - I can't have all the other collectible items in that same array since they won't be present in the game at the time and thus it would throw an Error 1009 at me correct? Or am I wrong?
There are multiple ways to do this. (like always)
You could create a Level class where each instance has it's own array with elements in this specific level.
Or share the same array between all levels and when a level starts the array is cleared and then repopulated by the level.
Give your items an isActive property and ignore all items which are not active when looping.
(I would not do it this way though)
I usually have a builder class where the level information is stored from which new level instances are created.
The builder will populate the levels array(s).
At 9/4/14 01:10 AM, Hero101 wrote: Is it:
if (brick = null)
OR
if (brick == null)
OR
if (brick === null)
if(brick = null)
This is not what you want, '=' is always an assignment, this code is analog to:
brick = null;
if(brick)
brick will always be null and thus the if will fail.
'==' schould almost always be what you're looking for.
The difference to '===' is explained here:
http://stackoverflow.com/questions/80646/how-do-the-php-equality-double-equals-and-identity-triple-equals-comp
At 9/3/14 11:55 PM, Hero101 wrote: So how should I go about achieving what I am trying to do? What would you suggest?
One possible solution would be:
Keep an array of objects to hitTestAgainst and add the brick to it,
loop through the array each frame and test its elements against the key.
Remove objects from the array when removing them.
If you really only want to use the brick you could just set its reference to null and check for null each time before collision checking.

