It is currently Tue Nov 20, 2018 4:22 pm

 All times are UTC - 7 hours

 Page 1 of 1 [ 6 posts ]
 Print view Previous topic | Next topic
Author Message
 Post subject: Calculating angles and angular velocity?Posted: Fri Mar 16, 2018 2:43 pm

Joined: Fri Mar 16, 2018 1:52 pm
Posts: 38
Location: Finland
RECCA is probably the best example of all that I'm going to ask here. Normally most of this stuff is really easy, but taking how limited the NES, is you don't have access to trigonometric functions which makes things a lot more complicated.

1. Aimed shots. I know a lot of games do this but I never understood how they do this. As a side thing, I'd also like to know how to add angular offset (as shown by the enemies in the picture below by shooting a 3-way shots).
Attachment:

Summer Carnival '92 - Recca (Japan)-18.png [ 4.21 KiB | Viewed 1459 times ]

2. Homing shots. Somewhat common, but still a mystery in to me.
Attachment:

Summer Carnival '92 - Recca (Japan)-20.png [ 7.42 KiB | Viewed 1459 times ]

3. Angular velocity. RECCA also has an enemy that has other objects spinning around it, which I know for a fact that creating such movement requires changing the angle x amount of degrees every frame to make it seem like its turning.
*no pucture for this one :c

4. Bonus! I don't even know how to explain this. I'm talking about that 'hand' in the top left corner. It starts retracted(?) and starts to extend out and reach out towards the player. I can't even figure out how to program something like that with the mathematical capabilities modern languages have...
Attachment:

Summer Carnival '92 - Recca (Japan)-19.png [ 7.85 KiB | Viewed 1459 times ]

Top

 Post subject: Re: Calculating angles and angular velocity?Posted: Fri Mar 16, 2018 2:55 pm

Joined: Sun Sep 19, 2004 11:12 pm
Posts: 20789
Location: NE Indiana, USA (NTSC)
Have you looked in the source code of Thwaite to see how it does aiming? The getAngle subroutine in math.s does the equivalent of atan2 to find the approximate angle of a vector from (x1, y1) and (x2, y2). Then once you have the angle, you can use sine/cosine tables with one entry for each angle to set a bullet's velocity.

A homing shot just repeats the aiming process for a bullet every 6 or 12 frames or whatever.

Top

 Post subject: Re: Calculating angles and angular velocity?Posted: Fri Mar 16, 2018 4:50 pm

Joined: Mon May 27, 2013 9:40 am
Posts: 496
For aiming, I use a formula I'd like to remember where I got it from (sorry). It's FAR (and I mean FAR) from perfect, but it seems to get the job done, at least in the instances I've used it:

Code:
dx = DELTA (x0, x1);
dy = DELTA (y0, y1);
aux = MIN (dx, dy);
distance = dx + dy - (aux >> 1) - (aux >> 2) + (aux >> 4);

aux = (V * dx / distance); vx = ADD_SIGN (x1, x0, aux);
aux = (V * dy / distance); vy = ADD_SIGN (y1, y0, aux);

Macros are roughly as follows:

Code:
#define MIN(a,b)                   ((a)<(b)?(a):(b))
#define DELTA(a,b)                  ((a)<(b)?(b)-(a):(a)-(b))

All are signed fixed point variables.

_________________
http://www.mojontwins.com

Top

 Post subject: Re: Calculating angles and angular velocity?Posted: Fri Mar 16, 2018 8:11 pm

Joined: Sat Feb 12, 2005 9:43 pm
Posts: 10977
Location: Rio de Janeiro - Brazil
The basic idea is that the ratio between the horizontal and vertical distances (deltas) between two objects will tell you the angle between them. That's what the atan2 function is for (there are a few implementations of it in 6502 ASM, using look-up tables or approximations). Once you have the angle, you can use sines and cosines to move in that angle. You can also use the angle to draw the correct sprite for the object/projectile moving in that angle.

Top

 Post subject: Re: Calculating angles and angular velocity?Posted: Wed Apr 04, 2018 4:45 am

Joined: Fri Mar 16, 2018 1:52 pm
Posts: 38
Location: Finland
So in other words to aim a projectile at something I need to make a lookup table that contains all possible X and Y distances between the two objects? If you'd simply take X and Y coordinate distances and use those as a pointer to where to read the angle, that would make a very large table if you account every possible pixel on screen. Once you have the angle I'm assuming you can simply use another table to extract the correct X and Y speeds from the angle that was returned from the other table. The size of the table is kinda worrying if I'd were to use this method. There has to be some other way to create the table that is smaller in size.

Top

 Post subject: Re: Calculating angles and angular velocity?Posted: Wed Apr 04, 2018 7:33 am

Joined: Sat Feb 12, 2005 9:43 pm
Posts: 10977
Location: Rio de Janeiro - Brazil
Having a table indexed by every possible delta X/Y combination is not really feasible, the table would be huge. Instead, we normally we use a table to translate DeltaX divided by DeltaY (i.e. the ratio between the deltas) into an angle, which's a much smaller table. Divisions are slow on the 6502 though, and a common "trick" to speed this up is to use a log table and do a logarithmic division of the vales, which is just a subtraction, and use that result to lookup the angle in another table. You also need some extra logic to handle the different quadrants/octants.

Here's an example of such a routine: http://codebase64.org/doku.php?id=base: ... -bit_angle

Top

 Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending
 Page 1 of 1 [ 6 posts ]

 All times are UTC - 7 hours

#### Who is online

Users browsing this forum: Google [Bot] and 2 guests

 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot post attachments in this forum

Search for:
 Jump to:  Select a forum ------------------ NES / Famicom    NESdev    NESemdev    NES Graphics    NES Music    Homebrew Projects       2018 NESdev Competition       2017 NESdev Competition       2016 NESdev Competition       2014 NESdev Competition       2011 NESdev Competition    Newbie Help Center    NES Hardware and Flash Equipment       Reproduction    NESdev International       FCdev       NESdev China       NESdev Middle East Other    General Stuff    Membler Industries    Other Retro Dev       SNESdev       GBDev    Test Forum Site Issues    phpBB Issues    Web Issues    nesdevWiki