figure eight movement pattern

hello. i'm new to C++ and am looking for some help in adding some code to a game that i am coding so that a game object moves in a figure eight pattern around the game board. below are some snippets of what i have coded so far with my game object, is anyone able to assist me in obtaining my desired result?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
	gameObjectType cave[TOTAL_ROWS][TOTAL_COLS];	// the cave--a two dimensional array
	char board[TOTAL_ROWS + 3][TOTAL_COLS + 3] =	// the game board--a two dimensional array
    	{
            	{MT,MT,'A','B','C','D','E','F','G','H','I','J','K','L','M','N', MT},
            	{MT,ULC,HB, HB, HB, HB, HB, HB, HB, HB, HB, HB, HB, HB, HB, HB, URC},
            	{'A',VB,MT, MT, MT, MT, MT, MT, MT, MT, MT, MT, MT, MT, MT, MT, VB},
            	{'B',VB,MT, MT, MT, MT, MT, MT, MT, MT, MT, MT, MT, MT, MT, MT, VB},
            	{'C',VB,MT, MT, MT, MT, MT, MT, MT, MT, MT, MT, MT, MT, MT, MT, VB},
            	{'D',VB,MT, MT, MT, MT, MT, MT, MT, MT, MT, MT, MT, MT, MT, MT, VB},
            	{'E',VB,MT, MT, MT, MT, MT, MT, MT, MT, MT, MT, MT, MT, MT, MT, VB},
            	{'F',VB,MT, MT, MT, MT, MT, MT, MT, MT, MT, MT, MT, MT, MT, MT, VB},
            	{'G',VB,MT, MT, MT, MT, MT, MT, MT, MT, MT, MT, MT, MT, MT, MT, VB},
            	{'H',VB,MT, MT, MT, MT, MT, MT, MT, MT, MT, MT, MT, MT, MT, MT, VB},
				{'I',VB,MT, MT, MT, MT, MT, MT, MT, MT, MT, MT, MT, MT, MT, MT, VB},
				{'J',VB,MT, MT, MT, MT, MT, MT, MT, MT, MT, MT, MT, MT, MT, MT, VB},
            	{MT,LLC,HB, HB, HB, HB, HB, HB, HB, HB, HB, HB, HB, HB, HB, HB, LRC}
    	};


	// <WK7 status=permanent>
	//...Add Thief in any empty location
	thief.row = rand() %1 + 4;								    // update thief's row
	thief.column = rand() %8 + 2;
	cave[thief.row][thief.column] = THIEF;
	// </WK7>



// <WK7 status=permanent>
// process thief
void processThief(bool thiefPause, gameObjectType cave[TOTAL_ROWS][TOTAL_COLS], gameObject& thief, int& thiefMoveCounter, 
	playerObject& player, string& msg, bool& gameOver, int& killedThiefValue, int& killThiefValue, bool& movePlayer, 
	gameObjectType& hold, char board[TOTAL_ROWS + 3][TOTAL_COLS + 3])

