Chunky by FelipeFS
Chunky by FelipeFS
GPWiki.org
It is currently Thu Apr 17, 2014 6:41 am

All times are UTC




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: E motion
PostPosted: Thu Jan 24, 2013 12:25 pm 
Level 1 Cleric

Joined: Mon Aug 31, 2009 11:21 am
Posts: 10
Location: zaanstad NL
this is an example on emotions in a NPC
Code:
#include <cstdlib>
#include <iostream>
#include <math.h>
#include <ctime>

using namespace std ;

class emotion
{
public :
  char* name ;
  double x ; //arousal
  double y ; //stance
  double z ; //valance
  void fill( char* nm
  , double a , double b , double c )
  {
    name = nm ;
    x = a ;
    y = b ;
    z = c ;
  }
} ;

const int accepting =  0 ;
const int alert     =  1 ;
const int anger     =  2 ;
const int calm      =  3 ;
const int disgust   =  4 ;
const int fear      =  5 ;
const int happy     =  6 ;
const int joy       =  7 ;
const int sooted    =  8 ;
const int sorrow    =  9 ;
const int suprice   = 10 ;
const int tired     = 11 ;
const int unhappy   = 12 ;

emotion emo[ 20 ] = {
  { "accepting" ,  0.0 ,  8.0 ,  0.0 }
, { "alert"     ,  4.0 ,  0.0 ,  0.0 }
, { "anger"     ,  3.0 , -5.0 ,  0.0 }
, { "calm"      ,  0.0 ,  0.0 ,  0.0 }
, { "disgust"   , -1.0 , -5.0 ,  0.0 }
, { "fear"      ,  3.0 ,  5.0 ,  3.0 }
, { "happy"     ,  0.0 ,  0.0 ,  8.0 }
, { "joy"       ,  2.0 ,  0.0 ,  3.0 }
, { "sooted"    , -3.0 ,  0.0 ,  3.0 }
, { "sorrow"    , -2.5 , -0.0 , -3.5 }
, { "suprice"   ,  8.0 ,  0.0 ,  0.0 }
, { "tired"     , -4.0 ,  0.0 ,  0.0 }
, { "unhappy"   ,  0.0 ,  0.0 , -4.0 } } ;

double dist( emotion a , emotion b )
{
  emotion c ;
  c.fill( " "
  , a.x - b.x
  , a.y - b.y
  , a.z - b.z ) ;
  return sqrt(
    c.x * c.x
  + c.y * c.y
  + c.z * c.z )
  + 1e-10 ;
}
emotion Eadd( emotion a , emotion b )
{
  emotion c ;
  c.fill( " "
  , a.x + b.x
  , a.y + b.y
  , a.z + b.z ) ;
  return c ;
}
emotion move(
  emotion a , emotion to , double m )
{
  double q = dist( a , to ) ;
  emotion b ;
  b.fill( " "
  , ( to.x - a.x ) / q * m
  , ( to.y - a.y ) / q * m
  , ( to.z - a.z ) / q * m ) ;
  return Eadd( a , b ) ;
}
double rnd()
{
  return (double)rand()/(double)RAND_MAX ;
}
int main(int argc, char *argv[])
{
  char in = ' ' ;
  srand( static_cast<unsigned int>( clock())) ;
  emotion now = { " " , 0 , 0 , 0 } ;
  emotion add ;
  while ( in != 'y' )
  {
    int i , e ;
    double d = 100 ;
    for ( i = 0 ; i < 13 ; i++ )
    {
      if ( d >
        dist( now , emo[ i ] ) )
      {
        e = i ;
        d = dist( now , emo[ i ] ) ;
      }
    }
    cout << "\n\n\n\n" ;
    cout << "I feel " ;
    cout << emo[ e ].name << " .\n\n" ;
    cout << "Menu :\n" ;
    cout << "q = show big spider\n" ;
    cout << "w = show smal spider\n" ;
    cout << "e = give hug\n" ;
    cout << "r = give kick\n" ;
    cout << "t = random reaction\n" ;
    cout << "y = quit sim .\n\n" ;
    cout << "choice = " ;
    cin >> in ;
    switch ( in )
    {
      case 'q' : //big spider
        now = move( now , emo[ fear ] , 2 ) ;
      break ;
      case 'w' : //smal spider
        now = move( now , emo[ fear ] , 1 ) ;
      break ;
      case 'e' : //hug
        now = move( now , emo[ sooted ] , 2 ) ;
      break ;
      case 'r' : //kick
        now = move( now , emo[ anger ] , 3 ) ;
      break ;
      default :
        add.x = rnd() * 4 - 2 ;
        add.y = rnd() * 4 - 2 ;
        add.z = rnd() * 4 - 2 ;
        now = Eadd( now , add ) ;
    }
    if ( now.x < -8 ) now.x = -8 ;
    if ( now.x >  8 ) now.x =  8 ;
    if ( now.y < -8 ) now.y = -8 ;
    if ( now.y >  8 ) now.y =  8 ;
    if ( now.z < -8 ) now.z = -8 ;
    if ( now.z >  8 ) now.z =  8 ;

  }

  return 0 ;
}

