I have a simple script (please no remarks on the fact that I'm not using a switch statement or better code, this is the earliest version and written this way by a peer, I am improving it) that takes an object and moves it back and forth. For some reason, the variable time gets stuck at 249. It is probably an obvious bug with this inefficient logic, but I cannot seem to find it.
var speed = 1;
private var time = 0;
function Start() {
}
function Update() {
if(condition == true)moveStuff();
}
function moveStuff() {
var timeSwitch = false;
if(time == 0)timeSwitch = false;
if(time == timeSet)timeSwitch = true;
if(direction == 1) {
if(timeSwitch == false) {
transform.Translate(Vector3.up * (Time.deltaTime * speed));
time += 1;
Debug.Log(time);
}else if(timeSwitch == true) {
transform.Translate(Vector3.up * ((Time.deltaTime * speed) * -1));
time -= 1;
Debug.Log(time);
}
} else if(direction == 2) {
if(timeSwitch == false) {
transform.Translate(Vector3.down * (Time.deltaTime * speed));
time += 1;
Debug.Log("Moved down. ");
}else if(timeSwitch == true){
transform.Translate(Vector3.down * ((Time.deltaTime * speed) * -1));
time -= 1;
}
} else if(direction == 3) {
if(timeSwitch == false) {
transform.Translate(Vector3.forward * (Time.deltaTime * speed));
time += 1;
Debug.Log("Moved forward. ");
}else if(timeSwitch == true){
transform.Translate(Vector3.forward * ((Time.deltaTime * speed) * -1));
time -= 1;
}
} else if(direction == 4) {
if(timeSwitch == false) {
transform.Translate(Vector3.back * (Time.deltaTime * speed));
time += 1;
Debug.Log("Moved back. ");
}else if(timeSwitch == true){
transform.Translate(Vector3.back * ((Time.deltaTime * speed) * -1));
time -= 1;
}
} else if(direction == 5) {
if(timeSwitch == false) {
transform.Translate(Vector3.right * (Time.deltaTime * speed));
time += 1;
Debug.Log("Moved right. ");
}else if(timeSwitch == true){
transform.Translate(Vector3.right * ((Time.deltaTime * speed) * -1));
time -= 1;
}
} else if(direction == 6) {
if(timeSwitch == false) {
transform.Translate(Vector3.left * (Time.deltaTime * speed));
time += 1;
Debug.Log("Moved left. ");
}else if(timeSwitch == true){
transform.Translate(Vector3.left * ((Time.deltaTime * speed) * -1));
time -= 1;
}
}
}
I had been declaring the variable timeSet as false each time at the beginning of moveStuff(). I moved that as a private var up to the area where I declared my other things and it worked perfectly.
Related
I'm fighting with this for like few days, and I have no idea, how to do that, so I'd like to ask you for help. I've got no idea how collision should look like right here, so player could jump through 'down zone' of the block, and stay right on the block.
block.cpp
bool block::CollidingWithPlayer(character& player) {
for (int i = 1; i < MAX_BLOCKS; i++) {
if (player.x + player.width >= coordinateX[i] && player.x <= coordinateX[i] + width[i] && player.y + player.height >= coordinateY[i] && player.y <= coordinateY[i] + block_height) {
player.onGround = true;
return true;
}
}
}
character.cpp
void character::startJump(map& Map, character& player) {
if (onGround)
{
vel[1] = -11.0;
onGround = false;
}
}
void character::updateJump(block& Block, character& player) {
if (!onGround) {
Block.CollidingWithPlayer(player);
vel[1] += 0.5;
y += vel[1];
x += vel[0];
}
if (y > 460){
y = 460;
vel[1] = 0.0;
onGround = true;
vel[0] = 0.0;
}
if ((x + width >= START_OF_RIGHT_WALL && x <= WALL_WIDTH + START_OF_RIGHT_WALL) || (x + width >= START_OF_LEFT_WALL &&x <= START_OF_LEFT_WALL + WALL_WIDTH)){
vel[0] *= -1;
bound = true;
if (direction == 1)
direction = 2;
else if (direction == 2)
direction = 1;
}
}
I'm trying to make a game (simple 2d platformer).
The program runs as it should, but an if statement doesn't work correctly.
I have this function:
int Collision::platformCollision(SDL_Rect *hitbox, SDL_Rect plat) {
if (checkCollision(*hitbox, plat)) {
//X
//LEFT SIDE
if (hitbox->x + hitbox->w > plat.x && hitbox->x + hitbox->w < plat.x + 5) {
hitbox->x = plat.x - hitbox->w;
return 1;
}
//RIGHT SIDE
if (hitbox->x < plat.x + plat.w && hitbox->x > plat.x + plat.w - 5) {
hitbox->x = plat.x + plat.w;
return 2;
}
//Y
//UPPER SIDE
if (hitbox->y + hitbox->h > plat.y && hitbox->y + hitbox->h < plat.y + 10) {
hitbox->y = plat.y - hitbox->h;
return 3;
}
//BOTTOM SIDE
if (hitbox->y < plat.y + plat.h && hitbox->y > plat.y + plat.h - 10) {
hitbox->y = plat.y + plat.h;
return 4;
}
}
//NOT COLLIDING
return -1;
}
So I have this function return an int whenever it collides with a certain part of the platform.
Then I have this function:
void Player::playerCheckPlatCollision(SDL_Rect rect) {
if (platformCollision(p_hitboxPTR, rect) == 3) {
setGravityF(0.0);
}
if (platformCollision(p_hitboxPTR, rect) == 4) {
p_space = false;
}
return;
}
The problem should be easy to fix.
When I debug the program it gets to return 4; in the platformCollision function, but when I do
if (platformCollision(p_hitboxPTR, rect) == 4) {
p_space = false;
}
It doesn't put p_space as false, it just ignores the == 4 and when I debugged, I saw it got to that if statement.
Can someone please help.
Thanks.
If platformCollision returns 4 on the first call, it alters state, and will not return 4 on the second call.
void Player::playerCheckPlatCollision(SDL_Rect rect) {
int bang = platformCollision(p_hitboxPTR, rect);
if (bang == 3) {
setGravityF(0.0);
} else if (bang == 4) {
p_space = false;
}
}
I'm designing and programming an elevator-like robot for a high school project. Could I possibly do anything to make this any simpler? Or better? I have attached a picture of my design that I made in AutoCAD Inventor with labels.
For those not familiar with RobotC or VEX (it is VERY similar to C and C++): the limit switches (limit1, limit2, ...) and bump switches (floor1, floor2, ...) are analog buttons and return a value of 0 if not pressed and 1 if pressed. The motor (mainMotor) rotates the gear which causes the mechanism to travel upwards on the slide. When the shaft sticking out the motor mechanism moves up and down, it presses limit switches and causes it to return a value of 1.
int callup [3];
int calldown [3];
int floorat[3];
int main ()
{
if (SensorValue[limit1] == 1)
{
floorat[0] = 1;
}
else
{
floorat[0] = 0;
}
if (SensorValue[limit2] == 1)
{
floorat[1] = 1;
}
else
{
floorat[1] = 0;
}
if (SensorValue[limit3] == 1)
{
floorat[2] = 1;
}
else
{
floorat[2] = 0;
}
if (SensorValue[floor1] == 1)
{
calldown[0] = 1;
SensorValue[LED1] = 1;
}
if (SensorValue[floor2] == 1 && floorat[2] == 1)
{
calldown[1] = 1;
SensorValue[LED2] = 1;
}
if (SensorValue[floor2] == 1 && floorat[0] == 1)
{
callup[1] = 1;
SensorValue[LED2] = 1;
}
if (SensorValue[floor3])
{
callup[2] = 1;
SensorValue[LED3] = 1;
}
motors ();
}
void motors ()
{
if (callup[2] == 1 && floorat[2] == 1)
{
int x = 1;
while (x < 3)
{
SensorValue[LED3] = 1;
wait(0.5);
SensorValue[LED3] = 0;
wait(0.5);
}
callup[2] = 0;
main ();
}
else if (callup[1] == 1 && floorat[1] == 1)
{
int x = 1;
while (x < 3)
{
SensorValue[LED2] = 1;
wait(0.5);
SensorValue[LED2] = 0;
wait(0.5);
}
callup[1] = 0;
main ();
}
else if (callup[0] == 1 && floorat[0] == 1)
{
int x = 1;
while (x < 3)
{
SensorValue[LED1] = 1;
wait(0.5);
SensorValue[LED1] = 0;
wait(0.5);
}
callup[0] = 0;
main ();
}
if (callup[2] == 1 && floorat[1] == 1 && calldown[0] == 0 || callup[2] == 1 && floorat[0] == 1 && callup[1] == 0)
{
startMotor(mainMotor, 60);
untilTouch(limit3);
stopMotor(mainMotor);
callup[2] = 0;
wait(1);
main ();
}
if (callup[1] == 1 && floorat[0] == 1)
{
startMotor(mainMotor, 60);
untilTouch(limit2);
stopMotor(mainMotor);
callup[1] = 0;
wait(1);
main();
}
if (calldown[1] == 1 && floorat[2] == 1)
{
startMotor(mainMotor, -60);
untilTouch(limit2);
stopMotor(mainMotor);
calldown[1] = 0;
wait(1);
main();
}
if (calldown[0] == 1 && floorat[2] == 1 && calldown[1] == 0 || calldown[0] == 1 && floorat[1] == 1)
{
startMotor(mainMotor, -60);
untilTouch(limit1);
stopMotor(mainMotor);
calldown[0] = 0;
wait(1);
main();
}
}
Although it shouldn't be a concern for this question, the 60 in the startMotor command is the speed of the motor, just to make it clearer.
Feel free to ask any more questions.
Let's define what are the states of an elevator at a given moment:
An elevator can go up, down, or be idle.
The elevator is at a given floor and go from one floor to the other when it trigger a switch:
Now, if we translate this into some pseudo code (which should be easily translated to RobotC) :
enum elevator_status = { idle, down, up };
int currentfloor; //1, 2, 3
switch(elevator_status)
{
case idle:
//we check if a button is pressed and possibly go up or down
if(SensorValue(floor1))
{
if(currentfloor > 1)
elevator_status = down;
}
else if(SensorValue(floor2))
{
if(currentfloor > 2)
elevator_status = down;
else if(currentfloor < 2)
elevator_status = up;
}
else if(SensorValue(floor3))
{
if(currentfloor < 3)
elevator_status = up;
}
break;
case up:
case down:
//we check if we trigger a floor switch and stop the elevator
if(SensorValue(limit1))
{
currentfloor = 1;
elevator_status = idle;
}
else if(SensorValue(limit2))
{
currentfloor = 2;
elevator_status = idle;
}
else if(SensorValue(limit3))
{
currentfloor = 3;
elevator_status = idle;
}
break;
}
//we set the speed of the motor
if(elevator_status == up)
{
set_motorstate(cw);
)
else if(elevator_status == down)
{
set_motorstate(ccw);
}
else if(elevator_status == idle)
{
set_motorstate(idle);
}
Note : in this code the elevator will only take care of new up and down floor calls when the elevator is idle. It does not store up and down call while it is moving and go there later. I do not know if it was a requirement for you.
I'm not familiar with RobotC or VEX, however I've noticed a certain amount of replicated operations that could be made into their own functions.
The following code snippets I would make into separate functions. So in the large function called motors you have the following set of operations:
int x = 1;
while (x < 3)
{
SensorValue[LED3] = 1;
wait(0.5);
SensorValue[LED3] = 0;
wait(0.5);
}
callup[2] = 0;
main ();
This is repeated with slightly different values.
Here I'd write a function like the following:
void adjust_sensors( size_t led, size_t level )
{
int x = 1;
while (x < 3)
{
SensorValue[led] = 1;
wait(0.5);
SensorValue[led] = 0;
wait(0.5);
}
callup[level] = 0;
main ();
}
You can do the same for the following code as well:
startMotor(mainMotor, 60);
untilTouch(limit3);
stopMotor(mainMotor);
callup[2] = 0;
wait(1);
main ();
Also it seems like the while loop will never end because the value of x never changes.
You also have a typo at the top when you declare:
int callown [2];
I presume you meant:
int calldown [2];
Would be good to add some comments to your code as well for clarity.
Hope this helps.
I could be way off, because I'm just a student with questions of my own but it looks like you may have made a mistake in your array sizes. For instance, when you declared:
int floorat[2];
This made the array size 2. Then you refer to 3 element locations in this array [0, 1, 2]. Also, can't you just use a regular integer, and assign it values 1, 2, or 3?
I would recommend redefining these varaibles as:
int callup;
int calldown;
int floorat;
Then you can avoid extra lines of code and simplify the if/else clauses to:
if (SensorValue[limit1] == 1)
{
floorat = 1;
}
if (SensorValue[limit2] == 1)
{
floorat = 2;
}
if (SensorValue[limit3] == 1)
{
floorat = 3;
}
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I had written a function , which is supposed to analyze and use the data (string to be specific) produced from another function and calculate the percentage of chars and print out the results.
this function is of type int but the previous function is of type string. I was thinking of using pointers but I don't know if it's a valid conversion.
I also don't believe that putting a function in another function argument is valid.
here is an almost complete function
int percentages(string line)
{
int overalcount;
double Leu, Phe, Ile, STA, Val, Ser, Pro, Thr, Ala, Tyr, STO, His, Gln, Asn, Lys, Asp, Glu, Cys, Trp, Arg, Gly;
int percentage, percentage1, percentage2, percentage3, percentage4, percentage5, percentage6, percentage7;
int percentage8, percentage9, percentage10, percentage11, percentage12, percentage13, percentage14;
int percentage15, percentage16, percentage17, percentage18, percentage19, percentage20;
int i;
for (i = 0; i + 3 < line.length(); i += 3)
{
overalcount++;
if(line.substr(i, 3) == "Phe")
{
Phe++;
if(!Phe == 0)
{
percentage = (Phe / overalcount) * 100;
}
}
else if(line.substr(i, 3) == "Leu")
{
Leu++;
if(!Leu == 0)
{
percentage = (Leu / overalcount) * 100;
}
}
else if(line.substr(i, 3) == "Ile")
{
Ile++;
if(!Ile == 0)
{
percentage = (Ile / overalcount) * 100;
}
}
else if(line.substr(i, 3) == "STA")
{
STA++;
if(!STA == 0)
{
percentage = (STA / overalcount) * 100;
}
}
else if(line.substr(i, 3) == "Val")
{
Val++;
if(!Val == 0)
{
percentage = (Val / overalcount) * 100;
}
}
else if(line.substr(i, 3) == "Ser")
{
Ser++;
if(!Ser == 0)
{
percentage = (Ser / overalcount) * 100;
}
}
else if(line.substr(i, 3) == "Pro")
{
Pro++;
if(!Pro == 0)
{
percentage = (Pro / overalcount) * 100;
}
}
else if(line.substr(i, 3) == "Thr")
{
Thr++;
if(!Thr == 0)
{
percentage = (Thr / overalcount) * 100;
}
}
else if(line.substr(i, 3) == "Ala")
{
Ala++;
if(!Ala == 0)
{
percentage = (Ala / overalcount) * 100;
}
}
else if(line.substr(i, 3) == "Tyr")
{
Tyr++;
if(!Tyr == 0)
{
percentage = (Tyr / overalcount) * 100;
}
}
else if(line.substr(i, 3) == "STO")
{
STO++;
if(!STO == 0)
{
percentage = (STO / overalcount) * 100;
}
}
else if(line.substr(i, 3) == "His")
{
His++;
if(!His == 0)
{
percentage = (His / overalcount) * 100;
}
}
else if(line.substr(i, 3) == "Gln")
{
Gln++;
if(!Gln == 0)
{
percentage = (Gln / overalcount) * 100;
}
}
else if(line.substr(i, 3) == "Asn")
{
Asn++;
if(!Asn == 0)
{
percentage = (Asn / overalcount) * 100;
}
}
else if(line.substr(i, 3) == "Lys")
{
Lys++;
if(!Lys == 0)
{
percentage = (Lys / overalcount) * 100;
}
}
else if(line.substr(i, 3) == "Asp")
{
Asp++;
if(!Asp == 0)
{
percentage = (Asp / overalcount) * 100;
}
}
else if(line.substr(i, 3) == "Glu")
{
Glu++;
if(!Glu == 0)
{
percentage = (Glu / overalcount) * 100;
}
}
else if(line.substr(i, 3) == "Cys")
{
Cys++;
if(!Cys == 0)
{
percentage = (Cys / overalcount) * 100;
}
}
else if(line.substr(i, 3) == "Trp")
{
Trp++;
if(!Trp == 0)
{
percentage = (Trp / overalcount) * 100;
}
}
else if(line.substr(i, 3) == "Arg")
{
Arg++;
if(!Arg == 0)
{
percentage = (Arg / overalcount) * 100;
}
}
else if(line.substr(i, 3) == "Gly")
{
Gly++;
if(!Gly == 0)
{
percentage = (Gly / overalcount) * 100;
}
}
}
if(!percentage == 0)
{
cout << "Percentage of Phe: " <<percentage <<endl;
}
if(!percentage1 == 0)
{
cout << "Percentage of Leu: " <<percentage1 <<endl;
}
if(!percentage2 == 0)
{
cout << "Percentage of Ile: " <<percentage2 <<endl;
}
if(!percentage3 == 0)
{
cout << "Percentage of STA: " <<percentage3 <<endl;
}
if(!percentage4 == 0)
{
cout << "Percentage of Val: " <<percentage4 <<endl;
}
if(!percentage5 == 0)
{
cout << "Percentage of Ser: " <<percentage5 <<endl;
}
if(!percentage6 == 0)
{
cout << "Percentage of Pro: " <<percentage6 <<endl;
}
if(!percentage7 == 0)
{
cout << "Percentage of Thr: " <<percentage7 <<endl;
}
if(!percentage8 == 0)
{
cout << "Percentage of Ala: " <<percentage8 <<endl;
}
if(!percentage9 == 0)
{
cout << "Percentage of Tyr: " <<percentage9 <<endl;
}
return 0;
}
the argument "string line" is supposedly the string produced by the previous function. But it is instead recognizing the line that is read from the input filestream.
Here, take this much less gross function:
int percentages(string line)
{
map<string, int> words;
double count = line.length() / 3;
for (int i = 0; i + 3 < line.length(); i += 3)
++words[line.substr(i, 3)];
string find[10] = {"Phe", "Leu", "Ile", "STA", "Val", "Ser", "Pro", "Thr", "Ala", "Tyr"};
for (int i = 0; i < 10; ++i)
cout << "Percentage of " << find[i] << ": " << words[find[i]] / count << endl;
return 0;
}
so I have a function that handles key presses in a game I'm working on in OpenGL. But, the thing is that even though I have made two squares and they both move when the correct key is pressed only one square is moved. Is there a way I can make the two squares move. This is the glutKeyboardFunc function I implimented:
void handleKeypress(unsigned char key, int x, int y)
{
if (key == 'w')
{
for (int i = 0; i < 12; i++)
{
if (i == 1 || i == 7 || i == 10 || i == 4)
{
square[i] = square[i] + 0.1;
}
}
}
if (key == 'd')
{
for (int i = 0; i < 12; i++)
{
if (i == 0 || i % 3 == 0)
{
square[i] = square[i] + 0.1;
}
}
}
if (key == 's')
{
for (int i = 0; i < 12; i++)
{
if (i == 1 || i == 7 || i == 10 || i == 4)
{
square[i] = square[i] - 0.1;
}
}
}
if (key == 'a')
{
for (int i = 0; i < 12; i++)
{
if (i == 0 || i % 3 == 0)
{
square[i] = square[i] - 0.1;
}
}
}
glutPostRedisplay();
}
If you need any more code just ask.
Edited for comments below.
// I'm making some assumptions about your functions here. Make adjustments.
// You can handle both players' key inputs here.
void handleKeypress(unsigned char key, int x, int y)
{
if (key == 27)
exit(0);
// Player 1
if (key == 'w')
{
A.moveSquareUp();
}
if (key == 'd')
{
A.moveSquareRight();
}
if (key == 's')
{
A.moveSquareDown();
}
if (key == 'a')
{
A.moveSquareLeft();
}
}
void handleSpecialKeypress(int key, int x, int y)
{
// Player 2
if (key == GLUT_KEY_UP)
{
B.moveSquareUp();
}
if (key == GLUT_KEY_RIGHT)
{
B.moveSquareRight();
}
if (key == GLUT_KEY_DOWN)
{
B.moveSquareDown();
}
if (key == GLUT_KEY_LEFT)
{
B.moveSquareLeft();
}
}
You need to handle your keyboard events somewhere in your game logic (the main loop, or a callback from glutKeyboardFunc()), and call the desired behaviours. This has some advantages:
Your keyboard input handling is unified in one place.
Using if instead of switch allows multiple keys being used.
You can organise this event handling inside your main loop, instead of depending on the timer.