Help with projectiles... [RESOLVED]
by Randy Lutcavich · in Torque X 2D · 09/26/2009 (5:44 am) · 5 replies
I'm not sure what the best way of firing a projectile at an odd angle (not Left/Right or Top/Down) is.
I'm using the AI from the blaster tutorial to kind of give me some guidance in figuring out how to shoot projectiles at odd angles.
I don't know what this code means though:
The way the code works now is the enemy is sent from the top of the screen to the bottom by setting its Y velocity to a negative number but along the way X velocity is dynamically updated depending on the direction to the player object. My confusion comes in with the line above when declaring the X direction... what does the last part mean/do (? 1 : -1)?
I'm using the AI from the blaster tutorial to kind of give me some guidance in figuring out how to shoot projectiles at odd angles.
I don't know what this code means though:
int DirectionX = (TargetX > SceneObject.Position.X) ? 1 : -1;
The way the code works now is the enemy is sent from the top of the screen to the bottom by setting its Y velocity to a negative number but along the way X velocity is dynamically updated depending on the direction to the player object. My confusion comes in with the line above when declaring the X direction... what does the last part mean/do (? 1 : -1)?
About the author
Recent Threads
#2
It has been too long since Trig.
Thank you, my projectiles now shoot in the correct direction.
I will also look into the ?: conditional operator.
09/26/2009 (8:01 pm)
You sir, rock!It has been too long since Trig.
Thank you, my projectiles now shoot in the correct direction.
I will also look into the ?: conditional operator.
#3
In the code I showed above:
An int variable (DirectionX) is being assigned either the value '1' or the value '-1'. If the conditional statement (TargetX > SceneObject.Position.X) is true than the value after the question mark (1) will be assigned. If the statement is false, the value after the semicolon (-1) is assigned.
09/26/2009 (8:06 pm)
Ok I understand the point of ?: now...In the code I showed above:
int DirectionX = (TargetX > SceneObject.Position.X) ? 1 : -1;
An int variable (DirectionX) is being assigned either the value '1' or the value '-1'. If the conditional statement (TargetX > SceneObject.Position.X) is true than the value after the question mark (1) will be assigned. If the statement is false, the value after the semicolon (-1) is assigned.
#4
John K.
www.envygames.com
09/26/2009 (9:07 pm)
Just for the record, I don't like the conditional operator and usually try to avoid it. Though it works well for shorthand, it definitely makes code less readable and is a pain to follow when step-debugging. I always encourage others to use simple if/else statements instead. John K.
www.envygames.com
#5
But it is nice to know what it means in case I run across it again. I most likely won't use it though.
09/26/2009 (9:09 pm)
You are definitely right about it causing extra confusion, John. I would have had no trouble understanding a simple if/else statement. But it is nice to know what it means in case I run across it again. I most likely won't use it though.
Torque Owner Lucas Shinkovich
The code you are referring to is officially called the conditional operator, you can read more about it on MSDN here: http://msdn.microsoft.com/en-us/library/ty67wk28(VS.80).aspx. Some people also call it the ternary operator comming from C++.
I'm not sure what you mean about odd angles to shoot projectiles but I use the following code to shoot projectiles in my game:
//Apply velocity to the projectile projectile.Physics.VelocityX = (float)Math.Sin(MathHelper.ToRadians(projectile.Rotation)) * _shotSpeed; projectile.Physics.VelocityY = -(float)Math.Cos(MathHelper.ToRadians(projectile.Rotation)) * _shotSpeed;This is of course assuming you have done a few things ahead of time. You have a projectile (this is the variable projectile above) already created and rotated in the direction you would like to fire it. The projectile has the physics component so a velocity can be applied to it. You have defined a speed for the projectile (this is the variable (_shotSpeed above).