_________________
cxiu diferas el tio respondas cxiu samvaloras
[ esperanto for : everybody is diferent therefore everybody is equal ]


Top
 Profile  
 
 Post subject: Re: E motion
PostPosted: Thu Jan 24, 2013 4:15 pm 
Dexterous Droid
User avatar

Joined: Wed Aug 18, 2004 7:40 pm
Posts: 3762
Location: South Africa
Quite interesting, but gives some weird results. Like going from spiders to hugs the transition is fear<->joy<->soothed. What's joy doing in there?

And from kicks to big spider there's another joy popping up on the way to fear :lol

Sometimes I worry I'm living in a simulation of a some 13 dimensional programmer who's spamming the kick button just to see what happens :geek

Some code improvements I would suggest are:
* move your methods into the class
* give the variables more descriptive names rather than having comments
* when you pass the class into methods use by-reference operator & like (emotion& a , emotion& b)
* use const in the method signatures if you aren't changing the value like (const emotion& a , const emotion& b)
* the internal value caps [-8,8] should also be enforced by the class, move if ( now.x < -8 ) now.x = -8 ; into the value setters
* instead of setting d to a large number, rather set it to INFINITY, which will guarantee that your logic will work if you change the range of values later. double d = INFINITY ;

_________________
Whatever the mind can conceive and believe, it can achieve


Top
 Profile  
 
 Post subject: Re: E motion
PostPosted: Tue Jan 29, 2013 8:34 pm 
BANNED

Joined: Sun Jun 24, 2012 12:49 am
Posts: 504
E-motion... electronic motion? :confused

Haha but this is pretty cool. :thumbs


Top
 Profile  
 
 Post subject: Re: E motion
PostPosted: Tue Jan 29, 2013 11:59 pm 
Bibliotherapist
User avatar

Joined: Wed Nov 03, 2004 1:28 pm
Posts: 7004
Location: Wilts, Englandshire
Every time I see this thread I think of this old Amiga/Atari ST game:
http://www.youtube.com/watch?v=F57ficYjR5A

_________________
10 PRINT "Bad Monkey ";
20 GOTO 10


Top
 Profile  
 
 Post subject: Re: E motion
PostPosted: Thu Jan 31, 2013 12:52 pm 
Level 1 Cleric

Joined: Mon Aug 31, 2009 11:21 am
Posts: 10
Location: zaanstad NL
the 3 D system and numbers
i got out of "de connectie"
[ arousal i know but what are 'valance' and 'stance' ? ]
a dutch ai magazine
i just tryed someting
any sugestions on how to improve it are welkome
thinking : more dim's , other numbers ?
i think if we fix this
it is better than a FSM

_________________
cxiu diferas el tio respondas cxiu samvaloras
[ esperanto for : everybody is diferent therefore everybody is equal ]


Last edited by blua tigro on Thu Jan 31, 2013 3:03 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: E motion
PostPosted: Thu Jan 31, 2013 2:44 pm 
Level 1 Cleric

Joined: Mon Aug 31, 2009 11:21 am
Posts: 10
Location: zaanstad NL
update :
- i tryed more class metods

error :
- 'arousal is not a type' [???]
etc...
- 'missing , ;' [???]
etc...

what is the corect syntaks ?

Code:
#include <cstdlib>
#include <iostream>
#include <math.h>
#include <ctime>

using namespace std ;

class emotion
{
public :
  char* name ;
  double arousal ;
  double stance ;
  double valance ;
  emotion()
  {
    emotion( "" , 0 , 0 , 0 ) ;
  }
  emotion( char* nm
  , double x , double y , double z )
  {
    name = nm ;
    arousal = x ;
    stance = y ;
    valance = z ;
    keepincube() ;
  }
  void fill( char* nm
  , double a , double b , double c )
  {
    name = nm ;
    arousal = a ;
    stance = b ;
    valance = c ;
    keepincube() ;
  }
  double lenght()
  {
    return sqrt( arousal * arousal
               + stance * stance
               + valance * valance ) ;
  }
  emotion operator + ( emotion other )
  {
    double x , y , z ;
    x = this.arousal + other.arousal ;
    y = this.stance + other.stance ;
    z = this.valance + other.valance ;
    keepincube() ;
    return emotion( "" , x , y , z ) ;
  }
  emotion operator - ( emotion other )
  {
    double x , y , z ;
    x = this.arousal - other.arousal ;
    y = this.stance - other.stance ;
    z = this.valance - other.valance ;
    keepincube() ;
    return emotion( "" , x , y , z ) ;
  }
  void move( emotion to , double m )
  {
    emotion b = to - this ;
    double q = b.lenght() + 1e-10 ;
    b.arousal *= m / q ;
    b.stance *= m / q ;
    b.valance *= m / q ;
    this = this + b ;
  }
private :
  void keepincube()
  {
    if ( arousal < -8 ) arousal = -8 ;     
    if ( arousal >  8 ) arousal =  8 ;     
    if ( stance < -8 ) stance = -8 ;     
    if ( stance >  8 ) stance =  8 ;     
    if ( valance < -8 ) valance = -8 ;     
    if ( valance >  8 ) valance =  8 ;     
  }
} ;

