Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / 2d sprite movement help

Author
Message
soundstone
15
Years of Service
User Offline
Joined: 11th Jan 2011
Location:
Posted: 12th Jan 2011 04:41
I quite new to programming in general let alone dbpro however I am still trying to create a game demo for future game creation. I am having trouble trying to get my ship to move forward based on the direction the sprite is facing. Here is what I have so far:

`offset ship rotation
offset sprite 1, 32, 32

`keep ship in vertical bounds
if shipy > 455 then shipy = 455
if shipy < 20 then shipy = 20

`rotate ship
if leftkey() then angle = wrapvalue(angle - 1)
if rightkey() then angle = wrapvalue(angle + 1)
rotate sprite 1, angle

`move ship left and right
`if leftkey() then shipx = shipx - shipspeed
`if rightkey() then shipx = shipx + shipspeed

`move ship
if upkey() then shipy = shipy - shipspeed
if downkey() then shipy = shipy + shipspeed

sprite 1, shipx, shipy, 1

`keep ship in horizontal bounds
if shipx > 615 then shipx = 615
if shipx < 25 then shipx = 25

basically I can get it to rotate but it still will only move horizontal then. my ultimate goal is to get it to rotate with the left and right keys and to move it foward and backward (relative to the rotation) with the up and down keys. Or perhaps eliminate the down key and only move it forward based on rotation. I hope I explained this well enough. Any help would be very much appreciated. Thanks in advance.
Van B
Moderator
23
Years of Service
User Offline
Joined: 8th Oct 2002
Location: Sunnyvale
Posted: 12th Jan 2011 08:56 Edited at: 12th Jan 2011 08:58
This is what I would do...



It might not work as expected, but probably would just be inverted angle. It would be quite easy to add smoother motion, for the movement speed especially - depends on the type of game. Note that most of your variables have # at the end now, that's so they are floats, it's always best to use floats when using non-integers, and functions like SIN and COS.
Also, I find it best to create the sprite, or re-create it before doing any rotations etc - so the order in your code has changed somewhat.

Health, Ammo, and bacon and eggs!
soundstone
15
Years of Service
User Offline
Joined: 11th Jan 2011
Location:
Posted: 13th Jan 2011 09:01
I tried your code and it sort of worked. The ship rotated and moved forward almost ok. When I held down the leftkey it kinda performed a parabolic swing back and forth. when no keys were pressed the ship moved on its on constantly rotating while moving along various x and y coordinates.
Grog Grueslayer
Valued Member
21
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 16th Jan 2011 01:49
The MOVE SPRITE command was designed to work with the ROTATE SPRITE command. It will move the sprite based on the current rotation of the sprite.



Login to post a reply

Server time is: 2026-07-18 02:19:23
Your offset time is: 2026-07-18 02:19:23