It is currently Thu Dec 05, 2013 11:10 pm

 All times are UTC

 Page 1 of 1 [ 6 posts ]
 Print view Previous topic | Next topic
Author Message
 Post subject: E motionPosted: Thu Jan 24, 2013 12:25 pm
 Level 1 Cleric

Joined: Mon Aug 31, 2009 11:21 am
Posts: 10
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 } ;
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 << "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 ;
}
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

 Post subject: Re: E motionPosted: Thu Jan 24, 2013 4:15 pm
 Dexterous Droid

Joined: Wed Aug 18, 2004 7:40 pm
Posts: 3750
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

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

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

 Post subject: Re: E motionPosted: Tue Jan 29, 2013 8:34 pm
 Lord of Cheesecakes

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

Haha but this is pretty cool.

Top

 Post subject: Re: E motionPosted: Tue Jan 29, 2013 11:59 pm
 Bibliotherapist

Joined: Wed Nov 03, 2004 1:28 pm
Posts: 6874
Location: Wilts, Englandshire
Every time I see this thread I think of this old Amiga/Atari ST game:

_________________
20 GOTO 10

Top

 Post subject: Re: E motionPosted: Thu Jan 31, 2013 12:52 pm
 Level 1 Cleric

Joined: Mon Aug 31, 2009 11:21 am
Posts: 10
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

 Post subject: Re: E motionPosted: Thu Jan 31, 2013 2:44 pm
 Level 1 Cleric

Joined: Mon Aug 31, 2009 11:21 am
Posts: 10
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 ) ;
"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 ) ;
while ( in != 'y' )
{
int i , e ;
double d = 100 , dist ;
for ( i = 0 ; i < 13 ; i++ )
{
add = emo[ i ] - now ;
if ( d > dist )
{
e = i ;
d = dist ;
}
}
cout << "\n\n\n\n" ;
cout << "I feel " ;
cout << emo[ e ].name << " .\n\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

 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

#### 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 forum

Search for:
 Jump to:  Select a forum ------------------ Forums    Forum Rules and Posting Guidelines Wiki Discussion    Help    Content Issues Game Programming Discussion    C and C++ Game Programming    Java Game Programming    Language Agnostic Programming    .NET Game Programming    VB Game Programming    Mobile Game Programming    Web-Based Game Programming    Other Languages    OpenGL Development    Direct X Development Game Development Discussion    Game Design    Game Media Off-Topic Discussion    Announcements    Off-Topic    Community Projects    News