The Daily Click ::. Forums ::. Klik Coding Help ::. Fun with trig! Help me fix my 360 degrees problem.
 

Post Reply  Post Oekaki 
 

Posted By Message

Levirules



Registered
  27/09/2008
Points
  37
18th December, 2009 at 18:27:02 -

Hey all. I've been reading about trigonometry and calculating angles and distances and what not. This is one area of math that, despite being good at math throughout school, never made much sense to me. Regardless, I copied and pasted some code to have an enemy smoothly follow the player, and instead of getting the enemy to follow him with 360 degrees of motion, he only seems to have about 16 directions.

Basically, the code finds the angle between the enemy and the player, and moves the enemy's x with cos and y with sin. The three lines are like so:


//Value A("Enemy") is where I store the calculated angle between the enemy and player

Always
--Set Value A("Enemy") to: ATan2(Y("Enemy")-Y("Player"), X("Player")-X("Enemy"))
--Set X("Enemy") to: X("Enemy") + Cos(Value A("Enemy")*2)
--Set Y("Enemy") to: Y("Enemy") - Sin(Value A("Enemy")*2)



This code seems to work just fine when you use the Angle of the object, but I don't want to alter the rotation of the image of the object, so I calculate the angle and put it in Value A of the enemy. That's the ONLY DIFFERENCE I can see between my code and the bullet example. It's the same exact code; just replace "Value A("Enemy")" with "Angle("Enemy")."

Here is a link to the barebones example that I threw together for this post:
http://www.filefront.com/15162311/trig%20problem.mfa

ALSO - Why the hell does the < code > tag look so nice in preview mode but create such ridiculous space between each line when you post? Is there any way around that, or should I just keep using < quote > instead?

Edited by Levirules

 
n/a

UrbanMonk

BRING BACK MITCH

Registered
  07/07/2008
Points
  49566

Has Donated, Thank You!Little Pirate!ARGH SignKliktober Special Award TagPicture Me This Round 33 Winner!The Outlaw!VIP MemberHasslevania 2!I am an April FoolKitty
Picture Me This Round 32 Winner!Picture Me This Round 42 Winner!Picture Me This Round 44 Winner!Picture Me This Round 53 Winner!
18th December, 2009 at 20:54:35 -

You need to store the x and y position in alterable variables, and then always set the objects x and y position to those alterable variables.

Reason being is that x and y positions are stored as integers instead of floats in memory. (The decimal place is shaved off the number: 1.12312 becomes 1)

Therefore it gives the appearance of only having 16 or so directions because the movement calculations become inaccurate over time.


Here is the fixed version: http://www.jsoftgames.com/trigproblem.zip

Edited by UrbanMonk

 
n/a

Levirules



Registered
  27/09/2008
Points
  37
19th December, 2009 at 00:00:51 -

Thanks for your help. I tried this already and it didn't fix it, so obviously I must have done something else wrong. I hate that. It's like, I never would have figured this out, because I tried this already, but because I must have screwed something else up and not known it, I never would have tried that ever again. I guess this is what we have help forums. Thanks again dude

Edit. I bet this is what I did:
-set newXpos to X(player) + blah blah
-set X(player) to X(player) + newXpos
instead of
-set newXpos to newXpos + blah blah
-set X(player) to newXpos

I probably used a variable in a way that doesn't make a difference at all. Every time I have a problem like this, it ends up being something stupid about MMF, and it makes me want to say "screw it" and move on to a real language.

Edited by Levirules

 
n/a

nim



Registered
  17/05/2002
Points
  7233
19th December, 2009 at 03:59:05 -


Originally Posted by Levirules
Thanks for your help. I tried this already and it didn't fix it, so obviously I must have done something else wrong.



What UrbanMonk said will fix it. Did you notice that he also posted an example?

 
//

UrbanMonk

BRING BACK MITCH

Registered
  07/07/2008
Points
  49566

Has Donated, Thank You!Little Pirate!ARGH SignKliktober Special Award TagPicture Me This Round 33 Winner!The Outlaw!VIP MemberHasslevania 2!I am an April FoolKitty
Picture Me This Round 32 Winner!Picture Me This Round 42 Winner!Picture Me This Round 44 Winner!Picture Me This Round 53 Winner!
19th December, 2009 at 04:44:01 -


Originally Posted by nim

Originally Posted by Levirules
Thanks for your help. I tried this already and it didn't fix it, so obviously I must have done something else wrong.



What UrbanMonk said will fix it. Did you notice that he also posted an example?



It's ok nim! Read the rest of his post!

Thanks for the help though!

 
n/a

Levirules



Registered
  27/09/2008
Points
  37
19th December, 2009 at 16:58:21 -


Originally Posted by nim

Originally Posted by Levirules
Thanks for your help. I tried this already and it didn't fix it, so obviously I must have done something else wrong.



What UrbanMonk said will fix it. Did you notice that he also posted an example?



Yeah dude I got it. I meant that I tried this before, but incorrectly. Noticed this right away when I tried the example.

Thanks again guys!

 
n/a

nim



Registered
  17/05/2002
Points
  7233
19th December, 2009 at 17:15:25 -

Yeah sorry about that, I read your reply wrong. Glad you got it fixed

 
//
   

Post Reply



 



Advertisement

Worth A Click