Another simple question about basic trigonomety

All topics about ZGameEditor goes here.

Moderator: Moderators

Post Reply
jinxtengu
Posts: 122
Joined: Wed Oct 14, 2009 2:05 pm
Contact:

Another simple question about basic trigonomety

Post by jinxtengu »

Hi again,
I'm working on a game with z game editor and I wanted to create a model that has 8 directions of animation depending on it's movements, so the most obvious comparison to what I want to implement would be the ghosts from a game like the arcade version of gauntlet, Image

the way I thought of doing this was by having a variable connected to different states, each state playing a different animation.
Unfortunately, even after some years of working on getting my head around basic trigonometric functions (the equivalent of year 10 maths)
I still find it hard to visualize the correct formulas to get the Sin (opposite over hypotenuse), cos (adjacent over hypotenuse) and tan (opposite over adjacent),in relation to the velocity of an object.
I really want to understand this stuff, since it actually quite basic, and it would mean that I could make games faster.

Anyway,
so, I wrote this code which doesn't appear to work, and im not sure why.

This is the code:

Dir=round(atan2(currentmodel.velocity.x,currentmodel.velocity.z)/PI*2)/2/8);


what I was hoping for it to do was to spit out 8 intergers, depending on the direction of my models,
for some reason it's not doing that.
Does anyone have any ideas on how I can solve this problem? :mrgreen:
User avatar
Kjell
Posts: 1915
Joined: Sat Feb 23, 2008 11:15 pm

Re: Another simple question about basic trigonomety

Post by Kjell »

Hi jinxtengu,
jinxtengu wrote: Tue Apr 12, 2022 2:21 pmI wanted to create a model that has 8 directions of animation depending on it's movements, so the most obvious comparison to what I want to implement would be the ghosts from a game like the arcade version of gauntlet.
I understand the reference, but do keep in mind that Gauntlet doesn't use the approach that you're attempting .. doing that kind of trigonometry on a 68010 is a waste of cycles.
jinxtengu wrote: Tue Apr 12, 2022 2:21 pmDir=round(atan2(currentmodel.velocity.x,currentmodel.velocity.z)/PI*2)/2/8);
Anyway, you were pretty close :wink: Here's a example using atan2:

Code: Select all

<?xml version="1.0" encoding="iso-8859-1" ?>
<ZApplication Name="App" Caption="ZGameEditor application" CameraPosition="0 0 9" FOV="90" FileVersion="2">
  <OnLoaded>
    <ZExternalLibrary ModuleName="opengl32">
      <Source>
<![CDATA[//

void glBegin(int mode){}
void glEnd(){}
void glVertex2f(float x, float y){}]]>
      </Source>
    </ZExternalLibrary>
  </OnLoaded>
  <OnRender>
    <ZExpression>
      <Expression>
<![CDATA[// Create "vector" from mouse input & draw to screen

float x, y;

x = App.MousePosition.X*16;
y = App.MousePosition.Y*9;

glBegin(1);
glVertex2f(0, 0);
glVertex2f(x, y);
glEnd();

// Calculate angle & print to screen

int direction = round(atan2(x, y) / PI * 4 + 4);
Print.Text = intToStr(direction & 7);

// Draw reference sectors

glBegin(1);

for(int i=0; i<4; i++)
{
  float a = (i + 0.5) * PI * 0.25;

  float s = sin(a) * 8;
  float c = cos(a) * 8;

  glVertex2f( s, -c);
  glVertex2f(-s,  c);
}

glEnd();]]>
      </Expression>
    </ZExpression>
    <RenderText Name="Print" X="-0.9" Align="1"/>
  </OnRender>
</ZApplication>
K
jinxtengu
Posts: 122
Joined: Wed Oct 14, 2009 2:05 pm
Contact:

Re: Another simple question about basic trigonomety

Post by jinxtengu »

Thanks Kjell!
Muchas gracias! :D

Yeah I suspected Gauntlet wouldn't use this approach, but I thought some reference would be useful to help visualize the problem.
Post Reply