Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 shooting mechanism in hockey game?
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
mapleleafs




PostPosted: Fri Feb 20, 2004 10:43 pm   Post subject: shooting mechanism in hockey game?

hey, i'm making this hockey game in turing, but i don't know how to make the players shoot the puck properly. here's the general framework that i have for shooting so far:

code:

Input.Keydown (chars)
if chars ('buttonForShoot') and playerHasPuck = true then
     loop
           exit when puckCoordinateX >= maxx
           puckCoorinateX += 5
     end loop
     playerHasPuck = false
end if
         


If i were to use this code in my game, the puck would just go straight at the wall/boards. I need to know how to make the puck go directly at the net, regardless of the location of the player/puck carrier. help is appreciated, and i will put you in my credits if i actually finish making this game.

and by the way, my hockey game will be moving from left and right, not up and down like the old SNES games
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Fri Feb 20, 2004 10:51 pm   Post subject: (No subject)

well first of all, NO. You dont make it into the loop. The force is applied to the puck just once. So you set puck's velocity and let it fly.

As for the direction. Use your trig/pythag knowledge to solve the "puck triangle"

hypotinuse = puck's total velocity
angle = tangent Y/X

solve for X, Y components and those are the compound velocities that will lead the puck straight into the net.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
mapleleafs




PostPosted: Sat Feb 21, 2004 3:10 pm   Post subject: (No subject)

i kind of understand, but the thing is i don't know trig....
so maybe you can give me the quick and dirty lesson on trigonometry, if not, can give me a nice site to learn from, cuz i am confus
Tony




PostPosted: Sat Feb 21, 2004 10:24 pm   Post subject: (No subject)

get your hands on a grade 10 math textbook and read the chapter.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
naoki




PostPosted: Mon Feb 23, 2004 4:39 pm   Post subject: (No subject)

Hmm, try something like About.com and see what they can offer

Basically the idea behind trig is finding a position on a circle. As you know, you can use the radius of the circle and draw a right angled triangle based on it. The height of the triangle is the sin portion and the base length is the cos.

By determining the angle of the applied force of the stick, you can calculate by how much should the puck move in increments
@DRI@N




PostPosted: Mon May 24, 2004 7:49 pm   Post subject: (No subject)

You could try and make a function to calculate slope...but the problem is it will give you a real number
guruguru




PostPosted: Mon May 24, 2004 7:59 pm   Post subject: (No subject)

I'll try here.

Lets say the horizontal distance between the puck and net is 15. And the vertical distance is 10.
code:

              /   o
         /      t |
     /            | adjacent = 10
 /                |
( ----------------|
     opposite = 15


To find the angle between the puck and the net, use a trig funtion called tan(). To much to explain how it works, but you can 'give' it the adjacent and opposite side and it will return the angle from the puck to the net.

tan^-1 (15/10) = 56 degrees

Ah sorry I got to go. I'll be back in 45 mins to complete this lesson Razz . Maybe someone else can tell you how to implement it.
guruguru




PostPosted: Mon May 24, 2004 8:43 pm   Post subject: (No subject)

Ok to continue. Lets say you want the total movement value to be 5. Think of 56 degrees as the ration between the x and y velocity values. (56/60). So to find the x velocity:

56 = x
90 TOTAL_MOVEMENT

x = 56 * TOTAL_MOVEMENT / 90
x = 3.1

So for this, the x velocity will be 3.1 pixels / cycle.

Using the same principle, the y value would be:

y = (90-56) * TOTAL_MOVEMENT / 90
y = 1.9

The y velcoity is 1.9 pixels / cycle.


To tie everything together... lets say the horizontal distance is 15. The vertical is 10.

code:

const TOTAL_MOVEMENT := 5
const RADIUS := 5

var puckX, puckY : int
var xVelocity, yVelocity : real
var angle : real
var horDistance : int
var verDistance : int
var netX, netY := 50

puckX := 200
puckY := 150

horDistance := puckX - netX
verDistance := puckY - netY

if verDistance = 0 then       % Test for divide by zero
    angle := 0
else
    angle := arctand (horDistance/verDistance)
end if

xVelocity := angle * TOTAL_MOVEMENT / 90
yVelocity := (90 - angle) * TOTAL_MOVEMENT / 90

loop     
     puckX -= round (xVelocity)
     puckY -= round (yVelocity)
     cls
     Draw.Oval (netX, netY, RADIUS, RADIUS, blue)
     Draw.FillOval (puckX, puckY, RADIUS, RADIUS, black)
     delay(100)
end loop


Thats the basics. And you can easily port this code so if the puck is below the net... or going in opposite directions...

Haha me so stupid Razz ! Arctand duh... Laughing

EDIT: Fixed code so it actually works Wink !

EDIT: Wow... I gotta fix this code better.
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 8 Posts ]
Jump to:   


Style:  
Search: