Game Design Music and Art

movement system – gaurdianAQ

gaurdianAQ

Member

Posts: 106
From:
Registered: 01-15-2007
I need to figure out a system to use so that I can move a ship like in asteroids where when you turn the ship it goes in the direction is facing preferably I would like it written in turing but if it can't then could you write it in BB and I will transfer it to turing
Mene-Mene

Member

Posts: 1398
From: Fort Wayne, IN, USA
Registered: 10-23-2006
I can't find the thread, but thats what sine, and Cosine are. Beware, in Sine and Cosine the angle starts out at east so you'll need to subtact 90 from your angle so it would be something like this.
BB:

;While you move
Ship\x = Ship\x + Cosine(Ship\Angle - 90)
Ship\y = Ship\y + Sine(Ship\Angle - 90)
;Continue looping

------------------
MM out-
Thought travels much faster than sound, it is better to think something twice, and say it once, than to think something once, and have to say it twice.
"Frogs and Fauns! The tournament!" - Professor Winneynoodle/HanClinto
"Of course, prayer requires that you actually take the time to listen for His answer..." - I'msold4Christ
"I would much rather say that every time you make a choice you are turning the central part of you, the part of you that chooses, into something a little different from what it was before." -C.S. Lewis, Mere Christianity

I reserve the full right to change my views/theories at any time.

[This message has been edited by Mene-Mene (edited February 23, 2007).]

gaurdianAQ

Member

Posts: 106
From:
Registered: 01-15-2007
that is how it was origanally being done but that caused to many problems using sine and cosine
Lava
Member

Posts: 1905
From:
Registered: 01-26-2005
If it's top-down view (like astertoids), my advice would be to try making the value of which you move your character a varible, and make that varible be linked with the rotational values.

Or make set values, so like if character rotation Y = 90 (however you find out how to measure the rotation) and move up, or wherever. But the set value way is too manual and would be very time consuming for a top-down game. Now it would work perfectly for a side-scrolling game, if you were making one.

[This message has been edited by LAVA (edited March 04, 2007).]

HanClinto

Administrator

Posts: 1828
From: Indiana
Registered: 10-11-2004
I've programmed a number of top-down asteroids style space shooters, and I'm sorry to say, I've never found a good way to do it other than using sine and cosine. Mene's solution was in the right direction.

Basically, you have to store a few things about your ship. Your coordinates (x and y values), your velocity (x and y values), and your heading (a single angle).


; game loop
; movement update section

; Rotation
if (LeftArrow) then ; If the player is pressing left
; Then rotate the ship ccw
ship\heading = ship\heading - ship\turnSpeed
end if
if (RightArrow) then ; If the ship is pressing right
; Then rotate the ship cw
ship\heading = ship\heading + ship\turnSpeed
end if

; Thrusters
if (UpArrow) then ; If the player is pressing the up arrow
; then add some thrust in the direction of their heading
ship\xVel = ship\xVel + Cosine(ship\heading - 90) * ship\thrustForce
ship\yVel = ship\yVel + Sine(ship\heading - 90) * ship\thrustForce
end if

; Then update the position according to their velocity
ship\xPos = ship\xPos + ship\xVel
ship\yPos = ship\yPos + ship\yVel

I'm just guessing at Blitz syntax from following Mene's example -- even though I'm sure it's wrong, I hope you get the basic gist of it.

All the best,
clint

[This message has been edited by HanClinto (edited March 06, 2007).]

jestermax

Member

Posts: 1064
From: Ontario, Canada
Registered: 06-21-2006
try using a lookup table; do all your calculates when you load a level for each direction and store it in memory. then when you want to figure something out then just pass your direction to a function and it'll give you back your vectors. just an idea...
gaurdianAQ

Member

