Jump to content

  • Log In with Google      Sign In   
  • Create Account


Target bounce away from wall


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
10 replies to this topic

#1 Thomas Wiborg   GDNet+   -  Reputation: 643

Like
0Likes
Like

Posted 19 January 2013 - 06:07 PM

Im very newbie and making a 2d spaceship game, similar to Stargunner.
I have target(enemies) coming from right side of screen and heading left, where my ship is at.
My targets are circles who randomly chooses Y coords from 0, 640.
The thing is. When one of my targets hit the upper "wall" i want them to bounce down.
And my problem is that i cant modify the value of Points. What can i do, is it even possible to modify Points values?

 

I have my properties in GameItem class.

 

"Cannot modify the return value of 'SpaceAttack.GameItem.Position' because it is not a variable"


 

 if (target.Position.Y < 0)
            {
                target.Position = new Point(this.target.Position.X, this.target.Position.Y = 0);

                target.Velocity = new Point(this.target.Velocity.X, this.target.Velocity.Y *= -1);
            }

 

 



Sponsor:

#2 __SKYe   Members   -  Reputation: 991

Like
1Likes
Like

Posted 19 January 2013 - 07:00 PM

You don't need to bounce enemies off walls.

 

If the enemies must appear within the range [0 - 640], then all you have to do is get a random value between 0 + enemyHeight/2 and 640 - enemyHeight/2 (the enemyHeight/2 in a circle is the radius).

This makes the enemy spawn inside the screen.

 

If what your asking is that you have an enemy that can move in the Y axis (up/down), and when it would go outside the screen you want to bounce it off on the walls, then a simple way to do it, is if you have a variable for the enemy's velocity(acceleration), when the enemy hits the wall, you change the velocity's sign. This will make the enemy suddenly move in the other direction, therefore bouncing it off on the wall.

 

I don't know what language you're working on, but to me it seems it is ActionScript.

 

If it is so, then perhaps you can go to http://asgamer.com/2009/flash-game-design-basics-adding-library-objects-to-stage-with-as3 to get a good tutorial on how to make spaceship game (although this is a vertical shooter) in AS3. It presents various concepts such as ship movement, the bouncing on walls, enemies, and even a star filled background.

This tutorials are separated into articles, so search them in the site.

 

If you're not using AS3, then perhaps you could show some more code, to see if i can help you more.



#3 0Circle0   Members   -  Reputation: 342

Like
1Likes
Like

Posted 19 January 2013 - 07:05 PM

If your enemy is less than the screen min or greater than the screen max you would reverse the speed of x and/or y.

 

xSpeed = 1;
ySpeed = 1;
If (target.x < screenMinX || target.x > screenMaxX)
    xSpeed = -xSpeed; //reverse
if(target.y < screenMinY || target.y > screenMaxX)
    ySpeed = -ySpeed; //reverse</pre>
target.x += xSpeed;
target.y += ySpeed;

 

you mean that?


Edited by 0Circle0, 19 January 2013 - 07:08 PM.

Sprite Creator 3 VX & XP

WARNING: I edit my posts constantly.


#4 Thomas Wiborg   GDNet+   -  Reputation: 643

Like
0Likes
Like

Posted 20 January 2013 - 04:18 AM

Correct. But the problem is that i cant modify the value of point. Is there a way to modify point?
Im using C# XNA :)

#5 Khatharr   Crossbones+   -  Reputation: 2940

Like
1Likes
Like

Posted 20 January 2013 - 07:51 AM

Why can you not modify the point?

 

The code you posted in the OP was trying to replace the point, which could be no good for any number of reasons depending on the class. Can't you just

 

if(target.Position.Y < 0) {
  target.Position.Y = 0;
  target.Velocity.Y = -target.Velocity.Y;
}

Edited by Khatharr, 20 January 2013 - 07:55 AM.

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

#6 Mizu   Members   -  Reputation: 972

Like
1Likes
Like

Posted 20 January 2013 - 08:17 AM

If target.Velocity is a C# Property then you won't be able to modify its members, since a Point in C# is of the struct data type. What this means is that target.velocity returns a temporary copy of your data, so modifying that has little meaning. The solution is to either just make velocity public, without any get:ers and set:ers, or create a method that allow you to set velocity, like so

 

public void SetVelocity(int velX, int velY)
{
      velocity.X = velX;
      velocity.Y = velY;
}

 

Why are you using Points by the way? I would suggest using Vector2 for things like speed and velocity. It's better suited for that kind of stuff

 

 

Hope that helps smile.png



#7 Thomas Wiborg   GDNet+   -  Reputation: 643

Like
0Likes
Like

Posted 20 January 2013 - 09:01 AM

Thanks alot for super info guys. Ill try this out! I will post my results :-)

#8 Thomas Wiborg   GDNet+   -  Reputation: 643

Like
0Likes
Like

Posted 20 January 2013 - 11:49 AM

If target.Velocity is a C# Property then you won't be able to modify its members, since a Point in C# is of the struct data type. What this means is that target.velocity returns a temporary copy of your data, so modifying that has little meaning. The solution is to either just make velocity public, without any get:ers and set:ers, or create a method that allow you to set velocity, like so

 

public void SetVelocity(int velX, int velY)
{
      velocity.X = velX;
      velocity.Y = velY;
}

 

Why are you using Points by the way? I would suggest using Vector2 for things like speed and velocity. It's better suited for that kind of stuff

 

 

Hope that helps smile.png

 

Well, Vector2 aint possible to modify like variables either?



#9 Mizu   Members   -  Reputation: 972

Like
0Likes
Like

Posted 20 January 2013 - 12:11 PM

That's right. Vector2 are also a struct, so the solution is the same as those a suggested in my previous post smile.png

Edited by Mizu, 20 January 2013 - 12:12 PM.


#10 Thomas Wiborg   GDNet+   -  Reputation: 643

Like
0Likes
Like

Posted 20 January 2013 - 12:16 PM

Yeah, but what you say, is that Vector2 is better to use than Point in games? Well I fixed it. And here is my new code. Thanks alot for the help guys! Realy amazing community here on gamedev!

 


            if (target.Position.Y < 0)
            {
                Vector2 vv;
                Vector2 vp;
                vv = target.Velocity;
                vv.Y *= -1;
                target.Velocity = vv;
                
                vp = target.Position;
                vp.Y = 0;
                target.Position = vp;            
            }
            else if (target.Position.Y > SCREEN_HEIGHT - target.Origin.Y)
            {
                Vector2 v2v;
                Vector2 v2p;
                v2v = target.Velocity;
                v2v.Y *= -1;
                target.Velocity = v2v;

                v2p = target.Position;
                v2p.Y = SCREEN_HEIGHT - target.Origin.Y;
                target.Position = v2p;      
            }

 


Edited by Thomas Wiborg, 20 January 2013 - 12:17 PM.


#11 Khatharr   Crossbones+   -  Reputation: 2940

Like
0Likes
Like

Posted 20 January 2013 - 04:32 PM

Yeah, but what you say, is that Vector2 is better to use than Point in games?
No, that's not what he said. He's pointing out that velocity is a vector-type value, so using a vector to represent velocity it is a good idea.

http://en.wikipedia.org/wiki/Velocity

http://en.wikipedia.org/wiki/Vector_(geometry)
void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS