Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / An embarrasing plea for help...

Author
Message
Santman
13
Years of Service
User Offline
Joined: 15th Sep 2011
Location: Inverness
Posted: 25th Jan 2015 00:14
Ok, so I can code to a fair degree....but i'm utterly hopeless at maths.

I'm doing a simple project that has a bunch of little soldiers running around a map, and it;s working fine, but I'm having issues with angles.

Issue 1 - the little guys need to rotate to face the way they are moving. They can move in only the 8 basic directions, and I can manually set the angle to be correct, however I want to have them actually rotate to face the correct way, not just suddenly be facing that way. But here's the problem; if the sprite is facing up, it's rotation is 0. If it then moves right, it would need to rotate 90 degrees. No problem. But if it turns left, it needs to rotate -90 degrees. Problem. When I read the angle the sprite is currently at, and I know the angle it should be facing, how do I work out which direction to rotate in is quickest? I.e. for turning left, how do I calculate that -90 degree angle?

Issue 2 - when they start shooting, I need bullet sprites to move in non linear ways, to allow for poor accuracy over distance. I considered using floats for the positions so that rounding would naturally have them move, however how do I calculate the angle? What I mean is, if a sprite is on row 1, and he shoots a sprite on row 3 that is 5 spaces away - pie can give me the distance to him, but how do I get that angle so that I can set the floats correctly and make the bullet navigate towards the unit which was shot?

These feel like REALLY basic questions I am asking, but I really am so very bad at maths any help at all would be appreciated.
paulrobson
10
Years of Service
User Offline
Joined: 22nd Nov 2014
Location: Norfolk, England
Posted: 25th Jan 2015 08:16
Have a look at atan2 which calculates the angle for an x/y offset.
easter bunny
12
Years of Service
User Offline
Joined: 20th Nov 2012
Playing: Dota 2
Posted: 26th Jan 2015 22:50
Quote: "Issue 1 - the little guys need to rotate to face the way they are moving. They can move in only the 8 basic directions, and I can manually set the angle to be correct, however I want to have them actually rotate to face the correct way, not just suddenly be facing that way. But here's the problem; if the sprite is facing up, it's rotation is 0. If it then moves right, it would need to rotate 90 degrees. No problem. But if it turns left, it needs to rotate -90 degrees. Problem. When I read the angle the sprite is currently at, and I know the angle it should be facing, how do I work out which direction to rotate in is quickest? I.e. for turning left, how do I calculate that -90 degree angle?"


Not sure I completely understand the question... Is this the sort of code you're looking for?



Quote: "Issue 2 - when they start shooting, I need bullet sprites to move in non linear ways, to allow for poor accuracy over distance. I considered using floats for the positions so that rounding would naturally have them move, however how do I calculate the angle? What I mean is, if a sprite is on row 1, and he shoots a sprite on row 3 that is 5 spaces away - pie can give me the distance to him, but how do I get that angle so that I can set the floats correctly and make the bullet navigate towards the unit which was shot?
"

Okay, this is actually much easier if you don't use angles at all!
Just do it like this:



the problem is if you want to rotate the bullet in the right direction, in that case you need to use Trig, if you want help with it, I can give you a few tips, but I'm not that great at it either


Audacia Games - Latest WIP - AUTOMAYTE 2.1, AppGameKit one click deploy to Android
"When you've finished 90% of your game, you only have 90% left"
Van B
Moderator
22
Years of Service
User Offline
Joined: 8th Oct 2002
Location: Sunnyvale
Posted: 27th Jan 2015 13:07 Edited at: 27th Jan 2015 13:08
You probably store the angle of the sprite movement, and you'd be able to work out the desired angle, so some while...endwhile angle difference checking would work out...

Assuming moveang# is the current movement angle, and faceang# is the desired 8-way angle...

angdif#=faceang#-moveang#
while angdif#<-180.0
angdif#=angdif#+360.0
endwhile
while angdif#>180.0
angdif#=angdif#-360.0
endwhile

What this does is works out the difference between the angles (-180 to 180), so if you set a limit of 10 for example, you can just add that to the sprite movement angle each frame...

if angdif#>10.0 then angdif#=10.0
if angdif#<-10.0 then angdif#=10.0

If the angle difference is between -10 and 10, it will be changed to the same angle as the facing angle automatically.
This is similar to the DBPro curveangle function.

I am the one who knocks...
Santman
13
Years of Service
User Offline
Joined: 15th Sep 2011
Location: Inverness
Posted: 29th Jan 2015 23:12
Hey guys.

Cheers for the responses, I spent a good bit of time going over them....but when I looked at the atan2 help it mentioned vectors and wanted two coords and I turned off! Lol.

That being said, I managed to do what I wanted to do using Sin. I probably wasn't clear in my original post, however here's what I was referring to with the rotations - I needed to know which way each unit should turn when it needed to change direction.

http://youtu.be/tZ9APNkdPn8

Cheers for the help though - next issue is bullets.
paulrobson
10
Years of Service
User Offline
Joined: 22nd Nov 2014
Location: Norfolk, England
Posted: 30th Jan 2015 09:50
All atan2 does - if you give it an offset it converts it to an angle.

So if soldierA is at 22,11 and soldierB is at 34,15, then the offset from soldierA to soldierB is 12,4, e.g. he (she?) is 12 units across and 4 up. (note at this point that trignometry y axis goes up and screen y axis goes down - a sneaky problem)

the value atan2(4,12) (y comes first) gives you the angle that soldier A needs to point to seem to be looking at soldier B. It's about 18 degrees (on this system, 0 degrees is due east/right and it numbers anticlockwise)

Login to post a reply

Server time is: 2024-11-25 17:37:13
Your offset time is: 2024-11-25 17:37:13