Posts: 106
From:
Registered: 01-15-2007
well this is how I figured it out it uses a lot of code but it works much better and guess what I have decided to finish reading my game programming for teens book any ways here is how I did it in turing
if bulletangle = 0 then
bulletdx := 0
bulletdy := 6
elsif bulletangle = 1 or bulletangle = 2 or bulletangle = 3 then
bulletdx := -4
bulletdy := 6
elsif bulletangle = 4 or bulletangle = 5 or bulletangle = 6 then
shipdx := -6
shipdy := 4
elsif bulletangle = 7 or bulletangle = 8 then
bulletdx := -6
bulletdy := 2
elsif bulletangle = 9 then
bulletdx := -6
bulletdy := 0
elsif bulletangle = 10 or bulletangle = 11 or bulletangle = 12 then
bulletdx := -6
bulletdy := -2
elsif bulletangle = 13 or bulletangle = 14 or bulletangle = 15 then
bulletdx := -6
bulletdy := -4
elsif shipangle = 16 or bulletangle = 17 then
bulletdx := -4
bulletdy := -6
elsif bulletangle = 18 then
bulletdx := 0
bulletdy := -6
elsif bulletangle = 19 or bulletangle = 20 or bulletangle = 21 then
bulletdx := 2
bulletdy := -6
elsif bulletangle = 22 or bulletangle = 23 or bulletangle = 24 then
bulletdx := 6
bulletdy := -4
elsif bulletangle = 25 or bulletangle = 26 then
bulletdx := 6
bulletdy := -2
elsif bulletangle = 27 then
bulletdx := 6
bulletdy := 0
elsif bulletangle = 28 or bulletangle = 29 or bulletangle = 30 then
bulletdx := 6
bulletdy := 2
elsif bulletangle = 31 or bulletangle = 32 or bulletangle = 33 then
bulletdx := 4
bulletdy := 6
elsif bulletangle = 34 then
bulletdx := 2
bulletdy := 6
end if
end if
now I know that looks ugly but it works a heck of a lot better
dartsman

Member

Posts: 484
From: Queensland, Australia
Registered: 03-16-2006
I used to do stuff like that when I first started it's a good learning curve though, when you step out of the basic 2d movement (if's and hard coded values) and then into angled movement (trig and time-based movement)... I'm glad that you posted this, shows you have real dedication and that you didn't just take an easier way out.

I know you must have looked over and over the trig stuff and just now have moved to something which you are able to get done your own way, and you understand what it's doing

keep on learning your doing pretty well in my opinion

------------------
www.auran.com

gaurdianAQ

Member

Posts: 106
From:
Registered: 01-15-2007
ty but actually I don't know trig I am only in grade 9. lol I copied the trig stuff from another program and then came up with my own system
gaurdianAQ

Member

Posts: 106
From:
Registered: 01-15-2007
do you have any idea on how to make it so the bullet comes from the center of the ship with out having to add another 20 lines of code or am I going to have to do that?
jestermax

Member

Posts: 1064
From: Ontario, Canada
Registered: 06-21-2006
instead of using the ship's x/y position use (pseudo-code):

ship.x + ( ship.width / 2 )
and
ship.y + ( ship.height / 2 )

Lazarus

Member

Posts: 1668
From: USA
Registered: 06-06-2006
Hello.

I'm working on a movement system similar to this one - but ran into some problems.

In main loop:
INC ship1.xPos, ship1.xVel
INC ship1.yPos, ship1.yVel

and in sub:

IF KEY(200)
ship1.xVel = ship1.xVel - COS(ship1.heading-90) * thrust
ship1.yVel = ship1.yVel - SIN(ship1.heading-90) * thrust
ELSE
IF KEY(208)
ship1.xVel = ship1.xVel + COS(ship1.heading-90) * thrust
ship1.yVel = ship1.yVel + SIN(ship1.heading-90) * thrust

Looks fine, right -
Problem is, the object being moved will go off in seemingly random directions - never at the right angle.

Mene-Mene

Member

Posts: 1398
From: Fort Wayne, IN, USA
Registered: 10-23-2006
Laz: Couple of problems, 1. Don't you mean to say that:
ship1.xVel = ship1.xVel + COS(ship1.heading-90) * thrust

