Asteroids movement (off screen)

Game Editor comments and discussion.

Asteroids movement (off screen)

Postby d-soldier » Wed Jun 06, 2007 3:41 am

So I'm going to make an Asteroids game next, and I've been playing with the Asteroids demo trying to figure out what makes the 'roids continue their movement off screen/on screen... can anyone explain the code:
int w = view.width/2 + width;
int h = view.height/2 + height;
if(x > w) x = -w;
if(y > h) y = -h;
if(x < -w) x = w;
if(y < -h) y = h;

- and give a break down of what this does, so I better understand why it works?
User avatar
d-soldier
 
Posts: 703
Joined: Sun Apr 08, 2007 2:13 am
Score: 61 Give a positive score

Re: Asteroids movement (off screen)

Postby Sgt. Sparky » Wed Jun 06, 2007 3:58 am

d-soldier wrote:So I'm going to make an Asteroids game next, and I've been playing with the Asteroids demo trying to figure out what makes the 'roids continue their movement off screen/on screen... can anyone explain the code:
int w = view.width/2 + width;
int h = view.height/2 + height;
if(x > w) x = -w;
if(y > h) y = -h;
if(x < -w) x = w;
if(y < -h) y = h;

- and give a break down of what this does, so I better understand why it works?


you must use this code on the draw actor event of the asteroid,
the w is set to the Above, and if so and so value is less than it, so and so value = the opposite of so and so value and vise versa,
it is a simple code for Movement. :D
here is the code without w and h declared:
Code: Select all
if(x > view.width/2 + width) x = -view.width/2 + width;
if(y > view.height/2 + height) y = -view.height/2 + height;
if(x < -view.width/2 + width) x = view.width/2 + width;
if(y < -view.height/2 + height) y =view.height/2 + height;//notice that it is alot bigger and has
//to calculate it Each time.

the width and height are the actors width and height. :D
Image
Random Links:
viewtopic.php?p=19474#19474
Right now (10/14/2009) I'm working on some C++ projects, but I might be able to help if you have some Game Editor questions. :)
User avatar
Sgt. Sparky
 
Posts: 1850
Joined: Sat Oct 07, 2006 5:28 pm
Location: Somewhere out there, beneath the pale blue sky...
Score: 236 Give a positive score

Postby d-soldier » Thu Jun 07, 2007 12:48 am

Much better movement off/on screen there with your code Sparky, thanks for the revision!
User avatar
d-soldier
 
Posts: 703
Joined: Sun Apr 08, 2007 2:13 am
Score: 61 Give a positive score

Postby d-soldier » Thu Jun 07, 2007 12:53 am

What about a speedcap? With the current movement:

double newangle = (animpos/nframes)*360;
vectoradd(&angle, &directional_velocity, newangle, 0.51);

If you dont run into something you can keep accelerating until your just a blinking frame on the screen. Can this velocity be capped somehow?
User avatar
d-soldier
 
Posts: 703
Joined: Sun Apr 08, 2007 2:13 am
Score: 61 Give a positive score

Postby Fuzzy » Thu Jun 07, 2007 1:41 am

yes. use the floor and ceiling functions with the max speed you want.


speed = floor(speed, 16);

will return the lower of the two.. so your speed is always 16 or less.
Mortal Enemy of IF....THEN(and Inspector Gadget)

Still ThreeFingerPete to tekdino
User avatar
Fuzzy
 
Posts: 1068
Joined: Thu Mar 03, 2005 9:32 am
Location: Plymostic Programmer
Score: 95 Give a positive score

Postby d-soldier » Thu Jun 07, 2007 1:51 am

I'm not using a variable (speed)... How would the floor/ceiling functions incorporate into the code?
User avatar
d-soldier
 
Posts: 703
Joined: Sun Apr 08, 2007 2:13 am
Score: 61 Give a positive score

Postby Sgt. Sparky » Thu Jun 07, 2007 2:30 am

d-soldier wrote:I'm not using a variable (speed)... How would the floor/ceiling functions incorporate into the code?

by using &speed instead of &directional_velocity then using the code fuzzy posted,
oh, and right below the directional_velocity stuff put directional_velocity = speed. :D
Image
Random Links:
viewtopic.php?p=19474#19474
Right now (10/14/2009) I'm working on some C++ projects, but I might be able to help if you have some Game Editor questions. :)
User avatar
Sgt. Sparky
 
Posts: 1850
Joined: Sat Oct 07, 2006 5:28 pm
Location: Somewhere out there, beneath the pale blue sky...
Score: 236 Give a positive score

Postby DilloDude » Thu Jun 07, 2007 2:34 am

I think the functions you actually want are max and min. You should be able to just use directional_velocity if you aren't using another variable.
Code: Select all
directional_velocity = min(directional_velocity, 16);
Image
User avatar
DilloDude
 
Posts: 866
Joined: Tue Jan 24, 2006 9:51 am
Location: Nyerellion
Score: 58 Give a positive score

Postby Fuzzy » Thu Jun 07, 2007 2:54 am

use it on your directional_velocity.

What you have going there in the first post and Sparkies revisions are types of floor functions too.

Here is a trick i've shown before in light of your example..

Sparky says here is the code without w and h declared... take that common section and define it.

