Skip to main content
gamedev.net
🔒 Locked

Easy, easy question

Started by MarkyD 4 replies 600+ views
Original Post
MarkyD
MarkyD
In my game, I already have the player and the computer moving about on the screen. Now I want to add a feature where when the "fire" button is pressed, something will be shot from the player to the computer, at a fixed speed. This is where I''m stuck. I can simply find the difference between the player and the computer on each axis, but that would make the thingys being fired go at different speeds. I don''t know much about vectors and stuff like that; all I want is to know how to get x and y velocities for the axis, so that they aren''t going faster when the computer is further away. Please can someone help me! ~ There''s no substitute for failure ~
jag_oes
jag_oes
Hey, you don''t need to know anything about vectors, but hopefully you remember sine, cosine, and tangent. All three trigonometric function are just ratios of sides of triangles. Which sides? Look at this picture:


/|
/ |
r / | y
/ |
/_t______|
x


"x," "y," and "r" are the sides of the triangle and "t" is the angle made up of "x" and "r." The three functions are defined like this:

sin t = y / r
cos t = x / r
tan t = y / x

There are also inverse functions of the trigonometric function where you plug in a ratio and it outputs an angle. The only one we are going to use is arctangent (as you will see in a minute).

t = arctan (y / x)

Applying this to your situation, "x" and "y" would be the velocity of the object along the x- and y-axis (what you want to find out) and "r" is its speed (magnitude of the velocity ... what you do know).

You also don''t know "t" but you can find out. You can imagine the above triangle in another way to find "t." Imagine one player at where "x" and "z" intersect and the other player (the one you are shooting at) where "y" and "z" intersect. Therefore, "x" and "y" are simply the distances along the x- and y-axis between the two players. You would now use the arctangent function to find the angle.

Once you know the angle you move around the equations for sine and cosine. If you solve them for "x" and "y" that will give you the velocities in terms of "t" and "r," the quantities you know.

x = r cos t
y = r sin t

Here is how you could write everything:


// speed of the object fired from player1 to player2
speed = 5;

// distance along the x- and y-axis between the two players
delta_x = player2.x - player1.x;
delta_y = player2.y - player1.y;

// angle between the two players
angle = Math.atan2 (delta_y, delta_x);

// velocities along the x- and y-axis
vel_x = speed * Math.cos (angle);
vel_y = speed * Math.sin (angle);


Good luck.
MarkyD
MarkyD
OK, I didn''t use that technique. I could completely understand it and how it works, but after finding about 15 trigonometry functions and quite simply not getting it to work, I managed to get this code down instead:


procedure TGame.AddBall;
var
x,y: real;
dist: real;
ratio: real;
begin
x:=Cp.xPos-Plr.xPos;
y:=Cp.yPos-Plr.yPos;
dist:=Sqrt(x*x+y*y);
ratio:=dist/power;

Newball.xPos:=Player.xPos;
Newball.yPos:=Player.yPos;
Newball.xSpd:=x/ratio;
Newball.ySpd:=y/ratio;
end;


At least it''s working now.

~ There''s no substitute for failure ~
Beer Hunter
Beer Hunter
Yeah, that''s the best way to do it. Sometimes it''s tempting to use trig, but three trig calculations are going to be more expensive than one square root. The whole point of using vectors is to avoid dealing with angles, anyway.

Good luck
jag_oes
jag_oes
Indeed, indeed ...

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.