Slick Forums

Discuss the Slick 2D Library
It is currently Sat Dec 07, 2019 4:44 pm

All times are UTC




Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: Fri Apr 22, 2011 5:44 pm 
Offline

Joined: Fri Apr 22, 2011 5:32 pm
Posts: 2
Code:
Vector2f.sub(Vector2f);


Subtracting a vector from another vector by using this method, does not seem to work.

Using the component system from the tutorial, I made a camera component, which calculates the offset for any entity its attached to.
Notice the line, which I have commented out; I tried printing both curPos, and playerStartPos after that line, and they were both the same. The line below it is my temporary workaround.
I'm no Java guru, so if I've missed something, and this isn't a bug, I apologize.
Note that the class isn't finished!

Code:
package slicktest.engine.component.camera;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.geom.Vector2f;
import org.newdawn.slick.state.StateBasedGame;

import slicktest.engine.component.Component;

public class SideScrollingCamera extends Component{
   Component sideScrollerMovement;
   Vector2f offset = new Vector2f(0,0);
   Vector2f playerStartPos;

   public SideScrollingCamera(String id){
      this.id = id;
   }
   
   public Vector2f getOffset(){
      return offset;
   }
   
   @Override
   public void update(GameContainer gc, StateBasedGame sb, int delta) {
      Vector2f curPos = owner.getPosition().copy();
      if(playerStartPos == null){
         playerStartPos = curPos;
      }
      //offset = (curPos.sub(playerStartPos));
      offset = new Vector2f (curPos.x -playerStartPos.x, curPos.y - playerStartPos.y);
   }

}


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 22, 2011 6:26 pm 
Offline
Regular

Joined: Sun Dec 07, 2008 5:22 am
Posts: 238
Location: Vancouver, BC, Canada
If I am understanding this correctly then I think you are overlooking something.
Code:
playerStartPos = curPos;
means that both references point to the same vector. Also Vector2f is mutable, when you call sub() you are subtracting from the instance you call it on, the return is just a convenience for chaining.

So when you call
Code:
curPos.sub(playerStartPos)
you are subtracting playerStartPos from both of them. That means that after the first run through of this code playerStartpos will always be (0,0) and that on every successive run the subtract will appear to do noting.

What you probably want is:
Code:
@Override
public void update(GameContainer gc, StateBasedGame sb, int delta) {
  offset = owner.getPosition.copy();
  if(playerStartPos == null){
    playerStartPos = owner.getPosition.copy();
  }
  offset.sub(playerStartPos);
}


Also here is the code for the vector2f subtract method for info sake.
Code:
public Vector2f sub(Vector2f v)
{
    x -= v.getX();
    y -= v.getY();

    return this;
}

_________________
If at first quads don't succeed tri tri again.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 22, 2011 6:26 pm 
Offline
Game Developer

Joined: Sun Nov 12, 2006 11:18 pm
Posts: 890
Location: Germany
edit: Chronocide beat me :wink:

Pretty easy to explain:
Vector2f.sub(Vector2f) doesn't return a new Vector2f instance as you might have assumed but it modifies the receiver object (this or curPos in your case).

So you should maybe copy your curPos vector.

Code:
offset = curPos.copy().sub(playerStartPos);


That should do the trick.

_________________
Right Angle Games | Marte Engine
Back to the past | Star Cleaner | SpiderTrap


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 22, 2011 6:32 pm 
Offline

Joined: Fri Apr 22, 2011 5:32 pm
Posts: 2
I see. Thank you both!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group