{
	int row = 0, column = 0;

	// store last 2 thief moves
	//thief.qTwoBackPosition = thief.qOneBackPosition;
	//thief.qOneBackPosition = thief.position;
	
	if (thiefPause)
	{
		// Thief paused. Check if Thief starts moving again
		if (rand() %3 == 1)
		{
			thiefPause = false;
		}
	}
	else
	{
		// Move Thief
		thiefMoveCounter = (++thiefMoveCounter) %2;
		row = thief.row;
		column = thief.column;
		switch (thiefMoveCounter)
		{
		case 0:
			column--;
			break;
		case 1:
			row++;
			break;
		case 2:
			row--;
			break;
		case 3:
			column++;
			break;
		case 4:
			column--;
			break;
		case 5:
			row++;
			break;
		case 6:
			row--;
			break;
		case 7:
			column++;
			break;
		default:
			break;
		}
		// Check for events
		// If player found
		if (cave[row][column] == PLAYER)
		{
			msg = "The thief has found you";
			//........Resolve combat
            //gameOver = resolveCombat(player, msg, movePlayer, cave, row, column, board);
			
			//if (player.hasWeapon)
			//{
				//msg = "The thief stole your weapon.";
				//monster.isFound = true;
			//}
			//else
			//{
				//gameOver = false;
				//player.alive = true;
				//msg = "You felt the slight breeze of something else.";				
				// when player has no weapon and thief is found
                // thief will move back two spaces
                // interacting message will display
				//thief.position = thief.qOneBackPosition;
				//cave[thief.position.row][thief.position.column] = EMPTY;		//updates position information
				//cave[thief.position.row][thief.position.column] = PLAYER;
				//board[thief.position.row + 2][thief.position.column + 2] = MT;	//clear the screen where player was

				// set up to move thief back another space
				//row = thief.qTwoBackPosition.row;
				//column = thief.qTwoBackPosition.column;
            	//return false;			
			//}
		}
		// Check for events
		// If monster found
		//if (cave[row][column] == MONSTER)
		//{
			//msg = "The thief has found the monster";
			// Resolve combat
            //gameOver = resolveCombat(thief, monster, msg, monsterPause, thiefPause, cave, row, column, board);

			//if (thief.hasWeapon)
			//{
				//msg = "The thief found the monster and monster was slain.";
				//monster.isFound = true;
			//}
			//else
			//{
				//gameOver = true;
				//player.alive = false;
				//msg = "The monster found you and you have died";
			//}
		//}
		else
		{
			// Move Thief
			cave[thief.row][thief.column] = hold;		    // reveal what is under the monster <bug fix in week 6>
			//cave[monster.row][monster.column] = EMPTY;	// clear the cave location <creates bug>
			board[thief.row + 4][thief.column + 4] = MT;	// clear the screen behind monster
			//hold = cave[row][column];						// save what the monster is about to move over <bug fix in week 6>
			thief.row = row;								// update thief's row
			thief.column = column;						    // update thief's column
			cave[row][column] = THIEF;			// change thief's location in the cave
		}
	}
}
// </WK7> 
Last edited on
Mathematicians would call a figure-8 a lemniscate. Algebraically, the points on a lemniscate are given by the zero set of (a^2 + b^2)^2.

Through some magic, this can be parametrized into
phi(t) -> ( (a*sqrt(2)*cos(t))          / (1 + sin(t)*sin(t)),  
            (a*sqrt(2)*cos(t) * sin(t)) / (1 + sin(t)*sin(t))) )

Where a is the scale factor (how big the figure-8 is).

Run t through the closed interval [0, 2pi], and you get all the points on a figure-8. Set a to be appropriately large (e.g., 8), and step t appropriately based on how quick you want the object to move.
hi mbozzi, how and where do i write this into my code exactly?
can you show me?
additionally, i don't want the object to continuously move. i'm wanting the object to move
one space of the figure eight pattern, each time the player moves.
Create a function that returns a point on the curve. Give it the parameter, t, and the result is the position of the object.

This program samples the curve 20 times and spits out the points. This is enough to show (using pen and paper) that it actually results in a figure-8. See:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# include <cmath>
# include <iostream>

struct point { double x, y; };

point lemniscate(double const t) {
    static constexpr double scale = 8.0;
    
    using std::sin; using std::cos;
    return point
        {scale * std::sqrt(2)*cos(t)          / (1 + sin(t)*sin(t)),
         scale * std::sqrt(2)*cos(t) * sin(t) / (1 + sin(t)*sin(t))};
}

static constexpr double pi = 4 * std::atan(1.0);

int main() { 
    // object completes one revolution in roughly this many steps:
    static constexpr int n_steps = 20; 
    for (double t = 0.0; t <= 2*pi; t += 2*pi / n_steps) {
        point const p = lemniscate(t);
        
        std::cout << '(' << p.x << ", " << p.y << ")\n";
    }
}

Live demo:
http://coliru.stacked-crooked.com/a/0c1a60168a15ce22

Note that if you want more complex behavior (e.g., because the object moves at a fixed velocity), that needs to be specified.
Last edited on
Topic archived. No new replies allowed.