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.

Newcomers DBPro Corner / My first sprite movement program successful - needs some tweaks before I move on. Help?

Author
Message
Captain Coder
FPSC Reloaded TGC Backer
13
Years of Service
User Offline
Joined: 6th Jul 2011
Playing: Elite: Dangerous
Posted: 2nd Jan 2012 17:07
Hello fellow coders!

So, I have (successfully!) written a program that has a sprite rotate and move based on which arrowkey is pressed. The problem I'm having is that when I press an arrow key, it rotates and moves as it should, but it seems to move forward, [i]then[i] rotate in the direction specified, and then moves some more. It is supposed to rotate, then move. This is the code I have for it:



Any ideas?

Thanks!
Captain Coder

As a believer in Jesus Christ, I am trying to use my passion for game creation for His glory.
TheComet
17
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 2nd Jan 2012 18:28 Edited at: 2nd Jan 2012 18:30
That's some very odd behavior I can't explain... I've modified your code so I use calculations to find the new position rather than moving the sprite:



The angles when using upkey() and downkey() are reversed because the Y axis is inverted when working with 2D, and the commands newxvalue() and newzvalue() are intended for 3D calculations.

Also, please set up the screen so the sync is on, and use the sync command at the end of your main loop to refresh. This makes sure your game runs constantly at the value you specify at the very top with sync rate. If you don't do this, your game will run slower the more code you add.

Keep up the good work, you're doing great

TheComet

Captain Coder
FPSC Reloaded TGC Backer
13
Years of Service
User Offline
Joined: 6th Jul 2011
Playing: Elite: Dangerous
Posted: 2nd Jan 2012 20:32
Quote: "That's some very odd behavior I can't explain... I've modified your code so I use calculations to find the new position rather than moving the sprite:"


