A Flash Developer Resource Site

Results 1 to 20 of 20

Thread: [RESOLVED] setting up max limit rotation

  1. #1
    my x booty it is that BIG
    Join Date
    Jun 2004
    Location
    New York
    Posts
    696

    resolved [RESOLVED] setting up max limit rotation

    okay so i got the guy to aim towards the mouse with this code. however i cant seen to find a way to set a max angle of 90 degrees to stop the guy from fully rotating when the mouse is beyong that point.
    initially i got this which makes him turn with the keys and it works fine to stop him from rotating
    Code:
    maxAng=90;
    function rotatePlayer(){
    	if (Key.isDown(Key.LEFT) && hero._rotation> -maxAng) {
    		hero._rotation -= 10;
    	}
    	if (Key.isDown(Key.RIGHT) && hero._rotation < maxAng) {
    		hero.gun._rotation += 10;
    	}
    
    	}
    }
    and this is the one am trying to get which is with the mouse and it doesnt work
    Code:
    var radians2:Number = 180/Math.PI;
    var maxAng:Number = 89;
    //Rotate Player
    function rotatePlayer(){
    	
    	playerX = player_mc._x;
    	playerY = player_mc._y;
    	
    	//calculate player_mc rotation, based on player position & mouse position 
    	rotationDirection =Math.round(180 - (Math.atan2(_root._xmouse - playerX, _root._ymouse - playerY)*180/Math.PI));
    	
    	player_mc._rotation = rotationDirection;
    	trace(player_mc._rotation);
    	if (player_mc._rotation >= -maxAng) {
    	player_mc._rotation -= 5;
    	}if (player_mc._rotation <= maxAng) {
    	player_mc._rotation += 5;
    	updateAfterEvent();
    	}
    }
    edit:added the fla might help a little more. sorry is in flash cs3.
    Attached Files Attached Files
    Last edited by mixedtrigeno; 08-07-2009 at 04:11 PM.
    Project||[GAME]-on hold for now
    ------------------
    [Hero]-80%
    [Enemies]-1%
    [Weapons]-90%
    [Items]-0%
    [Levels]-10%

  2. #2
    Senior Member UnknownGuy's Avatar
    Join Date
    Jul 2003
    Location
    Canada
    Posts
    1,361
    What do you mean it doesn't work? What kind of behaviour do you see?

    Something to remember is I think the results will range from -180 to 180 degrees, which can be confusing sometimes with min's and max's.

  3. #3
    my x booty it is that BIG
    Join Date
    Jun 2004
    Location
    New York
    Posts
    696
    the part thats not working is the one trying to stop the guy from making a full circle i want him to stop at 90 degrees. on the first code i got up there it works fine but thats with the keys. so this part of the code is not working
    and yah am aware of the -180 and 180 thats why i got the -maxAng there
    Code:
    	if (player_mc._rotation >= -maxAng) {
    	player_mc._rotation -= 5;
    	}if (player_mc._rotation <= maxAng) {
    	player_mc._rotation += 5;
    Project||[GAME]-on hold for now
    ------------------
    [Hero]-80%
    [Enemies]-1%
    [Weapons]-90%
    [Items]-0%
    [Levels]-10%

  4. #4
    President PhobiK's Avatar
    Join Date
    Jan 2005
    Location
    Guatemala
    Posts
    918
    why not if (player_mc._rotation > maxAng) {
    player_mc._rotation = maxAng;
    } else if (player_mc._rotation < -maxAng) {
    player_mc._rotation = -maxAng;
    }
    This is MC. His _parents sent him to stop() by the super market to buy some _root beer if he wanted to gotoAndPlay() with his friends at the park later.

    This is my Blog!... The gaming Process
    Please check out my site: Giddel Creations

  5. #5
    my x booty it is that BIG
    Join Date
    Jun 2004
    Location
    New York
    Posts
    696
    hmm...that worked...so i had the values of my maxAng backwards?!
    Project||[GAME]-on hold for now
    ------------------
    [Hero]-80%
    [Enemies]-1%
    [Weapons]-90%
    [Items]-0%
    [Levels]-10%

  6. #6
    President PhobiK's Avatar
    Join Date
    Jan 2005
    Location
    Guatemala
    Posts
    918
    well, yes, I think. But also setting the value with an "=" instead of doing it the "+=" way is the most accurate. The other way if your angle is above your maxAng it will be decreasing by 5 every frame (or every certain time), which can look a bit sloppy sometimes. Instead, now you're saying: if the angle is above maxAng, it will be equal to maxAng, so no chance to go beyond that.
    This is MC. His _parents sent him to stop() by the super market to buy some _root beer if he wanted to gotoAndPlay() with his friends at the park later.

    This is my Blog!... The gaming Process
    Please check out my site: Giddel Creations

  7. #7
    my x booty it is that BIG
    Join Date
    Jun 2004
    Location
    New York
    Posts
    696
    okay thanks didnt catch that one
    now one more dilemma this is what i got so far.
    i cant figure out at all how to adjust the rotating speed to give it a more natural feel to it. in other words i lets say you are on -maxAng and you move the mouse all of a sudden to maxAng i dont want him to get there at the same speed the mouse did i want him to rotate slower(rSpeed) hope am making sence.
    Code:
    var radians2:Number = 180/Math.PI;
    var maxAng:Number = 89;
    var rSpeed:Number =10;
    var playerX = player_mc._x;
    var playerY = player_mc._y
    //Rotate Player
    function rotatePlayer(){
    	
    	//calculate player_mc rotation, based on player position & mouse position 
    	player_mc._rotation =Math.round(180 - (Math.atan2(_root._xmouse - playerX, _root._ymouse - playerY)*180/Math.PI));
    	
    	//player_mc._rotation = rotationDirection;
    	if (player_mc._rotation > maxAng) {
    	player_mc._rotation = maxAng;
    } else if (player_mc._rotation < -maxAng) {
    	player_mc._rotation = -maxAng;
    	updateAfterEvent();
    	}
    }
    Project||[GAME]-on hold for now
    ------------------
    [Hero]-80%
    [Enemies]-1%
    [Weapons]-90%
    [Items]-0%
    [Levels]-10%

  8. #8
    Funkalicious TOdorus's Avatar
    Join Date
    Nov 2006
    Location
    Nijmegen, Netherlands
    Posts
    697
    This is actually the same problem (mathematicly) as in this thread, so you might get some answers there.

  9. #9
    my x booty it is that BIG
    Join Date
    Jun 2004
    Location
    New York
    Posts
    696
    aaaahhhh my head hurts
    and to think i use to be a math tutor in highschool
    i couldnt see the answer to my problem in that thread
    Project||[GAME]-on hold for now
    ------------------
    [Hero]-80%
    [Enemies]-1%
    [Weapons]-90%
    [Items]-0%
    [Levels]-10%

  10. #10
    President PhobiK's Avatar
    Join Date
    Jan 2005
    Location
    Guatemala
    Posts
    918
    This is a code I used to do what you are asking. It is in as1 though (cus I made it long ago), so you'll have to declare the variables. Also, the code is applied to a MC so adapt it to what you want. I just inserted your angle detecting code in there:
    Code:
    onClipEvent (enterFrame) {
    	//calculate player_mc rotation, based on player position & mouse position 
    	rotation =Math.round(180 - (Math.atan2(_root._xmouse - playerX, _root._ymouse - playerY)*180/Math.PI));
    
    	rotminn = rotation-360;
    	rotmaxx = rotation+360;
    	_rotminn = _rotation-360;
    	_rotmaxx = _rotation+360;
    	sum1 = Math.abs(rotation-_rotation);
    	sum2 = Math.abs(rotminn-_rotation);
    	sum3 = Math.abs(rotmaxx-_rotation);
    	if (sum1< sum2 && sum1<sum3) {
    		if (rotation<_rotation-vel*2) {
    			_rotation-=vel;
    		} else if (rotation>_rotation+vel*2) {
    			_rotation+=vel;
    		}
    	} else if (sum2<sum1 && sum2< sum3) {
    		if (rotminn<_rotation-vel*2) {
    			_rotation-=vel;
    		} else if (rotminn>_rotation+vel*2) {
    			_rotation+=vel;
    		}
    	} else if (sum3<sum1 && sum3<sum2) {
    		if (rotmaxx<_rotation-vel*2) {
    			_rotation-=vel;
    		} else if (rotmaxx>_rotation+vel*2) {
    			_rotation+=vel;
    		}
    	}
    }
    onClipEvent (load) {
    	vel =3;
    }
    Now, if you want a more slight movement, rather than a constant speed, this is another way to do so:
    Code:
    onClipEvent (enterFrame) {
    	//calculate player_mc rotation, based on player position & mouse position 
    	rotation =Math.round(180 - (Math.atan2(_root._xmouse - playerX, _root._ymouse - playerY)*180/Math.PI));
    	rotminn = rotation-360;
    	rotmaxx = rotation+360;
    	_rotminn = _rotation-360;
    	_rotmaxx = _rotation+360;
    	sum1 = Math.abs(rotation-_rotation);
    	sum2 = Math.abs(rotminn-_rotation);
    	sum3 = Math.abs(rotmaxx-_rotation);
    	if (sum1< sum2 && sum1<sum3) {
    		_rotation+=(rotation-_rotation)/7;
    	} else if (sum2<sum1 && sum2< sum3) {
    		_rotation+=(rotminn-_rotation)/7;
    	} else if (sum3<sum1 && sum3<sum2) {
    		_rotation+=(rotmaxx-_rotation)/7;
    	}
    }
    This is MC. His _parents sent him to stop() by the super market to buy some _root beer if he wanted to gotoAndPlay() with his friends at the park later.

    This is my Blog!... The gaming Process
    Please check out my site: Giddel Creations

  11. #11
    my x booty it is that BIG
    Join Date
    Jun 2004
    Location
    New York
    Posts
    696
    yah neither of the codes work they behave weird when i used your code
    Project||[GAME]-on hold for now
    ------------------
    [Hero]-80%
    [Enemies]-1%
    [Weapons]-90%
    [Items]-0%
    [Levels]-10%

  12. #12
    my x booty it is that BIG
    Join Date
    Jun 2004
    Location
    New York
    Posts
    696
    bump!cuse i still cant find the answer anywhere
    Project||[GAME]-on hold for now
    ------------------
    [Hero]-80%
    [Enemies]-1%
    [Weapons]-90%
    [Items]-0%
    [Levels]-10%

  13. #13
    President PhobiK's Avatar
    Join Date
    Jan 2005
    Location
    Guatemala
    Posts
    918
    ok, so they didn't work. Are you shure you adapted the code to yours? It works for me. Ok but just to try to give an easier way.
    Try something like:
    Code:
    rotSpeed = 5;
    onEnterFrame = function () {
    rotationDirection =Math.round(180 - (Math.atan2(_root._xmouse - playerX, _root._ymouse - playerY)*180/Math.PI));
    if ((rotationDirection+rotSpeed) < player_mc._rotation) {
       _player_mc._rotation -= rotSpeed;
    } else if (rotationDirection-rotSpeed) > player_mc._rotation) {
       _player_mc._rotation += rotSpeed;
    }
    }
    That should do. Or if you want the soft movement, even easier:
    Code:
    onEnterFrame = function () {
    rotationDirection =Math.round(180 - (Math.atan2(_root._xmouse - playerX, _root._ymouse - playerY)*180/Math.PI));
    player_mc._rotation += (rotationDirection - _player_mc._rotation) * 0.3;
    }
    Notice that when working with rotation it can be a bit tricky because when it reaches 360 it should go back to 0, but I think Flash does it in a -180 to 180 way, so this way should work if you stay into that range.
    This is MC. His _parents sent him to stop() by the super market to buy some _root beer if he wanted to gotoAndPlay() with his friends at the park later.

    This is my Blog!... The gaming Process
    Please check out my site: Giddel Creations

  14. #14
    my x booty it is that BIG
    Join Date
    Jun 2004
    Location
    New York
    Posts
    696
    okay this the code i ended up with after implenting one of your methods
    Code:
    var radians2:Number = 180/Math.PI;
    var maxAng:Number = 89;
    //this is the rotating speed that am trying to add
    //var rSpeed:Number =10;
    var playerX = player_mc._x;
    var playerY = player_mc._y
    
    //Rotate Player
    function rotatePlayer(){
    	rotSpeed = 5;
    	//calculate player_mc rotation, based on player position & mouse position 
    	rotDir=Math.round(180 - (Math.atan2(_root._xmouse - playerX, _root._ymouse - playerY)*180/Math.PI));
    	player_mc._rotation=rotDir;
    	//player_mc cant rotate further 89 degrees when rotating left and/or right
    	if((player_mc._rotation=rotDir+rotSpeed)<(player._mc._rotation=rotDir)){
    	player_mc._rotation-=rotSpeed;
    	}else if((player_mc._rotation=rotDir-rotSpeed)>(player._mc._rotation=rotDir)){
    	player_mc._rotation+=rotSpeed;
    	}if (player_mc._rotation > maxAng) {
    	player_mc._rotation = maxAng;
    	}else if (player_mc._rotation < -maxAng) {
    	player_mc._rotation = -maxAng;
    	updateAfterEvent();
    	}
    }
    
    
    _root.onEnterFrame = function() {
    	rotatePlayer();
    	//function_AimAng();
    }
    i cant really explain what rotSpeed is doing, just test out the the fla ull see what i mean. this is a flash mx version
    Attached Files Attached Files
    Project||[GAME]-on hold for now
    ------------------
    [Hero]-80%
    [Enemies]-1%
    [Weapons]-90%
    [Items]-0%
    [Levels]-10%

  15. #15
    President PhobiK's Avatar
    Join Date
    Jan 2005
    Location
    Guatemala
    Posts
    918
    Ok, here it is working. I've tried it already.
    Code:
    function rotatePlayer(){
    	rotSpeed = 5;
    	//calculate player_mc rotation, based on player position & mouse position 
    	rotDir=Math.round(180 - (Math.atan2(_root._xmouse - playerX, _root._ymouse - playerY)*180/Math.PI));
    	//player_mc._rotation=rotDir;
    	//player_mc cant rotate further 89 degrees when rotating left and/or right
    	if((player_mc._rotation>rotDir+rotSpeed)){
    	player_mc._rotation-=rotSpeed;
    	}else if((player_mc._rotation<rotDir-rotSpeed)){
    	player_mc._rotation+=rotSpeed;
    	}if (player_mc._rotation > maxAng) {
    	player_mc._rotation = maxAng;
    	}else if (player_mc._rotation < -maxAng) {
    	player_mc._rotation = -maxAng;
    	updateAfterEvent();
    	}
    }
    First this line should not go there: player_mc._rotation=rotDir. With that you automatically make it not work, because you are setting the actual rotation (_rotation) to the desired rotation (rotDir) so there's no movement to do anymore.
    Then in the if's you can't say something like this "(player_mc._rotation=rotDir+rotSpeed)" because, again you are setting the rotation to a specific value. Even when puttin it into an if, you cant place "=", you have to write "==" if you are checking a condition. In this case it's just < and > we are checking, no need of ==.
    I'll try to explain it an easy way. The mc starts with a rotation of 0. Let's suppose the mouse is at an angle of 25. Then it says: the mouse angle (rotDir) is higher than the actual rotation (player_mc._rotation), then it will add +5 (rotSpeed) each frame until it gets to 25. That is basically what it does. I want to explain it well so you can modify it the way you want to fit your needs, I think I haven't explained very well.
    So, you can change rotSpeed to any number you want, like 3 or other number depending on the speed you want. Also I noticed that the framerate is still at 12, that won't look so smooth so you'd better rise that a bit.
    This is MC. His _parents sent him to stop() by the super market to buy some _root beer if he wanted to gotoAndPlay() with his friends at the park later.

    This is my Blog!... The gaming Process
    Please check out my site: Giddel Creations

  16. #16
    my x booty it is that BIG
    Join Date
    Jun 2004
    Location
    New York
    Posts
    696
    OMFG finally!!!!
    and yah now i get understand the code
    i owe you a million dollars when i win the lotto lol
    and its only 12fps cuse i just made a quick fla for flash m the actual game is at 35 fps

    edit: just noticed a big bug though...if you place the mouse behind the player_mc out of range of the maxAng the player_mc starts to rotate like crazy and if you move back withing the maxAng range it will rotate to the right...

    oh and fyi i officially hate trigonometry after this lmao
    Last edited by mixedtrigeno; 08-11-2009 at 12:18 AM.
    Project||[GAME]-on hold for now
    ------------------
    [Hero]-80%
    [Enemies]-1%
    [Weapons]-90%
    [Items]-0%
    [Levels]-10%

  17. #17
    President PhobiK's Avatar
    Join Date
    Jan 2005
    Location
    Guatemala
    Posts
    918
    don't ask why... just paste this into your code:
    Code:
    var rotation:Number = 0;
    var rotminn:Number = 0;
    var rotmaxx:Number = 0;
    var _rotminn:Number = 0;
    var _rotmaxx:Number = 0;
    var sum1:Number = 0;
    var sum2:Number = 0;
    var sum3:Number = 0;
    var vel:Number = 4;
    
    function rotatePlayer() {
    	vel = 4;
    	yy = _root._ymouse - player_mc._y;
    	xx = _root._xmouse - player_mc._x
    	rotation = Math.atan2(yy, xx) * 180 / Math.PI;
    	rotminn = rotation-360;
    	rotmaxx = rotation+360;
    	_rotminn = player_mc._rotation-360;
    	_rotmaxx = player_mc._rotation+360;
    	
    	
    	
    	sum1 = Math.abs(rotation-player_mc._rotation);
    	sum2 = Math.abs(rotminn-player_mc._rotation);
    	sum3 = Math.abs(rotmaxx-player_mc._rotation);
    	
    	if (sum1< sum2 && sum1<sum3) {
    		
    		if (rotation<player_mc._rotation-vel*2) {
    			
    			player_mc._rotation-=vel
    		} else if (rotation>player_mc._rotation+vel*2) {
    			player_mc._rotation+=vel;
    		}
    	} else if (sum2<sum1 && sum2< sum3) {
    		if (rotminn<player_mc._rotation-vel*2) {
    			player_mc._rotation-=vel;
    		} else if (rotminn>player_mc._rotation+vel*2) {
    			player_mc._rotation+=vel;
    		}
    	} else if (sum3<sum1 && sum3<sum2) {
    		if (rotmaxx<player_mc._rotation-vel*2) {
    			player_mc._rotation-=vel;
    		} else if (rotmaxx>player_mc._rotation+vel*2) {
    			player_mc._rotation+=vel;
    		}
    	}
    	
    	if (player_mc._rotation < -90) {
    		player_mc._rotation = -90;
    	} else if (player_mc._rotation > 90) {
    		player_mc._rotation = 90;
    	} 
    }
    
    
    _root.onEnterFrame = function() {
    	rotatePlayer();
    	//function_AimAng();
    }
    I don't have a clue what was happening so I had to do it this way. Even doing it like this were weird things happening when I used the maxAng variable instead of putting the numbers directly into the if's. Don't know why. You'll just have to place your movieclip at rotation 0 by default, facing to the right and not like it is right now.
    This is MC. His _parents sent him to stop() by the super market to buy some _root beer if he wanted to gotoAndPlay() with his friends at the park later.

    This is my Blog!... The gaming Process
    Please check out my site: Giddel Creations

  18. #18
    my x booty it is that BIG
    Join Date
    Jun 2004
    Location
    New York
    Posts
    696
    well now is doing everything fine but one thing is not actually aiming at the mouse instead is aiming away from it
    http://www.swfme.com/view/1020542
    Project||[GAME]-on hold for now
    ------------------
    [Hero]-80%
    [Enemies]-1%
    [Weapons]-90%
    [Items]-0%
    [Levels]-10%

  19. #19
    President PhobiK's Avatar
    Join Date
    Jan 2005
    Location
    Guatemala
    Posts
    918
    You just have to make your mc be facing to the right from inside. I mean, the rectangle shape that is inside the mc should be facing right (0 degrees) instead of facing upwards.

    oh and fyi i officially hate trigonometry after this lmao
    lol that's ironic, things like this were the ones that made me like this trigonometric stuff :P
    This is MC. His _parents sent him to stop() by the super market to buy some _root beer if he wanted to gotoAndPlay() with his friends at the park later.

    This is my Blog!... The gaming Process
    Please check out my site: Giddel Creations

  20. #20
    my x booty it is that BIG
    Join Date
    Jun 2004
    Location
    New York
    Posts
    696
    got it thanks a million. i am so used to putting the registration point at the bottom
    Project||[GAME]-on hold for now
    ------------------
    [Hero]-80%
    [Enemies]-1%
    [Weapons]-90%
    [Items]-0%
    [Levels]-10%

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center