2. Sine is for Y, Cosine for X:
ship1.xVel = ship1.xVel + SIN(ship1.heading-90) * thrust
3. Wouldn't it be easier for 208 to be absolute of 200?

------------------
MM out-
Thought travels much faster than sound, it is better to think something twice, and say it once, than to think something once, and have to say it twice.
"Frogs and Fauns! The tournament!" - Professor Winneynoodle/HanClinto

I reserve the full right to change my views/theories at any time.

HanClinto

Administrator

Posts: 1828
From: Indiana
Registered: 10-11-2004
Hey Laz!

quote:
Originally posted by Lazarus:
I'm working on a movement system similar to this one - but ran into some problems.
*snip*
Looks fine, right -
Problem is, the object being moved will go off in seemingly random directions - never at the right angle.

In what units are you storing your ship's heading? Radians or degrees? Most computer programming languages use radians (where there are 2*pi of them in a circle) as opposed to degrees (where there are 360 of them in a circle).

Here's a decent page that shows how degrees and radians line up.

To convert from degrees to radians, multiply by (pi / 180). That would make your code look like the following:

IF KEY(200)
ship1.xVel = ship1.xVel - COS((ship1.heading-90) * 3.14159 / 180) * thrust
ship1.yVel = ship1.yVel - SIN((ship1.heading-90) * 3.14159 / 180) * thrust
ELSE
IF KEY(208)
ship1.xVel = ship1.xVel + COS((ship1.heading-90) * 3.14159 / 180) * thrust
ship1.yVel = ship1.yVel + SIN((ship1.heading-90) * 3.14159 / 180) * thrust

Give that a whirl, and see how it works for you.

To optimize it, you'd want to remove the division operation, and so the 3.14159 / 180 would turn into just 0.0174533, but I just left it that way so that you could see the theory behind it.

Hope that helps!

--clint

[This message has been edited by HanClinto (edited March 28, 2007).]

Lazarus

Member

Posts: 1668
From: USA
Registered: 06-06-2006
Hey, thanks for the replies, guys!

Clint, GLBasic uses degrees for the sin-cos-tan functions(a pleasant surprise - most Basics seem to use radians), so that part was working correctly.
I'm noting down your post for future reference, though - I've had alot of headaches with that degrees/radian problem in the past.

Mene, you hit on it - I reversed the sin-cos function calls, and it works perfectly. Sine for Y, Cosine for X. *slaps forehead*
Thanks a million.

Mene-Mene

Member

Posts: 1398
From: Fort Wayne, IN, USA
Registered: 10-23-2006
Laz: I'm so glad I could help, really I am. I've been constantly helped here, and glad I could return a little bit of ...

------------------
MM out-
Thought travels much faster than sound, it is better to think something twice, and say it once, than to think something once, and have to say it twice.
"Frogs and Fauns! The tournament!" - Professor Winneynoodle/HanClinto

I reserve the full right to change my views/theories at any time.

Lazarus

Member

Posts: 1668
From: USA
Registered: 06-06-2006
Glad to hear it, because I've got another question.

In this game - which is a Kspaceduel clone - the ship fires bullets(no surprise there).

I worked out firing one bullet at a time - as shown below:
(this is code plucked from various parts of the source)


TYPE bullet
xPos
yPos
xVel
yVel
heading
offscreen
ENDTYPE

...

GLOBAL bullet1 AS bullet

...

DEC bullet1.xPos, bullet1.xVel
DEC bullet1.yPos, bullet1.yVel

...

LOADSPRITE "ship1/bullet.bmp", ids

bullet1.xPos = ship1.xPos+10
bullet1.yPos = ship1.yPos+17
bullet1.heading = ship1.heading
bullet1.xVel = SIN(bullet1.heading-90) * 0.5
bullet1.yVel = COS(bullet1.heading-90) * 0.5

...

I'd like to be able to create another bullet each time the cannon is fired and have it move off in whichever direction, but - seems like I'd have to
make an entire copy of the above parts of the code for each new bullet.

Lazarus

Member

Posts: 1668
From: USA
Registered: 06-06-2006
K, nvm - I found a solution.