const int accepting =  0 ;
const int alert     =  1 ;
const int anger     =  2 ;
const int calm      =  3 ;
const int disgust   =  4 ;
const int fear      =  5 ;
const int happy     =  6 ;
const int joy       =  7 ;
const int sooted    =  8 ;
const int sorrow    =  9 ;
const int suprice   = 10 ;
const int tired     = 11 ;
const int unhappy   = 12 ; 

emotion emo[ 20 ] ;
emo[ accepting ].fill(
"accepting" ,  0.0 ,  8.0 ,  0.0 ) ;
emo[ alert ].fill(
"alert"     ,  4.0 ,  0.0 ,  0.0 ) ;
emo[ anger ].fill(
"anger"     ,  3.0 , -5.0 ,  0.0 ) ;
emo[ calm ].fill(
"calm"      ,  0.0 ,  0.0 ,  0.0 ) ;
emo[ disgust ].fill(
"disgust"   , -1.0 , -5.0 ,  0.0 ) ;
emo[ fear ].fill(
"fear"      ,  3.0 ,  5.0 ,  3.0 ) ;
emo[ happy ].fill(
"happy"     ,  0.0 ,  0.0 ,  8.0 ) ;
emo[ joy ].fill(
"joy"       ,  2.0 ,  0.0 ,  3.0 ) ;
emo[ sooted ].fill(
"sooted"    , -3.0 ,  0.0 ,  3.0 ) ;
emo[ sorrow ].fill[
"sorrow"    , -2.5 , -0.0 , -3.5 ) ;
emo[ suprice ].fill(
"suprice"   ,  8.0 ,  0.0 ,  0.0 ) ;
emo[ tired ].fill(
"tired"     , -4.0 ,  0.0 ,  0.0 ) ;
emo[ unhappy ].fill(
"unhappy"   ,  0.0 ,  0.0 , -4.0 ) ;

double rnd()
{
  return (double)rand()/(double)RAND_MAX ;
}
int main(int argc, char *argv[])
{
  char in = ' ' ;
  srand( static_cast<unsigned int>( clock())) ;
  emotion now( " " , 0 , 0 , 0 ) ;
  emotion add ;
  while ( in != 'y' )
  {
    int i , e ;
    double d = 100 , dist ;
    for ( i = 0 ; i < 13 ; i++ )
    {
      add = emo[ i ] - now ;
      dist = add.lenght() ;
      if ( d > dist )
      {
        e = i ;
        d = dist ;
      }
    }
    cout << "\n\n\n\n" ;
    cout << "I feel " ;
    cout << emo[ e ].name << " .\n\n" ;
    cout << "Menu :\n" ;
    cout << "q = show big spider\n" ;
    cout << "w = show smal spider\n" ;
    cout << "e = give hug\n" ;
    cout << "r = give kick\n" ;
    cout << "t = random reaction\n" ;
    cout << "y = quit sim .\n\n" ;
    cout << "choice = " ;   
    cin >> in ;
    switch ( in )
    {
      case 'q' : //big spider
        now.move( emo[ fear ] , 2 ) ;
      break ;
      case 'w' : //smal spider
        now.move( emo[ fear ] , 1 ) ;
      break ;
      case 'e' : //hug
        now.move( emo[ happy ] , 2 ) ;
      break ;
      case 'r' : //kick
        now.move( emo[ anger ] , 3 ) ;
      break ;
      default :
        add.arousal = rnd() * 4 - 2 ;
        add.stance = rnd() * 4 - 2 ;
        add.valance = rnd() * 4 - 2 ;
        now = now + add ;
    }

  }

  return 0 ;
}

_________________
cxiu diferas el tio respondas cxiu samvaloras
[ esperanto for : everybody is diferent therefore everybody is equal ]


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC


Who is online

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


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

Search for:
Jump to:  

Powered by phpBB® Forum Software © phpBB Group