Line direction
Author |
Message |
Flikerator
|
Posted: Fri Feb 18, 2005 7:23 pm Post subject: Line direction |
|
|
I want the line to try and go towards the mouse, like it is now. But only draw about 20 pixels (Give or take) towards it and then stop. Does anyone know how to do that?
code: | var mx,my,mz:int
var x,y:int:=200
var ch : array char of boolean
colourback(black)
loop
Input.KeyDown (ch)
if ch ('w') or ch ('8') then
if y < maxy - 20 then
y += 5
end if
elsif ch ('s') or ch ('2') then
if y > 20 then
y -= 5
end if
elsif ch ('d') or ch ('6') then
if x < maxx - 20 then
x += 5
end if
elsif ch ('a') or ch ('4') then
if x > 20 then
x -= 5
end if
elsif ch ('7') then
if y < maxy - 20 and x > 20 then
y += 2
x -= 2
end if
elsif ch ('9') then
if y < maxy - 20 and x < maxx - 20 then
y += 2
x += 2
end if
elsif ch ('3') then
if y > 20 and x < maxx - 20 then
y -= 2
x += 2
end if
elsif ch ('1') then
if y > 20 and x > 20 then
x -= 2
y -= 2
end if
end if
Mouse.Where (mx,my,mz)
Draw.Line (x,y,mx,my,yellow)
delay (25)
View.Update
cls
end loop |
|
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
Neo
![](http://compsci.ca/v3/uploads/user_avatars/441131374462fea7633fd3.gif)
|
Posted: Fri Feb 18, 2005 8:03 pm Post subject: (No subject) |
|
|
Do you want a really easy way out? Draw a black circle where the mouse pointer is after youve drawn the line. ![Laughing Laughing](http://compsci.ca/v3/images/smiles/icon_lol.gif) |
|
|
|
|
![](images/spacer.gif) |
Flikerator
|
Posted: Fri Feb 18, 2005 9:10 pm Post subject: (No subject) |
|
|
No because I will have many different backgrounds
(Not just colours, like actually pictured backgrounds. |
|
|
|
|
![](images/spacer.gif) |
Flikerator
|
Posted: Sat Feb 19, 2005 9:17 am Post subject: (No subject) |
|
|
Can someone help me out here? |
|
|
|
|
![](images/spacer.gif) |
Martin
![](http://www.compsci.ca/wiki/images/4/46/CanadianStickUp.jpg)
|
Posted: Sat Feb 19, 2005 10:02 am Post subject: (No subject) |
|
|
Sure. It's easy with a bit of trig.
So let's say that you want your line to be of length L and the start and end points are (x1, y1) and (x2, y2) respectively.
Now, you can calculate the length of line (x1, y1, x2, y2) with pythagarus easily enough. Let's call that M.
Now you can draw a line (x1, y1, x2, y2) easily, but you want a line that is a stretch of that line. That is, you want a line that is scaled by a factor of 'L/M'
So your line will be: (x1, y1, x1 + (x2 - x1) * (L/M), y1 + (y2 - y1) * (L/M))
Hope it helps. |
|
|
|
|
![](images/spacer.gif) |
Drakain Zeil
|
Posted: Sat Feb 19, 2005 10:02 am Post subject: (No subject) |
|
|
Math.Distance ? |
|
|
|
|
![](images/spacer.gif) |
Flikerator
|
Posted: Sat Feb 19, 2005 11:47 am Post subject: (No subject) |
|
|
code: |
var x2, y2, mz : int
var x1, y1 : int := 200
var M, L : real := 5
var m, l : int := 5
var ch : array char of boolean
colourback (black)
View.Set ("offscreenonly")
loop
Input.KeyDown (ch)
if ch ('w') or ch ('8') then
if y1 < maxy - 20 then
y1 += 5
end if
elsif ch ('s') or ch ('2') then
if y1 > 20 then
y1 -= 5
end if
elsif ch ('d') or ch ('6') then
if x1 < maxx - 20 then
x1 += 5
end if
elsif ch ('a') or ch ('4') then
if x1 > 20 then
x1 -= 5
end if
elsif ch ('7') then
if y1 < maxy - 20 and x1 > 20 then
y1 += 2
x1 -= 2
end if
elsif ch ('9') then
if y1 < maxy - 20 and x1 < maxx - 20 then
y1 += 2
x1 += 2
end if
elsif ch ('3') then
if y1 > 20 and x1 < maxx - 20 then
y1 -= 2
x1 += 2
end if
elsif ch ('1') then
if y1 > 20 and x1 > 20 then
x1 -= 2
y1 -= 2
end if
end if
Mouse.Where (x2, y2, mz)
L := Math.Distance (x1, y1, x2, y2)
M := Math.Distance (x1, y1, x2, y2)
colour (white)
locate (1, 1)
put round (L)
put round (M)
Draw.Line (x1, y1, x1 + (x2 - x1) * (L div M), y1 + (y2 - y1) * (L div M), brightgreen)
Draw.FillOval (x1, y1, 10, 10, yellow)
delay (25)
View.Update
cls
end loop
|
Alright when I read that M/L thing this is what I came up with. It doesn't work correctly because both my "M" and "L" are the same. Which one do I change and how? |
|
|
|
|
![](images/spacer.gif) |
Martin
![](http://www.compsci.ca/wiki/images/4/46/CanadianStickUp.jpg)
|
Posted: Sat Feb 19, 2005 1:42 pm Post subject: (No subject) |
|
|
Don't use div.
Divide, multiply and then round the result down.
20 * (2 div 4) = 0, but round (20 * (2/4)) = 10 |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
Flikerator
|
Posted: Sat Feb 19, 2005 2:32 pm Post subject: (No subject) |
|
|
What absolutely must I change. Because I changed the div to "/" and tried a phew other things but im still just stabbing in the dark at what to do. Thanks for all the help though ![Razz Razz](http://compsci.ca/v3/images/smiles/icon_razz.gif) |
|
|
|
|
![](images/spacer.gif) |
Flikerator
|
Posted: Sat Feb 19, 2005 5:03 pm Post subject: (No subject) |
|
|
Cervantes helped me out. We gots it to work yay ^^ Looks awsome too
I also added a sprint feature with energy : D So I can add that into my zombie game. |
|
|
|
|
![](images/spacer.gif) |
Flikerator
|
Posted: Fri Feb 25, 2005 6:58 pm Post subject: (No subject) |
|
|
Ive been working on this for half an hour or so. It is probably a really easy answer but I can't figure it out. I need it to face the opposite direction of the mouse but only go a certain length.
I already have it going towards it but I need it to go away. If you need me to post more of the code just say so.
code: |
Draw.Line (x1, y1, x1 + round ((x2 - x1) * (L / M)), y1 + round ((y2 - y1) * (L / M)), 22)
M := Math.Distance (x1,y1,x2,y2)
|
|
|
|
|
|
![](images/spacer.gif) |
Cervantes
![](http://compsci.ca/v3/uploads/user_avatars/1023105758475ab2e040bde.jpg)
|
Posted: Fri Feb 25, 2005 7:19 pm Post subject: (No subject) |
|
|
30 minutes? That sucks.
It's the same code, just subtract the round ((x2 - x1) stuff instead of add it. Same with y.
code: |
Draw.Line (x1, y1, x1 - round ((x2 - x1) * (L / M)), y1 - round ((y2 - y1) * (L / M)), 22)
|
|
|
|
|
|
![](images/spacer.gif) |
|
|