Thanks! I see what you are doing and it is very clever. that I think might be a bit beyond my ability to actually write on my own, (I've been dabbling in DBP on and off for the past couple of months), but I'll figure out soon enough

Quote: "please set up the screen so the sync is on, and use the sync command at the end of your main loop to refresh. This makes sure your game runs constantly at the value you specify at the very top with sync rate."


Oh, sure, no problem I thought sync applied to 3D programs only. That actually might fix my problem; I'll try it!

[Tries out TheComet's suggestion]

Ahh, I see the problem now! The image sprite is rotating around a very odd angle; not the center of the picture. I can probably use OFFSET SPRITE to fix that.

As for your code; it works like a dream; I like it! The only thing is that the sprite doesn't rotate so that the arrow is pointing in the direction it is moving. But that's good that you did that; allows me to learn by experience, which I badly need

Quote: "Keep up the good work, you're doing great"


Really? Thank you very much. I still have to learn to comment my code well

Thanks for the help,
Captain Coder

As a believer in Jesus Christ, I am trying to use my passion for game creation for His glory.
TheComet
17
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 2nd Jan 2012 23:01
Quote: "Ahh, I see the problem now! The image sprite is rotating around a very odd angle; not the center of the picture. I can probably use OFFSET SPRITE to fix that."


*slaps face* Of course! Nice one on finding that

The reason I find it better to use maths to find the new positions is because you can't "cheat". Your code uses box movement, where mine uses circular movement. If I move diagonally with your code, I can actually move 1.41 times faster than if I were to move horizontally or vertically. If you use newxvalue() and newzvalue(), you eliminate that problem. I've modified it again (sorry I hope I don't confuse you) so you can move on all 8 axes:



TheComet

Benjamin
21
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 3rd Jan 2012 03:23
In place of newxvalue and newyvalue I'd recommend learning to use cos and sin to plot movement, since it's much more universal and will most likely help you in the future. You don't really have to understand how they work, just know that with cos you can find the X coordinate, and with sin the Y coordinate, like this:



Here's a proper example showing it in action:



Note that when dealing with cos and sin you should always be dealing with floats, even for the pixel coordinates.

Hope it helps.



Support a charitable indie game project!
MrValentine
AGK Backer
13
Years of Service
User Offline
Joined: 5th Dec 2010
Playing: FFVII
Posted: 3rd Jan 2012 13:06
I really need to understamd cos and sin more...

TheComet
17
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 3rd Jan 2012 17:32 Edited at: 3rd Jan 2012 17:32
newxvalue() and newzvalue() are essentially cos() and sin(). I suggested using those commands because I find them easier to understand and there's less that could potentially go wrong. If you were to look at what the functions do, this is pretty much it here:



I have no idea what newyvalue() does though

If we visualise the commands, this is what's happening:



You can see that cos() and sin() calculate the length of both catheti of the right-angled triangle. newxvalue() and newzvalue() return the actual next position of the player if he were to travel in that angle with that speed, where cos() and sin() return the length the player should travel.

TheComet

Attachments

Login to view attachments
Benjamin
21
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 3rd Jan 2012 19:08
A little unsure what you're getting at. Are you saying you could have different results when doing it the manual (sin/cos) way?

Quote: "newxvalue() and newzvalue() are essentially cos() and sin()"


That's partially why I suggested it, because it's more or less the same thing except more universal.



Support a charitable indie game project!
TheComet
17
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 3rd Jan 2012 19:42
Quote: "A little unsure what you're getting at. Are you saying you could have different results when doing it the manual (sin/cos) way?"


What I'm trying to get at is that this
is more complex than this
in my opinion. Both ways work just as effectively, but I tend to use newxvalue and newzvalue.

TheComet

MrValentine
AGK Backer
13
Years of Service
User Offline
Joined: 5th Dec 2010
Playing: FFVII
Posted: 3rd Jan 2012 19:50
I would listen to TheComet... he is Legend

Benjamin
21
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 3rd Jan 2012 21:20
Oh right, yeah I agree. Just thought it'd be better to throw the universal method out there since it's something that you can take with you when you move on to other languages.



Support a charitable indie game project!
TheComet
17
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 3rd Jan 2012 21:39
Quote: "Oh right, yeah I agree. Just thought it'd be better to throw the universal method out there since it's something that you can take with you when you move on to other languages. "


Ah, now I see where you're coming from. Yes, it's good to understand what's really happening behind such functions, good point

TheComet

Hodgey
15
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 3rd Jan 2012 22:36
Just remember, when you are taking the formulas to other languages, to make sure you are using the degrees version of sin and cos. If you only have access to the radian versions you can convert from degrees to radians by multiplying the angle by pi/180. To convert from radians to degrees, multiply by 180/pi.

Captain Coder
FPSC Reloaded TGC Backer
13
Years of Service
User Offline
Joined: 6th Jul 2011
Playing: Elite: Dangerous
Posted: 4th Jan 2012 23:02
Wow, thanks for the help guys!

Quote: "Of course! Nice one on finding that"


Thanks!

I have added an "enemy" and have an idea on how to make him rotate to the player by tracking player's X and Y coordinates and then using something like the cos/sin idea above to calculate the angle the enemy should rotate to. No body give me the answer yet! This'll be a fun math/logic problem to figure out, I want to try to do it my self first (that's how I'll learn, too )

Thanks everyone,
Captain Coder

As a believer in Jesus Christ, I am trying to use my passion for game creation for His glory.
Captain Coder
FPSC Reloaded TGC Backer
13
Years of Service
User Offline
Joined: 6th Jul 2011
Playing: Elite: Dangerous
Posted: 5th Jan 2012 22:32
Okay, I've tried, and it doesn't want to work

Here is what I tried:



The variables "plrx" and "plry" were declared earlier. Could someone help me out?

Thank you,
Captain Coder

As a believer in Jesus Christ, I am trying to use my passion for game creation for His glory.
Hodgey
15
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 5th Jan 2012 22:40
I wrote a small chasing/tracking piece of code a while back, here is the relevant thread.

Benjamin
21
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 5th Jan 2012 22:45 Edited at: 5th Jan 2012 22:46
Hint: You can use the atanfull command to calculate the angle between two points.





Support a charitable indie game project!
TheComet
17
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 6th Jan 2012 13:34
Here are some trigonometry formulas that'll help you calculate everything in a right angled triangle.




c/a = cos(beta)
b/a = sin(beta)
b/c = tan(beta)

b/a = cos(gamma)
c/a = sin(gamma)
c/b = tan(gamma)


If your player is positioned at B, and the enemy at C, You'd solve for beta:

beta = arctan(b/c)

atanfull() is similar to the above : beta = atanfull(b,c). The only thing it does differently is it wraps it's output values. So now in code:



TheComet

Captain Coder
FPSC Reloaded TGC Backer
13
Years of Service
User Offline
Joined: 6th Jul 2011
Playing: Elite: Dangerous
Posted: 6th Jan 2012 18:06 Edited at: 6th Jan 2012 21:42
Hi all,

@Hodgey: I haven't taken a good look at your code yet, partially because I'm trying to learn as much of this by experience as possible. I do want to see how you did it though, thank you for your contribution!

@Benjamin and TheComet: ATANFULL makes the enemy rotate, thanks for the hint! However, I'm having a problem. When the player is on the same y-coordinate as the enemy, the enemy rotates to the player in the same direction. When the player is on the same x-coordinate as the enemy, the enemy is facing the opposite direction. Basically, he rotates in the opposite direction as the player. Here is the code I've eeked out.



Thanks!

As a believer in Jesus Christ, I am trying to use my passion for game creation for His glory.
TheComet
17
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 6th Jan 2012 18:30
Try this:



You need to switch x and y in the atanfull() function.

TheComet

Captain Coder
FPSC Reloaded TGC Backer
13
Years of Service
User Offline
Joined: 6th Jul 2011
Playing: Elite: Dangerous
Posted: 6th Jan 2012 21:38 Edited at: 6th Jan 2012 22:42
@TheComet: Huh! It works! Thank you, Comet for setting me straight. Interesting how I had to switch the X- and Y-coordinate variables out to make it work - perhaps I've got the variables holding the wrong coordinates?

All I did in addition to that was add 90 degrees to the answer the ATANFULL command gave and it made the enemy rotate to the player correctly (not adding ninety degrees caused the enemy's rotation to be ninety degrees off to the left).



So now that this challenge is resolved, I'm trying to decide which direction I should take this program in now. Do I want to give the player and enemy shooting capabilities? Should I figure out how to make multiple sprites use the same code to always rotate to the player? Which direction would all of the more experienced coders here recommend I go in?

Thank you all!
Captain Coder

EDIT: I decided to try cloning sprites and then make them all rotate to the player using code I had. It is a success! Here is what I ended up writing:

Sprite cloning:



Sprite rotation:



My first use of the FOR loop, I might add

As a believer in Jesus Christ, I am trying to use my passion for game creation for His glory.
Jamie_Leigh
12
Years of Service
User Offline
Joined: 15th Dec 2011
Location:
Posted: 25th Jan 2012 21:37
Hi everyone I'm very new to darkbasic and using it at college.

I decided to give moving a sprite around the screen a go. I used the code that was posted earlier, this is the my entire code at this point



When I run the game all I see is a black screen. Could any body help me with this?

Thankyou
Hodgey
15
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 25th Jan 2012 23:22
Hi Jamie, I glad you're using DBP at college. I myself am due to start a computer science degree this year at university but I don't know if I'll be lucky enough to use DBP in it.

With your current code, the sprite will only be drawn when a key is pressed. It would be better to create two variables 'x' and 'y', both integers, to store the coordinates of the sprite. So then you could put this in your loop:



You can then also change the if angle > -1 then do stuff bit to


This way your sprite should always be drawn.

Login to post a reply

Server time is: 2024-11-22 05:58:14
Your offset time is: 2024-11-22 05:58:14