• Create Account

# Tankyroo

Member Since 05 Jul 2010
Offline Last Active Aug 17 2012 11:43 AM

### In Topic: 2D Collision detection, need some help.

16 August 2012 - 08:16 AM

Thanks for the replies everyone.

Okay, I've taken some of what each of you guys said, and I think I have a very very basic version of what I want, the only problem is, it's only on the X-axis.

Here is what I have so far:

```if(p.p.intersects(c.c) && p.x + p.width <= c.x + 5 && p.x + p.width >= c.x - 5){
p.x -= p.xVelocity * 60 / 1000.0;
p.xVelocity = 0.0f;
}
if(p.p.intersects(c.c) && p.x <= c.x + c.width + 5 && p.x >= c.x + c.width - 5){
p.x -= p.xVelocity * 60 / 1000.0;
p.xVelocity = 0.0f;
}
if(p.p.intersects(c.c) && p.y + p.height >= c.y - 5 && p.y + p.height <= c.y + 15){
p.y -= p.yVelocity * 60 / 1000.0;
p.yVelocity = 0.0f;
p.onGround = true;
}
```
This was in the update function of the main class by the way.

Please note that I'm only writing the collision code against one rectangle because this is just to learn the basics of collision detection.

What's happening now is the player will stop when he hits either side on the X-axis (just like I want), but if he jumps onto the block colliding on the Y-axis, he continuously bounces up and down very slightly, making my "onGround" boolean go from true to false very rapidly so the player is unable to jump. What can I do to fix this?

Also, I haven't done any collision detection on the bottom yet, but I'm assuming it will be similar to how I've done it on the X-axis.

P.S. The multiplying the velocity by 60 / 1000.0 is to simulate multiplying it by delta as I've done in the player class. Please tell me if I'm misunderstanding this whole delta thing.

### In Topic: 2D Collision detection, need some help.

15 August 2012 - 11:42 PM

How would I check the collision on the lines? I've never worked with lines like that before.

Thanks for the response!

### In Topic: [Java] Detecting collision between rectangles between classes

20 December 2011 - 11:20 AM

You are instantiating new Entities in the render method. These entities are not tested for collision, because they do not exist in the scope of that method. Instead, you are using the Entity "eo", which isn't drawn.

Consider instantiating the Entities in the constructor or init() function, and placing them in a container e.g. ArrayList<Entity>. In the render method, loop through all the Entities and draw them. In the update method, loop through all the entities and test for collision.

Edit: Never mind, I finally got it, thank you!

### In Topic: [java] How to create multiple instances of the same thing?

05 December 2011 - 09:18 PM

Sorry for the late reply, I was busy yesterday.

I kind of get what you're saying, but I'm still confused. Could you give an example of what the class would look like?

This response makes it apparent that you don't quite yet have a basic understanding of what object oriented programming is.

That said, I'd read up a bit more on class structure in Java, and also hone your understanding of all the data types used in Java.

Only reason I'm assuming this is because I had the EXACT same problem when I didn't have a full understanding of OOP and data types, regardless of the language.

In my first game ever (written in C++), I was instantiating monsters by just saying something along the lines of:

`monster = Monster(arguments)`

And then just manipulating the object associated with that variable 'monster' throughout my entire game. I never really knew how else to store those monster objects, other than assigning each individual monster to a variable like this.

Instantiating a new monster is as easy as just saying
`Monster(arguments)`
if you already have a Monster class, but it ultimately comes down to your own understanding of object oriented programming and knowing data types that will allow you to do different things with this small 1 line of code. What prh99 said, using an array, is a good solution, but requires you to have an understanding of your class object (Monster in your case) and an array type of data structure.

Hope this helps.

Thanks for the advice, do you have any suggestions as to where I should read-up on the topic? or is it all the same and I should just google an article on it?

### In Topic: [java] How to create multiple instances of the same thing?

05 December 2011 - 01:41 PM

This is something I've struggled with and ended up bypassing with the two games I have made, but I figure I better learn it sooner than later, so any help is appreciated, I'll try and be more specific below:

What I'm trying to do is make multiple instances of the same thing, the most efficient way. (Ex: two of the same enemy on screen at once, or multiple collectibles on screen at once.)

How do I do this? My best guess would be making a class that has all of the info, but I don't know how to lay that out, or draw it to the screen. I know I'm not describing it well but I hope someone can help. Thanks!

Oh also, once I do draw them and everything, how do I destroy them?

A class would probably make the most since to represent an enemy etc, what you need is something to contain the reference to each instance you create, an array for example.
As for what you store in that class, it depends on what you're representing. If it's an enemy you'd probably want to keep track of it's stats like health and attack power etc also more fundamentally it's location in on the screen and in your game map. You'd also functions to move it etc.

To destroy a class in Java you can either use delete e.g delete obj; and set the reference to NULL or destroy references to it by just setting it to NULL and the JVM's garbage collection will get to it at some point. The former is probably better than the latter. Then when you update/redraw the scene you only draw enemies in the array who's reference isn't NULL.

Sorry for the late reply, I was busy yesterday.

I kind of get what you're saying, but I'm still confused. Could you give an example of what the class would look like?

PARTNERS