(in global)

Code: Select all
#define WRANGE view.width/2 + width
#define HRANGE view.width/2 + height


then in in the draw..

Code: Select all
if(x > WRANGE) x = -WRANGE;
if(y > HRANGE) y = -HRANGE;
if(x < -WRANGE) x = WRANGE;
if(y < -HRANGE) y = HRANGE;
// Smaller and neater, but it has to calculate it each time.
// but you can redefine the formula in one place and it affects all the code.
// Also, it doesnt have the overhead of a function.
Mortal Enemy of IF....THEN(and Inspector Gadget)

Still ThreeFingerPete to tekdino
User avatar
Fuzzy
 
Posts: 1068
Joined: Thu Mar 03, 2005 9:32 am
Location: Plymostic Programmer
Score: 95 Give a positive score

Postby d-soldier » Thu Jun 07, 2007 10:16 pm

... So I've got my thrusters(actor)which basically is invisible, rotating around on the same keydowns as the player, and transparency-adjusted when the right keydown is pressed (up/down). With this setup. it's either constant visible (when moving) or constant invisible (not moving)... can I script something that makes it invisible when not moving, and very quickly changing transparency values from 85-90%(to give a flickering effect) when the movement keydown is pressed?
Attachments
roids2.jpg
User avatar
d-soldier
 
Posts: 703
Joined: Sun Apr 08, 2007 2:13 am
Score: 61 Give a positive score

Postby Fuzzy » Thu Jun 07, 2007 11:58 pm

Sort of like a counter that flips rapidly between 0 and 1(on and off)? do you want it periodic in that short time, or erratic?

That is, it randomly picks a number between 85 and 95, or it shuts off intermittantly(goes to zero)?

For that, I would pick a number from one to 10, add it to 84. then dvide by 100 and assign that to the transparency.

For the other, again, a random number, but from 0.0 to 1.0. subtract a bit, say 0.2, and multiply by 4. That gives a range of -0.8 to 3.2. Use min/max to clamp it to zero and 1.0. Skip the if check and just clamp it both ways. Faster and less messy. That gives you a range of 0.0 to 1.0 with a high chance of it being 0.0 or 1.0 . use the result to multiply against yor transparency.

Play with those numbers and you will get a flicker that is either full on, or full off, with occasional weak bursts.
Last edited by Fuzzy on Fri Jun 08, 2007 12:16 am, edited 1 time in total.
Mortal Enemy of IF....THEN(and Inspector Gadget)

Still ThreeFingerPete to tekdino
User avatar
Fuzzy
 
Posts: 1068
Joined: Thu Mar 03, 2005 9:32 am
Location: Plymostic Programmer
Score: 95 Give a positive score

Postby d-soldier » Fri Jun 08, 2007 12:09 am

erratic, for sure... but super quick...
User avatar
d-soldier
 
Posts: 703
Joined: Sun Apr 08, 2007 2:13 am
Score: 61 Give a positive score

Postby Fuzzy » Fri Jun 08, 2007 12:20 am

you could also initialize an array of floats. hand pick or precalculate some values in the range you want and look them up with each draw. it would save math and computation. Faster and cleaner too.

float FlickerArray = {0.85, 0.0, 0.9, 0.0, 0.875, ....};
Mortal Enemy of IF....THEN(and Inspector Gadget)

Still ThreeFingerPete to tekdino
User avatar
Fuzzy
 
Posts: 1068
Joined: Thu Mar 03, 2005 9:32 am
Location: Plymostic Programmer
Score: 95 Give a positive score

ugg

Postby d-soldier » Fri Jun 08, 2007 10:28 pm

... Still on Asteroids (though not following the topic title)... I've recorded the sound of rocks hitting eachother for when the asteroids collide... but even though i have the playsound event set NOT to repeat while colliding, some collisions (where they end up colliding, bouncing back into another, which bounces them forward into the first one again) sound like machinegun fire, because it does start the sound every time they collide. Is there a way to script it so the sound (ogg file in this case) only plays if it's not already being played?
Attachments
roids3.jpg
User avatar
d-soldier
 
Posts: 703
Joined: Sun Apr 08, 2007 2:13 am
Score: 61 Give a positive score

Postby DilloDude » Sat Jun 09, 2007 1:41 am

This is actually a fairly tricky probem. Currently, there is no way to tell when a sound has finished. You'd have to use a timer or something. Otherwise, you might just stop the original sound. This means it'll still repeat, but you won't get them on top of eachother. Another related problem is only stopping a sound if that sound is still being played(so you don't stop another sound instead). I've thought of a method for that, but It probably won't help much.
One thing you'll probably want to do, though, is make it so only one asteroid makes the sound when two asteroids collide. If both are the same actor, this is simple:
Code: Select all
if (cloneindex < collide.cloneindex)
{
    //playsound
}
As no two of the same actor will have the same cloneindex, this will work. Without this, you'll get the sound playing twice at the same time.
Image
User avatar
DilloDude
 
Posts: 866
Joined: Tue Jan 24, 2006 9:51 am
Location: Nyerellion
Score: 58 Give a positive score

Next

Return to GE - General

Who is online

Users browsing this forum: No registered users and 1 guest

cron