[FONT=monospace][COLOR=#000000]Hello, I’m having trouble doing movement with cos/sin. Everything works, except the distance moved varies with different angles even though the speed is constant.
This is my code:
[/COLOR][/FONT]
var midline:Sprite = new Sprite();
midline.graphics.beginFill(0xff0000);
midline.graphics.drawRect(0,-1,stage.stageWidth,2);
var b1:Sprite = new Sprite();
b1.graphics.beginFill(0xff0000);
b1.graphics.drawCircle(0,0,5);
var b2:Sprite = new Sprite();
b2.graphics.beginFill(0xff0000);
b2.graphics.drawCircle(0,0,5);
var speed:Number = 1;
var origin:Point = new Point(0,stage.stageHeight/2);
var target1:Point = new Point(origin.x + 10,origin.y -5);
var target2:Point = new Point(origin.x + 10,origin.y +5);
var rad1:Number = -Math.atan2(origin.x-target1.x, origin.y-target1.y);
var rad2:Number = -Math.atan2(origin.x-target2.x, origin.y-target2.y);
var co:int = 0;
b1.x = b2.x = origin.x;
b1.y = b2.y = midline.y = origin.y;
addChild(b1);
addChild(b2);
addChild(midline);
while (b1.x < stage.stageWidth && b1.y > 0 && b2.x < stage.stageWidth && b2.y < stage.stageHeight) {
co++;
if (co > 10000) {
trace("loop")
break;
}
b1.x += speed * Math.sin(rad1);
b1.y += speed * -Math.cos(rad1);
b2.x += speed * Math.sin(rad2);
b2.y += speed * -Math.cos(rad2);
}
trace("top moved " + (origin.y-b1.y) + ", bottom moved " + (b2.y - origin.y));
[FONT=monospace][COLOR=#000000]
I’m not that familiar with trigonometry to begin with, but I have managed to use it before. I don’t know if this problem has occured before though. As far as I can tell the two balls should move the same distance, but they don’t. The top ball moves further than the bottom, and I can’t figure out why. If I set the speed very low, the bottom ball won’t even move downwards at all. If I set the angles to straight up and straight down, the balls both reach the end of the screen.
I would appreciate if someone could have a look.
[SIZE=3][COLOR=Red] edit:[/COLOR][/SIZE]
I have done some further testing, and when doing
[/COLOR][/FONT]
trace(Math.abs(speed * -Math.cos(rad1)))
trace(Math.abs(speed * -Math.cos(rad2)))
[FONT=monospace][COLOR=#000000]
I can see that they differ on the fifteenth or sixteenth decimal, but that doesn’t seem to be the reason for my problem. Instead it seems to be the way flash assigns and stores the values for an objects x and y properties. I can see the reason why setting an x value to the sixteenth decimal can be overkill, and apparently so does flash as it often rounds off such numbers. That’s why my objects didn’t end up where I expected them to. What I did to solve this was to create new Number variables to hold the x and y positions of my objects, and do the calculations on them, and only set the x and y properties to the calculated variables:
[/COLOR][/FONT]
var grid:Sprite = new Sprite();
var gridsize:int = 25;
for (var i:int = 0; i <= stage.stageWidth/gridsize; i++) {
grid.graphics.beginFill(0xffdddd);
grid.graphics.drawRect(i*gridsize,0,1,stage.stageHeight);
grid.graphics.endFill();
if (i*gridsize <= stage.stageHeight) {
grid.graphics.beginFill(0xffdddd);
grid.graphics.drawRect(0,i*gridsize,stage.stageWidth,1);
grid.graphics.endFill();
}
}
grid.graphics.beginFill(0xff0000);
grid.graphics.drawRect(0,stage.stageHeight/2-1,stage.stageWidth,2);
grid.graphics.endFill();
grid.graphics.beginFill(0xff0000);
grid.graphics.drawRect(stage.stageWidth/2-1,0,2,stage.stageHeight);
grid.graphics.endFill();
var b1:Sprite = new Sprite();
b1.graphics.beginFill(0xff0000);
b1.graphics.drawCircle(0,0,5);
var b2:Sprite = new Sprite();
b2.graphics.beginFill(0x00ff00);
b2.graphics.drawCircle(0,0,5);
var speed:Number = 1;
var origin:Point = new Point(0,stage.stageHeight/2);
var target1:Point = new Point(origin.x + 10,origin.y - 2);
var target2:Point = new Point(origin.x + 10,origin.y + 2);
var rad1:Number = -Math.atan2(origin.x-target1.x, origin.y-target1.y);
var rad2:Number = -Math.atan2(origin.x-target2.x, origin.y-target2.y);
//rad1 = (rad1 / (Math.PI / 180)+90) * (Math.PI / 180); //convert to degrees, add 90 degrees, then back to radians
//rad2 = (rad2 / (Math.PI / 180)+90) * (Math.PI / 180);
var co:int = 0;
b1.x = b2.x = stage.stageWidth/2;
b1.y = b2.y = origin.y;
addChild(grid);
addChild(b1);
addChild(b2);
var xPos1:Number = b1.x;
var yPos1:Number = b1.y;
var xPos2:Number = b2.x;
var yPos2:Number = b2.y;
while (xPos1+(b1.width/2) < stage.stageWidth && xPos1 > (b1.width/2) && yPos1 > (b1.height/2) && yPos1 + (b1.height/2) < stage.stageHeight && xPos2+(b2.width/2) < stage.stageWidth && xPos2 > (b2.width/2) && yPos2 + (b2.height/2) < stage.stageHeight && yPos2 > (b2.height/2)) {
xPos2 += speed * Math.sin(rad2);
yPos2 += speed * -Math.cos(rad2);
xPos1 += speed * Math.sin(rad1);
yPos1 += speed * -Math.cos(rad1);
b1.x = xPos1;
b1.y = yPos1;
b2.x = xPos2;
b2.y = yPos2;
co++;
if (co > 10000) {
trace("loop")
break;
}
}
graphics.beginFill(0xffaaff);
graphics.drawRect(Math.floor(xPos1/gridsize)*gridsize,Math.floor(yPos1/gridsize)*gridsize,gridsize,gridsize);
graphics.drawRect(Math.floor(xPos2/gridsize)*gridsize,Math.floor(yPos2/gridsize)*gridsize,gridsize,gridsize);
/*trace(Math.abs(speed * -Math.cos(rad1)))
trace(Math.abs(speed * -Math.cos(rad2))) //(reveals that one decimal is different from the one with rad1)
trace(Math.abs(speed * Math.sin(rad1)))
trace(Math.abs(speed * Math.sin(rad2)))*/
trace("top moved " + Math.abs(origin.y - yPos1) + ", bottom moved " + Math.abs(yPos2 - origin.y));
trace(new Point(xPos1,yPos1))
trace(new Point(xPos2,yPos2))
trace(co)
[FONT=monospace][COLOR=#000000]
I’m going to make these changes in my main project, so I guess this problem is now solved, just wanted to post this in case someone else runs into a similar problem.
This doesn’t seem to be a problem unless the speed is quite low. I use a speed of 15 for the objects in my main project and they ended up being like 1 pixel off and that wouldn’t really matter, but this really bugged me, and I feel better knowing my code works the way I want it to.
This might be common knowledge, and I have probably noticed how flash assigns x and y values before, now that I think about it, but I’ll try to remember this in the future.
[/COLOR][/FONT]