forums

TNT Basic Forums > Programming Q&A
Movement and Aiming
< Last Thread     Next Thread >
Author
Thread        Post A Reply

10-27-2002 23:28

Posted by:
Tony Rose

Click Here to Email Tony Rose   Find more posts by Tony Rose

Ok so heres my problem...

How do I make an object move in the right direction if it is not facing perfectly straight in one direction. I.E.-If I have a tank facing 30 degrees how do I make the tank move straight in that direction? It is probably something simple but Im not very good with math or programing (yet) so I could use some help.

10-28-2002 03:30

Posted by:
eLL'

Click Here to Email eLL'   Find more posts by eLL'

The dread Slope formula

Do you know the slope formula, i beliee it might be of use in this situation, as well as later on when programming missile trajectory

10-28-2002 04:00

Posted by:
Holmes

Click Here to Email Holmes   Find more posts by Holmes

Component vectors!

You gotta break up the angle into its x,y components.

Lets say the tank is moving 5 pixels per second at 30 degrees. Then the tank is moving:

Sin(30)*5 pixels per second vertically
Cos(30)*5 pixels per secon horizontally.

You can basically say that:
Vertical distance covered per frame = Sin(Angle)*speed
Horizontal distance covered per frame = Cos(Angle)*speed

I'll tell you, game programming has really helped my geometry skills. If you're still unclear on this, ask me for some sample code.

10-28-2002 05:53

Posted by:
Tony Rose

Click Here to Email Tony Rose   Find more posts by Tony Rose

Ok...

Ok I understand what your saying and I see how it would work but I cant quite implement it right. If you could send me an example and also expain exactly what a Sin and Cos are that would be great. If its not to much trouble,my email is muzein@jg26.org

10-28-2002 06:10

Posted by:
Tony Rose

Click Here to Email Tony Rose   Find more posts by Tony Rose

Aha!

Actualy,I think I got it figured out pretty well, I poked around with it a bit more and now I got a sprite that rotates with a and d and moves foward in the correct direction when w is pressed. I would still like it if you had the time to show me an example of how a projectile coming out would work..im sure I could figure it out if I spend enough hours though :)
Thanks again,this was an immense help and I had been wondering how people did this for some time.

10-28-2002 10:08

Posted by:
GreySnake59

Click Here to Email GreySnake59   Find more posts by GreySnake59

never understood.

I have never understood people that use W,A,S,D keys on the keyboard.

Does anyone notice the arrow keys or the keypad?!?!?!?!?

10-28-2002 11:22

Posted by:
Mark_667

Click Here to Email Mark_667   Find more posts by Mark_667

sin & cos

The Sin. and Cos. you mentioned are to do with trigonometry, this also uses tangents and is fairly easy to pick up. If you need any more help on this try the BBC bitesize, GCSE Maths bit (v.good). Hope this helps,
Mark

10-28-2002 16:33

Posted by:
Tony Rose

Click Here to Email Tony Rose   Find more posts by Tony Rose

WASD

Almost every right handed gamer who has played with the WASD config never goes back to the arrows. Think about it,if you have your right hand on the mouse,are you gonna want to rest your left hand inches away? That gets quite uncomfortable. The WASD does the same thing as the Arrow keys,not to mention it gives better access to the space bar(thumb rests here),shift,control(pinky rests on these) and alot of other keys close by,and it does it with more comfort. I remember when games shipped with default arrow key control,but nowadays even the big companys have adopted WASD instead.

10-28-2002 19:51

Posted by:
jamie

Find more posts by jamie

WASD

Another good reason for using these keys is not everyone is right handed, if you were left handed I have to think it would be much more confortable and natural to use the WASD keys instead of reaching across the keyboard to the arrow keys or keypad.

Also for games with a lot of functions assigned to keys it is alot easier to reach the keys around the WASD keys for changing weapons, flipping switches, jumping, whatever... instead of just having the four arrow keys close together.

just my 2 cents

10-29-2002 02:25

Posted by:
Tony Rose

Click Here to Email Tony Rose   Find more posts by Tony Rose

Another Problem

Ok Ive come across another problem...

The tank seems to "slide" in the wrong direction when at certain angles. I noticed that if I divide its rotation by 12 (Only let it rotate 30 degrees at a time) it doesnt have this problem,but certain angles make the tank move "wrong". Like sometimes Ill have the tank rotated just a bit past 0 degrees,and you can see the image has rotated fine,but the tank still moves straight east. Any help would be appreciated.

Otherwise its coming along quite fine,gonna be a tank combat game in the sorta Bolo fashion,take bases,build defensive placements and fight. One cool thing im doin is independant turret control. Quite cool.

10-29-2002 04:17

Posted by:
Holmes

Click Here to Email Holmes   Find more posts by Holmes

Judging from what you said, and looking back at problems I've had in the past I'd say the graphic for your tank is probably pointed 90, 180, or 270 degrees off. But instead of redo-ing the graphic, try this first.

In the code where you have you're sprite being rotated like:
set sprite rotation thesprite,therotation

