In my hockey game, shooting the puck is becoming a problem. How do I make t always shoot at the net? :confused:
Printable View
In my hockey game, shooting the puck is becoming a problem. How do I make t always shoot at the net? :confused:
find the differences between the coordinates and use trigonometry to give you a heading to the net. you shouldnt need Tan(), just Sin() or Cos().
Can't help much more without seeing your program.
I know of the functions, but I don't know trig yet :(
ok, gimme some numbers to play with and i'll try to work something our for you
the net is always here:
Net.x = 120
Net.y = 0
the player is all over
the screen is about 270
the height is about 300
bear with me, i'm talkin to someone knowledgeable in this kinda thing.
OK here goes, the following is not VB code, so you'll have to decypher it, (should be easy)...
Bearing = arcsin[ |x| / sqrt(x²+y²)] + 90(x>=0)(y<=0) + 180(x<=0)(y<=0) + 270(x<=0)(y>=0)
|x| means absolute value of x
sqrt means square root
(y<=0) etc. are logical operators.
x,y are the differences in positions.
am I too late to join this discussion? Maybe you would like to work with vectors instead?
Whatever would be easier, wossname's or kedaman if you think vectors are easier than please post :)
Vectors are of course easier, they save a lot of time, both in programming and runtime ;) trigs just consume a lot, hehe-.
Currently do you have a angle/speed set up? Or do you have anything else like vectors for the movement
Anyway i'm introducing you to vectors! Because that's exactly what you need ;) The speed is composed by x and y components so addition goes with no trigs at all, that means a lot of speed gain when calculating new position for each each sequence. To handle the coordinates you use an UDT, for instance here's Coordinate32:
Now the two functions i pasted here, the first one should project the result speed vector of target and source coordinates, whereas you set the target to the net and keep the source as the puck. VectorAdd should be just fine for moving the puck, just set the puck coordinate to what you return by adding the puck to the speed.Code:Type Coordinate32
x As Single
y As Single
End Type
Function Scalar32ProjG2Vector(Target As Coordinate32, source As Coordinate32, Velocity As Single) As Coordinate32
Dim k As Single
k = speed / ((Target.y - source.y) ^ 2 + (Target.x - source.x) ^ 2) ^ 0.5
With Scalar32ProjG2Vector
.x = k * (Target.x - source.x)
.y = k * (Target.y - source.y)
End With
End Function
Function Vector32Add(V1 As Coordinate32, V2 As Coordinate32) As Coordinate32
With Vector32Add
.x = V1.x + V2.x
.y = V1.y + V2.y
End With
End Function
Kedaman, you like physics don't you? :) One of my favorite classes back in college (until we got into E&M and ticked off the prof).
I love physics and math, especially in combination with VB :)
If you ever need help with Particle/Wave Duality problems, then I'm your guy!
I got 97% in that module in college, but i still got a "D" overall, go figure!