Please Help With My Game!!! Asap
Author |
Message |
klxxguitarxx
|
Posted: Sat Dec 15, 2012 10:33 am Post subject: Please Help With My Game!!! Asap |
|
|
Please excuse my bad spelling, never been good at it
Can somebody help me out? i think i am going to have to use triginomatry. but im not to sure how to do it. if you could tell me how to do it and write a example in turing that would be great. i think that cos sin and tan a important, but idk .
im only working on the physics right now, so the graphics are not to great.
the black box is the player.
the mouse is target.
What is it you are trying to achieve?
make a bullet go from the player, to the mouse. but have the line keep going till it reaches the edge
What is the problem you are having?
Cant figure it out
Describe what you have tried to solve this problem
alot
Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
Turing: |
setscreen ("graphics:600;400")
%player
var player_x1 : int
var player_y1 : int
var player_x2 : int
var player_y2 : int
var chars : array char of boolean
%enemys
%map
var back_x : int
var ground : int
var map_maxx : int
var map_minx : int
%gravity/volocity
var volocity_x : int
var volocity_y : int
var volocity_y_const : int
var gravity : int
%projectiles
var projectile_increment : int
var projectile_x : int
var projectile_y : int
var bullet_screen : boolean := false
%curser
var mouse_x : int
var mouse_y : int
var mouse_click : int
%menu
%intro
%Declared variables
map_maxx := - 1700
map_minx := 0
ground := 0
back_x := - 700
player_x1 := 293
player_y1 := ground
player_x2 := player_x1 + 14
player_y2 := player_y1 + 22
volocity_x := 7
volocity_y := 15
volocity_y_const := 15
gravity := 1
procedure update
%Update variables
player_x2 := player_x1 + 14
player_y2 := player_y1 + 22
end update
procedure aim
Mouse.Where (mouse_x, mouse_y, mouse_click )
end aim
procedure player_position
Input.KeyDown (chars )
%check keys
if chars (KEY_RIGHT_ARROW) then
back_x - = volocity_x
elsif chars (KEY_LEFT_ARROW) then
back_x + = volocity_x
elsif chars (KEY_UP_ARROW) and player_y1 = ground then
player_y1 + = volocity_y
elsif chars (KEY_RIGHT_ARROW) and chars (KEY_UP_ARROW) and player_y1 = ground then
back_x - = volocity_x
player_y1 + = volocity_y
elsif chars (KEY_LEFT_ARROW) and chars (KEY_UP_ARROW) and player_y1 = ground then
back_x + = volocity_x
player_y1 + = volocity_y
end if
%Calculate variables
if player_y1 > ground then
volocity_y - = gravity
player_y1 + = volocity_y
elsif player_y1 = ground or player_y1 < ground then
player_y1 := ground
volocity_y := volocity_y_const
volocity_y := volocity_y_const
elsif back_x = map_maxx or back_x > map_maxx then
back_x := map_maxx
elsif back_x = map_minx or back_x < map_minx then
back_x := map_minx
end if
end player_position
procedure projectiles
end projectiles
procedure draw_map
Draw.FillBox (map_maxx,map_maxx, maxx, maxy, 53)
Draw.FillBox (player_x1, player_y1, player_x2, player_y2, black)
Draw.FillBox (back_x, 0, back_x + 25, 50, green)
%mouse
Draw.Oval (mouse_x, mouse_y, 35, 35, black)
Draw.Line (mouse_x, mouse_y - 35, mouse_x, mouse_y + 35, black)
Draw.Line (mouse_x - 35, mouse_y, mouse_x + 35, mouse_y, black)
Draw.FillOval (mouse_x, mouse_y, 3, 3, red)
Draw.Line (player_x1,player_y1,mouse_x,player_y1, blue)
Draw.Line (mouse_x,player_y1,mouse_x,mouse_y, blue)
Draw.Line (player_x1,player_y1,mouse_x,mouse_y, red)
if mouse_click = 1 then
end if
View.Update
end draw_map
loop
player_position
update
aim
draw_map
delay (25)
end loop
|
Please specify what version of Turing you are using
4.1.2 |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
TerranceN
|
Posted: Sat Dec 15, 2012 1:35 pm Post subject: Re: Please Help With My Game!!! Asap |
|
|
The first thing you should understand is how to use an angle and a distance to get a position relative to another. In other words, how to make a line of some length that's at some angle. Then I will show you how to get the angle between two points, which when combined will allow you to make any length of lines that point in the same direction as the mouse.
Take a look at the picture here. What we want to figure out is, given position A and the angle at A, and the distance between A and B, find position B. Figuring this out would allow us to draw lines of a specific length, at a specific angle, which will become really useful later when we find the angle between a point and the mouse. First of all, the distance between A and B is just c. Next, since the line segments a and b are aligned with our axis (the x-axis and y-axis) we can move b along the x axis and a along the y axis then we get to position B. So if we find out a and b then Bx = Ax + b and By = Ay + a, where Bx and By are the coordinates of B and Ax and Ay are the coordinates of A.
To find b, we can rearrange the equation
to get
similarly
We can then apply this like so:
Turing: |
%% Run this program and press a and d to change the angle
var aX, aY : int := 100
var bX, bY : int := 0
var len : int := 100
%% since this is using the degree versions of trig
%% functions (sind instead of sin, for example),
%% this value is in degrees
var angle : real := 45
var keys : array char of boolean
View.Set("offscreenonly")
loop
Input.KeyDown(keys )
if (keys ('a')) then
angle + = 1
elsif (keys ('d')) then
angle - = 1
end if
bX := aX + round(cosd(angle ) * len )
bY := aY + round(sind(angle ) * len )
cls()
Draw.Line(aX, aY, bX, bY, black)
View.Update()
%% Approximately 60 fps
Time.DelaySinceLast(17)
end loop
|
Now we just need to find the angle between the player and the mouse. Once again this can be done with trigonometry. If you go back to the diagram and imagine A as the player and B as the mouse, then we have three different equations to find the angle. Since finding c, the hypotenuse, will require us to find a and b anyway, we might as well use the equation with Tan (which only uses a and b), so you don't need to calculate c. So what are a and b? Simply the difference in the y-axis and x-axis between A and B. Specifically, b = Bx - Ax and a = By - Ay.
A fist attempt at writing a function to find the angle from one point to another might look like this:
Turing: |
fcn angleBetweenPoints (x1: real, y1: real, x2: real, y2: real): real
var dx : real := x2 - x1
var dy : real := y2 - y1
%% this is just the inverse of tand
%% you can think of it as reversing tan
%% arctand(tand(x)) = x
result arctand(dy / dx )
end angleBetweenPoints
|
After using this to find an angle, one can immediately see a problem: when your mouse is left of aX, the angle is the opposite of what we want. Another problem is that when the points are vertical we get a division by 0 error. We can solve the latter problem by checking if dx is 0 then returning either 90 (straight up) or 270 (straight down), depending on the value of dy. The former problem happens because arctand can only return values from -90 to 90. To fix this, we just need to increase the angle by half a rotation when x2 < x1 which is the same as dx < 0. When using degrees, 360 is a full rotation, so 180 is a half rotation.
Which gives us this working version of the function:
Turing: |
fcn angleBetweenPoints (x1: real, y1: real, x2: real, y2: real): real
var dx : real := x2 - x1
var dy : real := y2 - y1
%% need to manualy result values to avoid a divide by
%% zero error
if (dx = 0) then
if (dy >= 0) then
%% straight up
result 90
else
%% straight down
result 270
end if
%% need to reverse the result if angle is to the left,
%% since arctand can only return values from -90 .. 90
elsif (dx < 0) then
result arctand(dy / dx ) + 180
else
result arctand(dy / dx )
end if
end angleBetweenPoints
|
As for getting the line to draw to the edge of the screen, you can just use a very long length when applying what is explained above.
I hope this helped you understand how you can do it, good luck. |
|
|
|
|
![](images/spacer.gif) |
|
|