change it to
set sprite rotation thesprite,therotation+90

see what happens then. If that doesn't work try 180 and 270.

Hope this works, if not, send me your source file, I'm sure I could fix it.

10-29-2002 04:19

Posted by:
Holmes

Click Here to Email Holmes   Find more posts by Holmes

Whoops!

Whoops, nevermind, disregard that last post!

I know the problem. You're tanks coordinates are being stored as int's! You need them as floats because sine and cosine are going to make lots of decimal values!

10-29-2002 04:22

Posted by:
Holmes

Click Here to Email Holmes   Find more posts by Holmes

Source code

I made a LAN game a lot like this. It has tanks and independent turret control as well. Tell me if you'd like the source code.

10-29-2002 05:06

Posted by:
Tony Rose

Click Here to Email Tony Rose   Find more posts by Tony Rose

Perfect!

Great it worked! Thx alot for your help. Shoulda thought of tryin a float doh. Which brings me to another point,I almost always use ints but im starting to wonder why... If a float can do the same thing and more why not just always use a float,is it more memory consuming?
And yes I would love to see the game you made,my addy is muzein@jg26.org

10-29-2002 06:22

Posted by:
Tony Rose

Click Here to Email Tony Rose   Find more posts by Tony Rose

Nother Problem

I hate to spam the boards with questions,but I figure I might as well not get to frustrated spending over a couple hours tryin to do somethin (that always makes me want to go and do somethin else heh) and just ask one of you guys that is good at this stuff.
My problem is...
I cant get the Map Tile command to work right.

maptile=Map Tile(tankx,tanky,0)

Tankx and Tanky are just that,my x and y coordinates of my tank. Now shouldnt this line of code return the index of the tile at my tanks current location? I cant get it to work,it just returns a -1 always.

10-29-2002 16:18

Posted by:
John Treece-Birch

Find more posts by John Treece-Birch

Tile Coords

I think the problem is that the Map Tile function takes parameters in tile co-ordinates rather than canvas co-ordinates.

If you change the line to be something like this...

maptile=Map Tile(tankx/Map Tile Width,tanky/Map Tile Height,0)

.. then hopefully things should improve.

Cheers,
John

10-29-2002 19:14

Posted by:
Heiko Hartmann

Click Here to Email Heiko Hartmann   Find more posts by Heiko Hartmann

floats vs ints

Yes, floats consume more memory than integers - but today with Gigabytes of RAM this should no longer matter.

However, the must important difference for game makers is that floats are slower than integers (and with slower I mean slower by factors!). You may try this yourself (use for example a large loop and in the loop do an addition or multiplication).

So if you don't need floating point variables then don't use them.

Ciao, Heiko.

10-29-2002 19:15

Posted by:
Tony Rose

Click Here to Email Tony Rose   Find more posts by Tony Rose

Yup

It worked when I did it in that format,thanks!

03-14-2003 06:57

Posted by:
CubeFusion

Location:
California, USA

Find more posts by CubeFusion

Ok, now that I know how to get a vector out of an angle.

Vertical distance covered per frame = Sin(Angle)*speed
Horizontal distance covered per frame = Cos(Angle)*speed

How would I go about getting an angle out of a vector? My mind just can't wrap itself around this concept. What I'm looking for is something that would take a standard 2D vector (x1,y1 to x2,y2) and spit out what angle in degrees it is pointing at.

03-14-2003 10:04

Posted by:
No Frills

Click Here to Email No Frills   Find more posts by No Frills

Holmes Source Code repository

Oh man! Seems like since I posted my SIN WAVE help request, the TNT forum has turned into advanced mathmatics lessons for the terminally insane.... it's making my brain hurt!

Quote:

I made a LAN game a lot like this. It has tanks and independent turret control as well. Tell me if you'd like the source code.

Endquote:


yes please :-)

I'm sort of 'toying' with the idea (the graphics guy really, REALLY wants this) of adding LAN / Net support to my Worms clone I'm doing at present.

Is it small enough for you to e-mail over to me on no_frills@blueyonder.co.uk

How come you don't have a TNT website all of your own?


Oh, and I'm left handed - I use the mouse with my left hand, and so cursor key movement in games is a breeze with my right hand (I'm just a weird left-hander, I do some things left handed, some things right - what comes of being 'forced' (apparently - too you to really remember) to use my right hand to write with etc until I was about 9 by some suck ass scholl I attended many moons ago.

03-14-2003 21:39

Posted by:
Machi

Click Here to Email Machi   Find more posts by Machi

... this isn't meant as a criticism, but ignorance of trigonometry doesn't need to be a permanent state of mind. May I suggest that you look at this link http://aleph0.clarku.edu/~djoyce/java/trig/

and then have a look at the TNT maths commands and think through how you can use what you have learnt.

Hint, tan and invtan may be useful.

All times are GMT        Post A Reply

Forum Jump:
< Last Thread     Next Thread >

< Contact Us - TNT Basic >

Powered by: vBulletin Lite Version 1.0.1 Lite
Copyright © Jelsoft Enterprises Limited 2000.