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.

AppGameKit Classic Chat / Applying angular impulse

Author
Message
Funnell7
12
Years of Service
User Offline
Joined: 8th Sep 2011
Location: UK, England
Posted: 4th Nov 2011 12:59 Edited at: 4th Nov 2011 14:01
Hello, I'm currently using impulse to move a sprite around which is working fine for up/down/left and right... However, what I am actually trying to do is move the sprite forward and backwards, using impuse and turning the sprite using SetSpriteAngle... Meaning that the forward impulse should always be applied to the back of the sprite taking into account the current angle, and vice versa for backwards... I think this requies some mental math, but seeing as maths is not my strong point I thought I'd ask for a little help...

If this doesn't make sense, think of Micro-Machines, thats pretty much what I am trying to do...

This is what I have so far...



However, this only moves the sprite up and down... I believe the direction x/y needs to be calculated based on the angle, and thats where I'm stumped... Thanks in advance...
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 4th Nov 2011 14:16 Edited at: 4th Nov 2011 14:17
Try something like this:


Funnell7
12
Years of Service
User Offline
Joined: 8th Sep 2011
Location: UK, England
Posted: 4th Nov 2011 14:21
Genius!! Thanks Baxslash! Works perfectly!!
bitJericho
21
Years of Service
User Offline
Joined: 9th Oct 2002
Location: United States
Posted: 4th Nov 2011 14:25 Edited at: 4th Nov 2011 15:08
It'd be better to move the sin/cos calculations to inside the if/then statement, because sin/cos calculations are intensive calculations.

To save some more processing time, you can pre-calculate the values of sin/cos. Bear in mind AppGameKit might already pre-calculate these values, I dunno, probably not though, you'd have to test to see which is faster. This is a great tutorial on implementing a lookup table:

http://wiki.processing.org/w/Sin/Cos_look-up_table


baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 4th Nov 2011 14:28
Good point @Jerico2day!

bitJericho
21
Years of Service
User Offline
Joined: 9th Oct 2002
Location: United States
Posted: 4th Nov 2011 15:10
You might also want to consider using a lookup table. I edited my above post with details.


baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 4th Nov 2011 15:25
I have to be honest, I'm using this method for 300 zombies and 20 marines in the game I took those two lines of code from and it runs smooth at 60fps (old desktop running on Vista)... I will use a lookup table at some point but if you're only running a few cars around a track or something I wouldn't worry

Anyways the code can be used to make a lookup table like this:


...then change my code to:


...although I haven't tested the results

Funnell7
12
Years of Service
User Offline
Joined: 8th Sep 2011
Location: UK, England
Posted: 4th Nov 2011 16:17
Great, thank you both for the input... For now, I think I'll stick with your first suggestion! Thanks all the same...

Login to post a reply

Server time is: 2024-04-19 10:35:56
Your offset time is: 2024-04-19 10:35:56